{"id":44278,"library":"apollo-link-context","title":"Apollo Link Context","description":"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.","status":"maintenance","version":"1.0.20","language":"javascript","source_language":"en","source_url":"https://github.com/apollographql/apollo-link","tags":["javascript","typescript"],"install":[{"cmd":"npm install apollo-link-context","lang":"bash","label":"npm"},{"cmd":"yarn add apollo-link-context","lang":"bash","label":"yarn"},{"cmd":"pnpm add apollo-link-context","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"Core link interface required for serialization and composition with other links.","package":"apollo-link","optional":false},{"reason":"Often used alongside for query parsing, but not a strict runtime dependency.","package":"graphql-tag","optional":true}],"imports":[{"note":"Default or named import works in ESM environments. CJS users must use destructured require.","wrong":"const setContext = require('apollo-link-context').setContext","symbol":"setContext","correct":"import { setContext } from 'apollo-link-context'"},{"note":"Requiring the module directly without destructuring yields an object, not the function.","wrong":"const setContext = require('apollo-link-context')","symbol":"setContext","correct":"const { setContext } = require('apollo-link-context')"},{"note":"Library uses named export, not default export.","wrong":"import setContext from 'apollo-link-context'","symbol":"setContext","correct":"import { setContext } from 'apollo-link-context'"}],"quickstart":{"code":"import { setContext } from 'apollo-link-context';\nimport { ApolloLink } from 'apollo-link';\nimport { createHttpLink } from 'apollo-link-http';\n\nconst authLink = setContext((_, { headers }) => ({\n  headers: {\n    ...headers,\n    authorization: process.env.AUTH_TOKEN || ''\n  }\n}));\n\nconst httpLink = createHttpLink({ uri: '/graphql' });\n\nconst link = authLink.concat(httpLink);\n// Use with Apollo Client: new ApolloClient({ link, cache: new InMemoryCache() })","lang":"typescript","description":"Creates an Apollo Link that sets an authorization header on every GraphQL request, preserving existing headers."},"warnings":[{"fix":"Upgrade to apollo-link-context@1.0.0+ or use @apollo/client/link/context for Apollo Client v3+.","message":"apollo-link-context is part of the legacy @apollo/client link system; newer Apollo Client (v3+) uses @apollo/client/link/context","severity":"deprecated","affected_versions":"<1.0.0"},{"fix":"Explicitly spread previousContext inside the setContext function to preserve fields.","message":"setContext does not merge deeply; returning only partial context may override other links' context parts.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Always return an object or a promise that resolves to an object from the setContext callback.","message":"Promise returned from setContext must resolve to an object; resolving to null/undefined silences context updates.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Migrate to Apollo Client v3's internal context link: import { setContext } from '@apollo/client/link/context';","message":"The package has been deprecated in favor of @apollo/client/link/context","severity":"deprecated","affected_versions":">=1.0.0"},{"fix":"Wrap your static object in an arrow function: setContext(() => ({ token: 'abc' }))","message":"setContext does not accept a static object; it must be a function that returns the context object.","severity":"gotcha","affected_versions":">=1.0.0"}],"env_vars":null,"search_vec":"'1.0.20':23 'ago':31 'allow':55 'apollo':1,9,36 'apollo-link':35 'async':14 'asynchron':67 'authent':17 'base':60 'cach':56 'cadenc':27 'chain':38 'client':10 'condit':58 'context':3,7,63,68,73 'current':19 'differenti':65 'ecosystem':39 'either':49 'enabl':13 'function':46 'graphql':11 'handl':66 'imper':44 'javascript':75 'key':64 'last':28 'like':16 'link':2,37 'logic':59 'lookup':15 'low':25 'nativ':70 'object':51 'oper':12 'option':74 'part':32 'prepar':69 'previous':62 'promis':54 'provid':41 'releas':26 'return':48 'set':6 'setcontext':45 'simpl':43 'stabl':20 'static':72 'token':18 'typescript':76 'unlik':71 'updat':29 'util':4 'version':21 'year':30","created_at":"2026-06-07T12:49:20.596811+00:00","updated_at":"2026-06-07T12:49:20.596811+00:00","problems":[{"fix":"Use import { setContext } from 'apollo-link-context';","cause":"Using default import instead of named import","error":"TypeError: setContext is not a function"},{"fix":"Run npm install apollo-link-context or use the correct import from @apollo/client/link/context in Apollo Client v3+.","cause":"Package not installed or wrong import path","error":"Cannot find module 'apollo-link-context'"},{"fix":"Provide default value for previousContext parameter: (_, { headers = {} } = {}) => ({ headers: { ...headers, Authorization: 'token' } })","cause":"setContext callback called without previousContext when used as the first link in chain","error":"Uncaught (in promise) TypeError: Cannot destructure property 'headers' of 'previousContext' as it is undefined."},{"fix":"Ensure the promise resolves correctly and catch errors: setContext(() => fetchToken().catch(() => ({})))","cause":"setContext fails to update headers due to async token resolution error","error":"ApolloError: Response not successful: Received status code 401"}],"ecosystem":"npm","meta_description":null,"install_score":null,"quickstart_score":null,"quickstart_tag":null,"pypi_latest":null,"cli_name":null,"cli_version":null,"type":"library","homepage":"https://github.com/apollographql/apollo-link#readme","github":"https://github.com/apollographql/apollo-link","docs":null,"changelog":null,"pypi":null,"npm":"apollo-link-context","openapi_spec":null,"status_page":null,"smithery":null,"categories":["api"],"base_url":null,"auth_type":null,"provenance":{"verified_status":null,"verified_at":null,"last_verified":"2026-06-07","next_check":"2026-09-05","install_tag":null}}