Socket
Socket
Sign inDemoInstall

@urql/exchange-persisted-fetch

Package Overview
Dependencies
4
Maintainers
24
Versions
35
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    @urql/exchange-persisted-fetch

An exchange that allows for persisted queries support when fetching queries


Version published
Weekly downloads
2.7K
decreased by-48.49%
Maintainers
24
Install size
1.40 MB
Created
Weekly downloads
 

Readme

Source

@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-fetch

You'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: Use GET for fetches with persisted queries
  • enforcePersistedQueries: 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 the window.crypto API in the browser and the crypto module 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,
  ],
});

Keywords

FAQs

Last updated on 14 Feb 2023

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc