Mirror: The highly customizable and versatile GraphQL client with which you add on features like normalized caching as you grow.
1--- 2title: '@urql/exchange-request-policy' 3order: 9 4--- 5 6# Request Policy Exchange 7 8> **Note:** These API docs are deprecated as we now keep TSDocs in all published packages. 9> You can view TSDocs while using these packages in your editor, as long as it supports the 10> TypeScript Language Server. 11> We're planning to replace these API docs with a separate web app soon. 12 13The `@urql/exchange-request-policy` package contains an addon `requestPolicyExchange` for `urql` 14that may be used to upgrade [Operations' Request Policies](./core.md#requestpolicy) on a 15time-to-live basis. 16 17[Read more about request policies on the "Document Caching" page.](../basics/document-caching.md#request-policies) 18 19This exchange will conditionally upgrade `cache-first` and `cache-only` operations to use 20`cache-and-network`, so that the client gets an opportunity to update its cached data, when the 21operation hasn't been seen within the given `ttl` time. This is often preferable to setting the 22default policy to `cache-and-network` to avoid an unnecessarily high amount of requests to be sent 23to the API when switching pages. 24 25## Installation and Setup 26 27First install `@urql/exchange-request-policy` alongside `urql`: 28 29```sh 30yarn add @urql/exchange-request-policy 31# or 32npm install --save @urql/exchange-request-policy 33``` 34 35Then add it to your `Client`, preferably in front of the `cacheExchange` and in front of any asynchronous 36exchanges, like the `fetchExchange`: 37 38```js 39import { createClient, cacheExchange, fetchExchange } from 'urql'; 40import { requestPolicyExchange } from '@urql/exchange-request-policy'; 41 42const client = createClient({ 43 url: 'http://localhost:3000/graphql', 44 exchanges: [ 45 requestPolicyExchange({ 46 /* config */ 47 }), 48 cacheExchange, 49 fetchExchange, 50 ], 51}); 52``` 53 54## Options 55 56| Option | Description | 57| --------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | 58| `ttl` | The "time-to-live" until an `Operation` will be upgraded to the `cache-and-network` policy in milliseconds. By default 5 minutes is set. | 59| `shouldUpgrade` | An optional function that receives an `Operation` as the only argument and may return `true` or `false` depending on whether an operation should be upgraded. This can be used to filter out operations that should never be upgraded to `cache-and-network`. |