Registry / api / apollo-link-context

apollo-link-context

JSON →
library1.0.20jsnpmunverified

Utility for setting context on Apollo Client GraphQL operations, enabling async lookups like authentication tokens. Current stable version is 1.0.20, with low release cadence (last update years ago). Part of the apollo-link chain ecosystem, it provides a simple imperative setContext function that returns either an object or a promise, allowing caching and conditional logic based on previous context. Key differentiator: handles asynchronous context preparation natively, unlike static context options.

npm install apollo-link-context
INSTALL
IMPORT
SIG · APOLLO-LINK-CONTEX
A
apollo-link-context
apijavascriptv1.0.20
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

setContext
import { setContext } from 'apollo-link-context'
const setContext = require('apollo-link-context').setContext
Default or named import works in ESM environments. CJS users must use destructured require.
setContext
const { setContext } = require('apollo-link-context')
const setContext = require('apollo-link-context')
Requiring the module directly without destructuring yields an object, not the function.
setContext
import { setContext } from 'apollo-link-context'
import setContext from 'apollo-link-context'
Library uses named export, not default export.

Creates an Apollo Link that sets an authorization header on every GraphQL request, preserving existing headers.

import { setContext } from 'apollo-link-context'; import { ApolloLink } from 'apollo-link'; import { createHttpLink } from 'apollo-link-http'; const authLink = setContext((_, { headers }) => ({ headers: { ...headers, authorization: process.env.AUTH_TOKEN || '' } })); const httpLink = createHttpLink({ uri: '/graphql' }); const link = authLink.concat(httpLink); // Use with Apollo Client: new ApolloClient({ link, cache: new InMemoryCache() })
Debug
Known issues
deprecatedapollo-link-context is part of the legacy @apollo/client link system; newer Apollo Client (v3+) uses @apollo/client/link/context
fix
Upgrade to apollo-link-context@1.0.0+ or use @apollo/client/link/context for Apollo Client v3+.
affects: <1.0.0
gotchasetContext does not merge deeply; returning only partial context may override other links' context parts.
fix
Explicitly spread previousContext inside the setContext function to preserve fields.
affects: >=1.0.0
gotchaPromise returned from setContext must resolve to an object; resolving to null/undefined silences context updates.
fix
Always return an object or a promise that resolves to an object from the setContext callback.
affects: >=1.0.0
deprecatedThe package has been deprecated in favor of @apollo/client/link/context
fix
Migrate to Apollo Client v3's internal context link: import { setContext } from '@apollo/client/link/context';
affects: >=1.0.0
gotchasetContext does not accept a static object; it must be a function that returns the context object.
fix
Wrap your static object in an arrow function: setContext(() => ({ token: 'abc' }))
affects: >=1.0.0
Errors
Common errors & fixes
TypeError: setContext is not a function
Using default import instead of named import
fix
Use import { setContext } from 'apollo-link-context';
Cannot find module 'apollo-link-context'
Package not installed or wrong import path
fix
Run npm install apollo-link-context or use the correct import from @apollo/client/link/context in Apollo Client v3+.
Uncaught (in promise) TypeError: Cannot destructure property 'headers' of 'previousContext' as it is undefined.
setContext callback called without previousContext when used as the first link in chain
fix
Provide default value for previousContext parameter: (_, { headers = {} } = {}) => ({ headers: { ...headers, Authorization: 'token' } })
ApolloError: Response not successful: Received status code 401
setContext fails to update headers due to async token resolution error
fix
Ensure the promise resolves correctly and catch errors: setContext(() => fetchToken().catch(() => ({})))
Upgrade
Version history
1.0.20latest on npm
Audit
Dependencies
apollo-linkrequiredCore link interface required for serialization and composition with other links.
graphql-tagoptionalOften used alongside for query parsing, but not a strict runtime dependency.
Agent activity
43 hits · last 30 days
node
34
OpenAI (training)
1
Resources
apollo-link-context — npm install apollo-link-context · libregistry