Skip to content

Commit abdb8d6

Browse files
antoinerousseauvladargatsbybot
authored
feat(gatsby-source-graphql): Default Apollo Link fetch wrapper to show better API errors (#28786)
* apollo fetch http error status * update tests Co-authored-by: Vladimir Razuvaev <vladimir.razuvaev@gmail.com> Co-authored-by: gatsbybot <mathews.kyle+gatsbybot@gmail.com>
1 parent 3b40d80 commit abdb8d6

3 files changed

Lines changed: 20 additions & 4 deletions

File tree

packages/gatsby-source-graphql/src/__tests__/gatsby-node.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ jest.mock(`gatsby/graphql`, () => {
2020
}
2121
})
2222
const { sourceNodes } = require(`../gatsby-node`)
23-
const nodeFetch = require(`node-fetch`)
23+
const { fetchWrapper } = require(`../fetch`)
2424

2525
const getInternalGatsbyAPI = () => {
2626
const actions = {
@@ -98,7 +98,7 @@ describe(`createHttpLink`, () => {
9898
})
9999

100100
expect(createHttpLink).toHaveBeenCalledWith(
101-
expect.objectContaining({ fetch: nodeFetch })
101+
expect.objectContaining({ fetch: fetchWrapper })
102102
)
103103
})
104104
})
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
const nodeFetch = require(`node-fetch`)
2+
3+
// this is passed to the Apollo Link
4+
// https://www.apollographql.com/docs/link/links/http/#fetch-polyfill
5+
6+
exports.fetchWrapper = async (uri, options) => {
7+
const response = await nodeFetch(uri, options)
8+
9+
if (response.status >= 400) {
10+
throw new Error(
11+
`Source GraphQL API: HTTP error ${response.status} ${response.statusText}`
12+
)
13+
}
14+
15+
return response
16+
}

packages/gatsby-source-graphql/src/gatsby-node.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ const {
77
} = require(`@graphql-tools/wrap`)
88
const { linkToExecutor } = require(`@graphql-tools/links`)
99
const { createHttpLink } = require(`apollo-link-http`)
10-
const nodeFetch = require(`node-fetch`)
1110
const invariant = require(`invariant`)
11+
const { fetchWrapper } = require(`./fetch`)
1212
const { createDataloaderLink } = require(`./batching/dataloader-link`)
1313

1414
const {
@@ -26,7 +26,7 @@ exports.sourceNodes = async (
2626
typeName,
2727
fieldName,
2828
headers = {},
29-
fetch = nodeFetch,
29+
fetch = fetchWrapper,
3030
fetchOptions = {},
3131
createLink,
3232
createSchema,

0 commit comments

Comments
 (0)