Skip to content

Instantly share code, notes, and snippets.

@PCreations
Last active September 3, 2020 12:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save PCreations/2694ba504d84a767f2b9d07dfbf44629 to your computer and use it in GitHub Desktop.
Save PCreations/2694ba504d84a767f2b9d07dfbf44629 to your computer and use it in GitHub Desktop.
import axios from "axios";
export class GraphQLError extends Error {
constructor(graphQLErrors = []) {
super();
this.message = `GraphQL errors : ${graphQLErrors
.map(({ message }) => message)
.join("\n")}`;
}
}
const LATEST_ARTICLES_QUERY = `
query {
latestArticles {
publicationDate
title
url
}
}
`;
export const executeLatestArticlesQuery = ({ domain, graphQLEndpoint }) =>
axios({
url: graphQLEndpoint,
method: "POST",
data: {
query: LATEST_ARTICLES_QUERY,
},
headers: {
"content-type": "application/json",
domain,
},
})
.then((response) => response.data.data.latestArticles)
.catch((error) => {
throw new GraphQLError(error.response.data.errors);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment