Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

apollo-link-context

Package Overview
Dependencies
Maintainers
4
Versions
22
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

apollo-link-context

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

  • 1.0.20
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
301K
decreased by-15.7%
Maintainers
4
Weekly downloads
 
Created

What is apollo-link-context?

The apollo-link-context package is used to set context for Apollo Client operations. It allows you to dynamically set headers, authentication tokens, and other context values for each GraphQL request.

What are apollo-link-context's main functionalities?

Setting Headers

This feature allows you to set headers for each request dynamically. In this example, an authorization token is retrieved from local storage and added to the headers of each request.

const { setContext } = require('apollo-link-context');
const { ApolloClient, InMemoryCache, HttpLink } = require('@apollo/client');

const httpLink = new HttpLink({ uri: 'https://example.com/graphql' });

const authLink = setContext((_, { headers }) => {
  const token = localStorage.getItem('token');
  return {
    headers: {
      ...headers,
      authorization: token ? `Bearer ${token}` : '',
    }
  };
});

const client = new ApolloClient({
  link: authLink.concat(httpLink),
  cache: new InMemoryCache()
});

Setting Context Values

This feature allows you to set custom context values for each request. In this example, a custom header and a custom context value are added to each request.

const { setContext } = require('apollo-link-context');
const { ApolloClient, InMemoryCache, HttpLink } = require('@apollo/client');

const httpLink = new HttpLink({ uri: 'https://example.com/graphql' });

const contextLink = setContext((_, { headers }) => {
  return {
    headers: {
      ...headers,
      'x-custom-header': 'custom-value'
    },
    customValue: 'myCustomValue'
  };
});

const client = new ApolloClient({
  link: contextLink.concat(httpLink),
  cache: new InMemoryCache()
});

Other packages similar to apollo-link-context

FAQs

Package last updated on 09 Apr 2020

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc