What is @urql/exchange-retry?
@urql/exchange-retry is an npm package that provides retry logic for GraphQL requests made using the urql client. It allows developers to specify retry strategies for failed requests, improving the resilience and reliability of their applications.
What are @urql/exchange-retry's main functionalities?
Basic Retry Logic
This feature allows you to add basic retry logic to your urql client. The retryExchange is added to the list of exchanges, enabling automatic retries for failed requests.
const { retryExchange } = require('@urql/exchange-retry');
const { createClient, dedupExchange, cacheExchange, fetchExchange } = require('urql');
const client = createClient({
url: 'https://my-graphql-endpoint.com/graphql',
exchanges: [dedupExchange, cacheExchange, retryExchange(), fetchExchange],
});
Custom Retry Logic
This feature allows you to customize the retry logic by specifying parameters such as initial delay, maximum delay, random delay, maximum number of attempts, and conditions for retrying.
const { retryExchange } = require('@urql/exchange-retry');
const { createClient, dedupExchange, cacheExchange, fetchExchange } = require('urql');
const retryConfig = {
initialDelayMs: 1000,
maxDelayMs: 15000,
randomDelay: true,
maxNumberAttempts: 5,
retryIf: (error) => error.networkError,
};
const client = createClient({
url: 'https://my-graphql-endpoint.com/graphql',
exchanges: [dedupExchange, cacheExchange, retryExchange(retryConfig), fetchExchange],
});
Exponential Backoff
This feature allows you to implement exponential backoff for retries. The backoff function calculates the delay before the next retry attempt, increasing exponentially with each attempt.
const { retryExchange } = require('@urql/exchange-retry');
const { createClient, dedupExchange, cacheExchange, fetchExchange } = require('urql');
const retryConfig = {
initialDelayMs: 500,
maxDelayMs: 10000,
randomDelay: true,
maxNumberAttempts: 3,
retryIf: (error) => error.networkError,
backoff: (attempt) => Math.min(1000 * 2 ** attempt, 30000),
};
const client = createClient({
url: 'https://my-graphql-endpoint.com/graphql',
exchanges: [dedupExchange, cacheExchange, retryExchange(retryConfig), fetchExchange],
});
Other packages similar to @urql/exchange-retry
axios-retry
axios-retry is a library that adds retry functionality to axios, a popular HTTP client. It allows you to specify retry strategies for failed HTTP requests. Compared to @urql/exchange-retry, axios-retry is more general-purpose and can be used with any HTTP requests, not just GraphQL.
fetch-retry
fetch-retry is a library that adds retry logic to the Fetch API. It allows you to specify retry strategies for failed fetch requests. Similar to axios-retry, fetch-retry is more general-purpose and can be used with any HTTP requests, whereas @urql/exchange-retry is specifically designed for urql and GraphQL.
@urql/exchange-retry (Exchange factory)
@urql/exchange-retry
is an exchange for the urql
GraphQL client that allows operations (queries, mutations, subscriptions) to be retried based on an options
parameter.
Quick Start Guide
First install @urql/exchange-retry
alongside urql
:
yarn add @urql/exchange-retry
npm install --save @urql/exchange-retry
Read more about the retry exchange.