1.0.20 • Published 5 years ago

apollo-link-context v1.0.20

Weekly downloads
968,534
License
MIT
Repository
github
Last release
5 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@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-1153temporary-midas-packagewith-apollowallet-mandalawalkuerewalkuere-corewalkuere-rnweb-backoffice-newvue-neo4jvue-cli-plugin-apollovue-cli-plugin-apollo-graphql-wsvue-cli-plugin-apollo-without-regenerator-runtimewebstudio-muixcim-codesandboxws-flare-cliteddly-sdksportsbet-apollosportsbet-promotions-appsportsbet-user-appstorm-system-componentstesthelyos-sdktesthelyosjs-sdktest-trackingthemis-graphqlvenia-concept-spfoosverdaccio-apm-servervicads5-liveview@doingitright/gatsby-source-prismic-graphql@donatas123/client@donatas123/front@dphunkt/mnstr-client@dosvit/api-gateway@dosvit/sdk@digitaliseringsbyran/gatsby-source-prismic-graphql@dne/apollo-client@dockite/admin@drural/sdk@clutch-marketplace/apollo@cobuildlab/8base-chat@crawlo/graphql-client@crds_npm/rollup-plugin-content-blocks@cumulus-consulting/mfgx-io-api-client@cooper-app/sdk@cooperjs/sdk@dev.smartbox/smartui@demandcluster/reaction-channel-manager@draftbit/auth-schema-link@peak-stone/vue-admin@rukmanamreza/apollo-datasource-graphql
1.0.20

5 years ago

2.0.0-beta.0

6 years ago

1.0.19

6 years ago

1.0.18

6 years ago

1.0.17

6 years ago

1.0.16

6 years ago

1.0.15

6 years ago

1.0.14

6 years ago

1.0.13

6 years ago

1.0.12

6 years ago

1.0.11

7 years ago

1.0.10

7 years ago

1.0.9

7 years ago

1.0.8

7 years ago

1.0.7

7 years ago

1.0.6

7 years ago

1.0.5

7 years ago

1.0.3

8 years ago

1.0.2

8 years ago

1.0.1

8 years ago

1.0.0

8 years ago

0.1.0

8 years ago