I have added a RetryLink
to my Apollo Client
in the following way:
const retryLink = new RetryLink({
attempts: (count, _, error) => {
return count <= 5 && !!error;
},
delay: (count, operation) => {
const {response: { headers }} = operation.getContext();
// headers is always empty
return count * 1000 * Math.random();
}
});
The objective is to adjust the delay by using a custom header sent by the server (which has rate limiting). However, the headers field in the response always remains empty. The response itself looks like this:
{
body: (...)
bodyUsed: true
headers: Headers {}
ok: false
redirected: false
status: 429
statusText: ""
type: "cors"
url: "https://graphql.endpoint.co/"
}
If I switch to another link, the header is present as expected. This alternative approach works:
const afterwareLink = new ApolloLink((operation, forward) => {
return forward(operation).map(response => {
const context = operation.getContext();
const {
response: { headers }
} = context;
if (headers) {
console.log(headers.get('Retry-After'));
}
return response;
});
});
To provide an overview, here is how the client setup looks like:
const createApolloClient = () => {
return new ApolloClient({
ssrMode: typeof window === 'undefined',
link: from([retryLink, authLink.concat(httpLink)]),
cache: new InMemoryCache()
});
};
Is there a way to access the response headers within a RetryLink
? Any insights would be appreciated.