Created
September 3, 2020 13:48
-
-
Save PCreations/ae247722292bb5d598fe95cb93efaa42 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 createExecuteLatestArticlesQuery = ({ | |
sendQuery = axios, | |
} = {}) => ({ domain, graphQLEndpoint }) => | |
sendQuery({ | |
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); | |
}); | |
const fakeSendQuery = (articlesForDomain) => (request) => | |
Promise.resolve({ | |
data: { | |
data: { | |
latestArticles: articlesForDomain[request.headers.domain], | |
}, | |
}, | |
}); | |
export const createFakeExecuteLatestArticlesQuery = (articlesForDomain) => | |
createExecuteLatestArticlesQuery({ | |
sendQuery: fakeSendQuery(articlesForDomain), | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment