Created
September 11, 2019 06:30
-
-
Save tkssharma/e09b885740f0c44c6ec92f38ab6f71c8 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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