Easy setup for retrying rate limited request in Shopify's (Admin) GraphQL API.
For npm:
npm i apollo-link-shopify-retry
For yarn:
yarn add apollo-link-shopify-retry
When creating your client, inject this link
as a middleware:
// client.js
import {
ApolloClient,
HttpLink,
ApolloLink,
InMemoryCache,
} from "@apollo/client";
import { createShopifyRetryLink } from "apollo-link-shopify-retry";
export const createClient = (shop, accessToken) => {
const endpoint = new HttpLink({
uri: `https://${shop}/admin/api/2021-04/graphql.json`,
});
const rateLimit = createShopifyRetryLink();
const link = ApolloLink.from([
rateLimit,
endpoint,
]);
return new ApolloClient({
cache: new InMemoryCache(),
link,
});
};
By default, we log the throttles to console.log. You can disable this by passing { debug: false }
to createShopifyRetryLink
.
It utilizes @apollo/client/link/error
. This link checks how much cost needs to be restored in order to perform a GraphQL operation, allows you to wait until the operation can succeed. If the operation exceeds the maximum allowed cost of your Shopify API, the error will still be raised.