1.0.20 • Published 4 years ago

apollo-link-context v1.0.20

Weekly downloads
968,534
License
MIT
Repository
github
Last release
4 years ago

title: apollo-link-context

description: Easily set a context on your operation, which is used by other links further down the chain.

The setContext function takes a function that returns either an object or a promise that returns an object to set the new context of a request.

It receives two arguments: the GraphQL request being executed, and the previous context. This link makes it easy to perform async look up of things like authentication tokens and more!

import { setContext } from "apollo-link-context";

const setAuthorizationLink = setContext((request, previousContext) => ({
  headers: {authorization: "1234"}
}));

const asyncAuthLink = setContext(
  request =>
    new Promise((success, fail) => {
      // do some async lookup here
      setTimeout(() => {
        success({ token: "async found token" });
      }, 10);
    })
);

Caching lookups

Typically async actions can be expensive and may not need to be called for every request, especially when a lot of request are happening at once. You can setup your own caching and invalidation outside of the link to make it faster but still flexible!

Take for example a user auth token being found, cached, then removed on a 401 response:

import { setContext } from "apollo-link-context";
import { onError } from "apollo-link-error";

// cached storage for the user token
let token;
const withToken = setContext(() => {
  // if you have a cached value, return it immediately
  if (token) return { token };

  return AsyncTokenLookup().then(userToken => {
    token = userToken;
    return { token };
  });
});

const resetToken = onError(({ networkError }) => {
  if (networkError && networkError.name ==='ServerError' && networkError.statusCode === 401) {
    // remove cached token on 401 from the server
    token = null;
  }
});

const authFlowLink = withToken.concat(resetToken);
@xdn/apollo@yelm/next-apollo@kikstart-graphql/client@kikstart-graphql/core@edgio/apollo@layer0/apollozwap-qrcode-frontendzwap_chatbot_guidezwap_chatbot_npm@axeedge/go-educator-utils@axeedge/go-student-utilspri_cnip_module_ine@productfy/common@gqlkit/client-toolscreative-gallery-shonpclassdoapp-campussingle-page-app@puzl/puzl-cloud_authorization-modulegatsby-source-prismic-graphql-forked@alyz.tech/alyz-sdk@alyz.tech/alyz_js_sdkirisvr-cloud-util@moonwalker/lifesupportconnie-gatsby-source-prismic-graphqlphoenix-shared@apraamcos/apra-amcos-uidecoupled-front-end@ifchor/common@engine2/efa-apollo@radar/deploy-sdkecp-frontendreact-auth-testing@platyplus/hasura-apollo-client@brudi/brudi-toolbox-graphql@conclurer/bricks@conclurer/edel-plugin-apollomarketing-sitetest-component-shbitcasino-v3apra-amcos-ui@gomoko/admin-uisdk-saleor-quammbreeze-sdk@citydna/datocmspe-layouts@wpshopify/api@echidna/uichimp.clickgraphql-api-gatewaygatsby-source-modular-graphqlpepsico_sdk@everything-registry/sub-chunk-1153ebr-componentsedge-core@base86inc/apollo-clientgh-forkstatshasura-data-enginegraphql-service-bindinggraphql-schema-stitchgraphql-stitchgraphql-gateway-apigraphqlqbgenie-pwa-todo-examplegridsome-source-craftqlgridsome-source-graphqlgridsome-source-graphql-prismicgridsome-source-graphql-with-logsreact-dinamic-contentgraphcurlgt-gatsby-wagtailetherfacefork1-polymathnetwork-polymesh-sdkgazamsbaseappgatsby-theme-consumer-sitegatsby-starter-procyongatsby-plugin-apollo-shopifygatsby-source-prismic-graphqlgatsby-source-prismic-graphql-tweakedgatsby-source-prismic-graphql-withlovegatsby-source-wagtail-masterhelyosjs-sdkhtv-sdkfervorgfh-clientgh-shipitfosinventory-frontendhanstoolsheartreachaisi-cli-plugin-apollo@boxcc/aws-appsync@asany/umi-plugin-apollo@akheel-s/developer-adk-interact@akiolabs/app@anaphasejs/create-apollo-clientscada-comsaleor-sdk@allganize/sdk@birdeatsbug/vue-cli-plugin-apollojson-vue-core
1.0.20

4 years ago

2.0.0-beta.0

4 years ago

1.0.19

5 years ago

1.0.18

5 years ago

1.0.17

5 years ago

1.0.16

5 years ago

1.0.15

5 years ago

1.0.14

5 years ago

1.0.13

5 years ago

1.0.12

5 years ago

1.0.11

5 years ago

1.0.10

5 years ago

1.0.9

6 years ago

1.0.8

6 years ago

1.0.7

6 years ago

1.0.6

6 years ago

1.0.5

6 years ago

1.0.3

6 years ago

1.0.2

6 years ago

1.0.1

6 years ago

1.0.0

7 years ago

0.1.0

7 years ago