Skip to content

Instantly share code, notes, and snippets.

@gdhardy1
Last active December 22, 2020 22:50
Show Gist options
  • Save gdhardy1/f88d3a78cc82c0fbf00915b190ced357 to your computer and use it in GitHub Desktop.
Save gdhardy1/f88d3a78cc82c0fbf00915b190ced357 to your computer and use it in GitHub Desktop.
Gatsby Retry GraphQL Query
// Composing Apollo links
// https://www.apollographql.com/docs/link/composition/
// for apollo-retry-link info:
// - https://www.apollographql.com/docs/link/links/retry/
// - https://www.youtube.com/watch?v=bv74TcKb1jw
// for gatsby-source-grapql config info:
// see https://www.gatsbyjs.com/plugins/gatsby-source-graphql/
// gatsby-config.js
// Creating Apollo Retry Link
const { createHttpLink, ApolloLink } = require("@apollo/client")
const { RetryLink } = require("@apollo/client/link/retry")
const retryLink = new RetryLink({
delay: {
initial: 300,
max: Infinity,
jitter: true,
},
attempts: {
max: 5,
retryIf: (error, _operation) => !!error,
},
});
module.exports = {
plugins: [
{
resolve: "gatsby-source-graphql",
options: {
typeName: "GitHub",
fieldName: "github",
url:"https://api.mysite.com/graphql",
// Create Apollo Link manually. Can return a Promise.
createLink: (pluginOptions) => {
return ApolloLink.from([retryLink, createHttpLink(pluginOptions)]);
},
},
},
],
};
@gdhardy1
Copy link
Author

This should be done in gatsby-config.js. Must install @apollo/client using your package manager.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment