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!
; const setAuthorizationLink = ; const asyncAuthLink = ;
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:
;; // cached storage for the user tokenlet token;const withToken = ; const resetToken = ; const authFlowLink = withToken;