Skip to content

Commit

Permalink
feat(gatsby-source-graphql): Default Apollo Link fetch wrapper to sho…
Browse files Browse the repository at this point in the history
…w better API errors (#28786)

* apollo fetch http error status

* update tests

Co-authored-by: Vladimir Razuvaev <[email protected]>
Co-authored-by: gatsbybot <[email protected]>
  • Loading branch information
3 people authored Dec 29, 2020
1 parent 3b40d80 commit abdb8d6
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 4 deletions.
4 changes: 2 additions & 2 deletions packages/gatsby-source-graphql/src/__tests__/gatsby-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down Expand Up @@ -98,7 +98,7 @@ describe(`createHttpLink`, () => {
})

expect(createHttpLink).toHaveBeenCalledWith(
expect.objectContaining({ fetch: nodeFetch })
expect.objectContaining({ fetch: fetchWrapper })
)
})
})
16 changes: 16 additions & 0 deletions packages/gatsby-source-graphql/src/fetch.js
Original file line number Diff line number Diff line change
@@ -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
}
4 changes: 2 additions & 2 deletions packages/gatsby-source-graphql/src/gatsby-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -26,7 +26,7 @@ exports.sourceNodes = async (
typeName,
fieldName,
headers = {},
fetch = nodeFetch,
fetch = fetchWrapper,
fetchOptions = {},
createLink,
createSchema,
Expand Down

0 comments on commit abdb8d6

Please sign in to comment.