@urql/exchange-persisted-fetch v2.1.0
@urql/exchange-persisted-fetch
The persistedFetchExchange is an exchange that builds on the regular fetchExchange
but adds support for Persisted Queries.
Quick Start Guide
First install @urql/exchange-persisted-fetch alongside urql:
yarn add @urql/exchange-persisted-fetch
# or
npm install --save @urql/exchange-persisted-fetchYou'll then need to add the persistedFetchExchange method, that this package exposes,
to your exchanges.
import { createClient, dedupExchange, fetchExchange, cacheExchange } from 'urql';
import { persistedFetchExchange } from '@urql/exchange-persisted-fetch';
const client = createClient({
url: 'http://localhost:1234/graphql',
exchanges: [
dedupExchange,
cacheExchange,
persistedFetchExchange({
/* optional config */
}),
fetchExchange,
],
});The persistedQueryExchange supports three configuration options:
preferGetForPersistedQueries: UseGETfor fetches with persisted queriesenforcePersistedQueries: This disables automatic persisted queries and disables any retry logic for how the API responds to persisted queries. Instead it's assumed that they'll always succeed.generateHash: A function that takes a GraphQL query and returns the hashed result. This defaults to thewindow.cryptoAPI in the browser and thecryptomodule in node.
The persistedFetchExchange only handles queries, so for mutations we keep the
fetchExchange around alongside of it.
Avoid hashing during runtime
If you want to generate hashes at build-time you can use a webpack-loader to achieve this, when using this all you need to do in this exchange is the following:
import { createClient, dedupExchange, fetchExchange, cacheExchange } from 'urql';
import { persistedFetchExchange } from '@urql/exchange-persisted-fetch';
const client = createClient({
url: 'http://localhost:1234/graphql',
exchanges: [
dedupExchange,
cacheExchange,
persistedFetchExchange({
generateHash: (_, document) => document.documentId,
}),
fetchExchange,
],
});3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
4 years ago
4 years ago
4 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
6 years ago
6 years ago
6 years ago
6 years ago