RetryLink
Attempt an operation multiple times if it fails due to network or server errors.
RetryLink is a non-terminating link that attempts to retry operations that
fail due to network errors. It enables resilient GraphQL operations by
automatically retrying failed requests with configurable delay and retry
strategies.
RetryLink is particularly useful for handling unreliable network conditions
where you would rather wait longer than explicitly fail an operation. It
provides exponential backoff and jitters delays between attempts by default.
ErrorLink to retry an operation after a GraphQL error. For more
information, see the Error handling documentation.1 import { RetryLink } from "@apollo/client/link/retry";
2
3 const link = new RetryLink();Constructor signature
1constructor(
2 options?: RetryLink.Options
3): RetryLinkAvoiding thundering herd
Starting with initialDelay, the delay of each subsequent retry is increased exponentially, meaning it's multiplied by 2 each time. For example, if initialDelay is 100, additional retries will occur after delays of 200, 400, 800, etc.
With the jitter option enabled, delays are randomized anywhere between 0ms (instant), and 2x the configured delay. This way you get the same result on average, but with random delays.
These two features are combined to help alleviate the thundering herd problem, by distributing load during major outages. Without these strategies, when your server comes back up it will be hit by all of your clients at once, possibly causing it to go down again.
Custom strategies
Instead of the options object, you may pass a function for delay and/or attempts, which implement custom strategies for each. In both cases the function is given the same arguments (attempt, operation, error).
The attempts function should return a boolean (or a Promise which resolves to a boolean) indicating whether the response should be retried. If yes, the delay function is then called, and should return the number of milliseconds to delay by.
1import { RetryLink } from "@apollo/client/link/retry";
2
3const link = new RetryLink({
4 attempts: (attempt, operation, error) => {
5 return !!error && operation.operationName != "specialCase";
6 },
7 delay: (attempt, operation, error) => {
8 return attempt * 1000 * Math.random();
9 },
10});Types
A function used to determine whether to retry the current operation.
Signature
1AttemptsFunction(
2 attempt: number,
3 operation: ApolloLink.Operation,
4 error: ErrorLike
5): boolean | Promise<boolean>Parameters
The current attempt number
The current ApolloLink.Operation for the request
Show/hide child attributes
ApolloClientThe Apollo Client instance executing the request.
Record<string, any>A map that stores extensions data to be sent to the server.
() => Readonly<ApolloLink.OperationContext>A function that gets the current context of the request. This can be used by links to determine which actions to perform. See managing context
string | undefinedThe string name of the GraphQL operation. If it is anonymous,
operationName will be undefined.
OperationTypeNodeThe type of the GraphQL operation, such as query or mutation.
DocumentNodeA DocumentNode that describes the operation taking place.
{
(context: Partial<ApolloLink.OperationContext>): void;
(updateContext: (previousContext: Readonly<ApolloLink.OperationContext>) => Partial<ApolloLink.OperationContext>): void;
}A function that takes either a new context object, or a function which takes in the previous context and returns a new one. See managing context.
OperationVariablesA map of GraphQL variables being sent with the operation.
The error that triggered the retry attempt
Configuration options for the standard retry attempt strategy.
numberThe max number of times to try a single operation before giving up.
Note that this INCLUDES the initial request as part of the count.
E.g. max of 1 indicates no retrying should occur.
Pass Infinity for infinite retries.
(error: ErrorLike, operation: ApolloLink.Operation) => boolean | Promise<boolean>Predicate function that determines whether a particular error should trigger a retry.
For example, you may want to not retry 4xx class HTTP errors.
A function used to determine the delay for a retry attempt.
Signature
1DelayFunction(
2 attempt: number,
3 operation: ApolloLink.Operation,
4 error: ErrorLike
5): numberParameters
The current attempt number
The current ApolloLink.Operation for the request
Show/hide child attributes
ApolloClientThe Apollo Client instance executing the request.
Record<string, any>A map that stores extensions data to be sent to the server.
() => Readonly<ApolloLink.OperationContext>A function that gets the current context of the request. This can be used by links to determine which actions to perform. See managing context
string | undefinedThe string name of the GraphQL operation. If it is anonymous,
operationName will be undefined.
OperationTypeNodeThe type of the GraphQL operation, such as query or mutation.
DocumentNodeA DocumentNode that describes the operation taking place.
{
(context: Partial<ApolloLink.OperationContext>): void;
(updateContext: (previousContext: Readonly<ApolloLink.OperationContext>) => Partial<ApolloLink.OperationContext>): void;
}A function that takes either a new context object, or a function which takes in the previous context and returns a new one. See managing context.
OperationVariablesA map of GraphQL variables being sent with the operation.
The error that triggered the retry attempt
Configuration options for the standard retry delay strategy.
numberThe number of milliseconds to wait before attempting the first retry.
Delays will increase exponentially for each attempt. E.g. if this is set to 100, subsequent retries will be delayed by 200, 400, 800, etc, until they reach the maximum delay.
Note that if jittering is enabled, this is the average delay.
booleanWhether delays between attempts should be randomized.
This helps avoid thundering herd type situations by better distributing load during major outages. Without these strategies, when your server comes back up it will be hit by all of your clients at once, possibly causing it to go down again.
numberThe maximum number of milliseconds that the link should wait for any retry.
Options provided to the RetryLink constructor.
RetryLink.AttemptsOptions | RetryLink.AttemptsFunctionConfiguration for the retry strategy to use, or a custom retry strategy.
RetryLink.DelayOptions | RetryLink.DelayFunctionConfiguration for the delay strategy to use, or a custom delay strategy.