Mirror: The highly customizable and versatile GraphQL client with which you add on features like normalized caching as you grow.
1# @urql/exchange-request-policy (Exchange factory)
2
3`@urql/exchange-request-policy` is an exchange for the [`urql`](../../README.md) GraphQL client that will automatically upgrade operation request-policies
4on a time-to-live basis.
5
6## Quick Start Guide
7
8First install `@urql/exchange-request-policy` alongside `urql`:
9
10```sh
11yarn add @urql/exchange-request-policy
12# or
13npm install --save @urql/exchange-request-policy
14```
15
16Then add it to your client.
17
18```js
19import { createClient, cacheExchange, fetchExchange } from 'urql';
20import { requestPolicyExchange } from '@urql/exchange-request-policy';
21
22const client = createClient({
23 url: 'http://localhost:1234/graphql',
24 exchanges: [
25 requestPolicyExchange({
26 // The amount of time in ms that has to go by before upgrading, default is 5 minutes.
27 ttl: 60 * 1000, // 1 minute.
28 // An optional function that allows you to specify whether an operation should be upgraded.
29 shouldUpgrade: operation => operation.context.requestPolicy !== 'cache-only',
30 }),
31 cacheExchange,
32 fetchExchange,
33 ],
34});
35```
36
37Now when the exchange sees a `cache-first` operation that hasn't been seen in ttl amount of time it will upgrade
38the `requestPolicy` to `cache-and-network`.