@urql/exchange-context
TypeScript icon, indicating that this package has built-in type declarations

0.3.0 • Public • Published

@urql/exchange-context

An exchange for setting operation context in urql

@urql/exchange-context is an exchange for the urql GraphQL client which can set the operation context both synchronously as well as asynchronously

Quick Start Guide

First install @urql/exchange-context alongside urql:

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

You'll then need to add the contextExchange, that this package exposes, to your urql Client, the positioning of this exchange depends on whether you set an async setter or not. If you set an async context-setter it's best placed after all the synchronous exchanges (in front of the fetchExchange).

import { createClient, cacheExchange, fetchExchange } from 'urql';
import { contextExchange } from '@urql/exchange-context';

const client = createClient({
  url: 'http://localhost:1234/graphql',
  exchanges: [
    cacheExchange,
    contextExchange({
      getContext: async operation => {
        const token = await getToken();
        return { ...operation.context, headers: { authorization: token } };
      },
    }),
    fetchExchange,
  ],
});

Package Sidebar

Install

npm i @urql/exchange-context

Weekly Downloads

3,206

Version

0.3.0

License

MIT

Unpacked Size

26.2 kB

Total Files

13

Last publish

Collaborators

  • philpl
  • andyrichardson
  • formidable-owner
  • jdecroock
  • parkerziegler
  • npm-urql