Mirror: The highly customizable and versatile GraphQL client with which you add on features like normalized caching as you grow.

title: '@urql/exchange-request-policy' order: 9#

Request Policy Exchange#

Note: These API docs are deprecated as we now keep TSDocs in all published packages. You can view TSDocs while using these packages in your editor, as long as it supports the TypeScript Language Server. We're planning to replace these API docs with a separate web app soon.

The @urql/exchange-request-policy package contains an addon requestPolicyExchange for urql that may be used to upgrade Operations' Request Policies on a time-to-live basis.

Read more about request policies on the "Document Caching" page.

This exchange will conditionally upgrade cache-first and cache-only operations to use cache-and-network, so that the client gets an opportunity to update its cached data, when the operation hasn't been seen within the given ttl time. This is often preferable to setting the default policy to cache-and-network to avoid an unnecessarily high amount of requests to be sent to the API when switching pages.

Installation and Setup#

First install @urql/exchange-request-policy alongside urql:

yarn add @urql/exchange-request-policy
# or
npm install --save @urql/exchange-request-policy

Then add it to your Client, preferably in front of the cacheExchange and in front of any asynchronous exchanges, like the fetchExchange:

import { createClient, cacheExchange, fetchExchange } from 'urql';
import { requestPolicyExchange } from '@urql/exchange-request-policy';

const client = createClient({
  url: 'http://localhost:3000/graphql',
  exchanges: [
    requestPolicyExchange({
      /* config */
    }),
    cacheExchange,
    fetchExchange,
  ],
});

Options#

Option Description
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.
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.