Skip to content

Instantly share code, notes, and snippets.

@tkssharma
Created September 11, 2019 06:30
Show Gist options
  • Save tkssharma/e09b885740f0c44c6ec92f38ab6f71c8 to your computer and use it in GitHub Desktop.
Save tkssharma/e09b885740f0c44c6ec92f38ab6f71c8 to your computer and use it in GitHub Desktop.
import { ApolloClient } from 'apollo-client';
import { HttpLink } from 'apollo-link-http';
import { ApolloLink, concat } from 'apollo-link';
const httpLink = new HttpLink({ uri: '/graphql' });
// registering middlewares [authMiddleware, otherMiddleware ]
const authMiddleware = new ApolloLink((operation, forward) => {
// add the authorization to the headers
operation.setContext({
headers: {
authorization: localStorage.getItem('token') || null,
}
});
return forward(operation);
})
const otherMiddleware = new ApolloLink((operation, forward) => {
// add the recent-activity custom header to the headers
operation.setContext(({ headers = {} }) => ({
headers: {
...headers,
'recent-activity': localStorage.getItem('lastOnlineTime') || null,
}
}));
return forward(operation);
})
const cache = new InMemoryCache();
const client = new ApolloClient({
link: from([
authMiddleware,
otherMiddleware,
httpLink
]),
cache
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment