diff --git a/packages/gatsby-source-graphql/src/__tests__/gatsby-node.js b/packages/gatsby-source-graphql/src/__tests__/gatsby-node.js index 0e0a2c8476117..094cf264a93f1 100644 --- a/packages/gatsby-source-graphql/src/__tests__/gatsby-node.js +++ b/packages/gatsby-source-graphql/src/__tests__/gatsby-node.js @@ -20,7 +20,7 @@ jest.mock(`gatsby/graphql`, () => { } }) const { sourceNodes } = require(`../gatsby-node`) -const nodeFetch = require(`node-fetch`) +const { fetchWrapper } = require(`../fetch`) const getInternalGatsbyAPI = () => { const actions = { @@ -98,7 +98,7 @@ describe(`createHttpLink`, () => { }) expect(createHttpLink).toHaveBeenCalledWith( - expect.objectContaining({ fetch: nodeFetch }) + expect.objectContaining({ fetch: fetchWrapper }) ) }) }) diff --git a/packages/gatsby-source-graphql/src/fetch.js b/packages/gatsby-source-graphql/src/fetch.js new file mode 100644 index 0000000000000..6b6589a04ff11 --- /dev/null +++ b/packages/gatsby-source-graphql/src/fetch.js @@ -0,0 +1,16 @@ +const nodeFetch = require(`node-fetch`) + +// this is passed to the Apollo Link +// https://www.apollographql.com/docs/link/links/http/#fetch-polyfill + +exports.fetchWrapper = async (uri, options) => { + const response = await nodeFetch(uri, options) + + if (response.status >= 400) { + throw new Error( + `Source GraphQL API: HTTP error ${response.status} ${response.statusText}` + ) + } + + return response +} diff --git a/packages/gatsby-source-graphql/src/gatsby-node.js b/packages/gatsby-source-graphql/src/gatsby-node.js index 50197dfedb7a4..eea80e1a247f2 100644 --- a/packages/gatsby-source-graphql/src/gatsby-node.js +++ b/packages/gatsby-source-graphql/src/gatsby-node.js @@ -7,8 +7,8 @@ const { } = require(`@graphql-tools/wrap`) const { linkToExecutor } = require(`@graphql-tools/links`) const { createHttpLink } = require(`apollo-link-http`) -const nodeFetch = require(`node-fetch`) const invariant = require(`invariant`) +const { fetchWrapper } = require(`./fetch`) const { createDataloaderLink } = require(`./batching/dataloader-link`) const { @@ -26,7 +26,7 @@ exports.sourceNodes = async ( typeName, fieldName, headers = {}, - fetch = nodeFetch, + fetch = fetchWrapper, fetchOptions = {}, createLink, createSchema,