# apollo-link-context

> An easy way to set and cache context changes for Apollo Link

Latest version **1.0.20** (published 2020-04-09) · MIT license · 0 weekly downloads

## Install

```sh
npm install apollo-link-context
pnpm add apollo-link-context
yarn add apollo-link-context
bun add apollo-link-context
```

## Health

**Score 30/100 (F)** — status: abandoned.

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.20 |
| Published | 2020-04-09 |
| First published | 2017-10-21 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 2 |
| Unpacked size | 17.5 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 1423 |
| Author | James Baxley |
| Maintainers | apollo-bot, evanshauser, jbaxleyiii, peggyrayzis |

## Links

- npm: https://www.npmjs.com/package/apollo-link-context
- Repository: https://github.com/apollographql/apollo-link
- Homepage: https://github.com/apollographql/apollo-link#readme
- Issues: https://github.com/apollographql/apollo-link/issues
- npm.io page: https://npm.io/package/apollo-link-context

## Dependencies (2)

- [tslib](https://npm.io/package/tslib.md) ^1.9.3
- [apollo-link](https://npm.io/package/apollo-link.md) ^1.2.14

## Recent versions

- 1.0.20 (latest) — 2020-04-09
- 2.0.0-beta.0 (beta) — 2019-11-15
- 1.0.19 — 2019-09-06
- 1.0.18 — 2019-06-14
- 1.0.17 — 2019-03-14
- 1.0.16 — 2019-03-14
- 1.0.15 — 2019-03-05
- 1.0.14 — 2019-02-01
- 1.0.13 — 2019-02-01
- 1.0.12 — 2018-12-15
- 1.0.11 — 2018-12-13
- 1.0.10 — 2018-11-21
- 1.0.9 — 2018-09-15
- 1.0.8 — 2018-04-12
- 1.0.7 — 2018-02-23
- … 7 more at https://npm.io/package/apollo-link-context/versions

## README

---
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!

```js
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:

```js
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);
```

---
_Source: https://npm.io/package/apollo-link-context · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
