···11+# @urql/exchange-retry (Exchange factory)
22+33+`@urql/exchange-retry` is an exchange for the [`urql`](../../README.md) GraphQL client that allows operations (queries, mutations, subscriptions) to be retried based on an `options` parameter.
44+55+The `retryExchange` is of type `Options => Exchange`.
66+77+It periodically retries requests that fail due to network errors. It accepts five optional options:
88+99+- `initialDelayMs` - the minimum delay between retries, defaults to 1000
1010+- `maxDelayMs` - the maximum delay that can be applied to an operation, defaults to 15000
1111+- `randomDelay` - whether to apply a random delay to protect against thundering herd, defaults to true
1212+- `maxNumberAttempts` - the maximum number of attempts to retry any given operation, defaults to Infinity
1313+- `retryIf` - optional function to apply to errors to determine whether they should be retried
1414+1515+The `retryExchange` will exponentially increase the delay from `minDelayMs` up to `maxDelayMs` with some random jitter added to avoid the [thundering herd problem](https://en.wikipedia.org/wiki/Thundering_herd_problem).
1616+1717+## Quick Start Guide
1818+1919+First install `@urql/exchange-retry` alongside `urql`:
2020+2121+```sh
2222+yarn add @urql/exchange-retry
2323+# or
2424+npm install --save @urql/exchange-retry
2525+```
2626+2727+You'll then need to add the `retryExchange`, that this package exposes, to your
2828+`urql` Client:
2929+3030+```js
3131+import { createClient, dedupExchange, cacheExchange, fetchExchange } from 'urql';
3232+import { retryExchange } from '@urql/exchange-retry';
3333+3434+const options = {
3535+ initialDelayMs: 50,
3636+ maxDelayMs: 500,
3737+ randomDelay: true,
3838+ maxNumberAttempts: 10,
3939+ retryIf: err => err.message === '[GraphQL] Error Message A',
4040+};
4141+4242+const client = createClient({
4343+ url: 'http://localhost:1234/graphql',
4444+ exchanges: [
4545+ dedupExchange,
4646+ cacheExchange,
4747+ fetchExchange,
4848+ retryExchange(options), // Use the retryExchange factory to add a new exchange
4949+ ],
5050+});
5151+```
5252+5353+You'll likely want to place the `retryExchange` after the `fetchExchange` so that retries are only performed _after_ the operation has passed through the cache and has attempted to fetch.
5454+5555+## Usage
5656+5757+After installing `@urql/exchange-retry` and adding it to your `urql` client, `urql` will retry operations based on the options passed to the `retryExchange`.
5858+5959+```js
6060+import React from 'react';
6161+import { useQuery } from 'urql';
6262+6363+const LoadingIndicator = () => <h1>Loading...</h1>;
6464+6565+const YourContent = () => {
6666+ const [{ fetching, data }] = useQuery({ query: allPostsQuery });
6767+ // Unlike a normal query, if the first request resulted in an error,
6868+ // the query will be automatically retried based on the `retryExchange` options
6969+ return !fetching && <div>{data}</div>;
7070+};
7171+7272+return <YourContent />;
7373+```
7474+7575+<!-- TODO?: Add code sandbox demo -->