Last active
February 5, 2018 20:44
-
-
Save maticzav/21f3e50d7dd66d91f94548dfca281aa5 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 fetch from 'isomorphic-fetch' | |
import { ApolloClient } from 'apollo-client' | |
import { HttpLink } from 'apollo-link-http' | |
import { InMemoryCache } from 'apollo-cache-inmemory' | |
import { from, ApolloLink } from 'apollo-link' | |
// WORKING ------------------------------------------------------------------- | |
const httpLink = new HttpLink({ | |
uri: process.env.GRAPHCOOL_ENDPOINT, | |
credentials: 'same-origin' | |
}) | |
const authLink = new ApolloLink((operation, forward) => { | |
const token = localStorage.getItem('graphcoolToken') | |
operation.setContext(({ headers = {} }) => ({ | |
headers: { | |
Authorization: token ? `Bearer ${token}` : null, | |
...headers | |
} | |
})) | |
return forward(operation) | |
}) | |
const link = from([authLink, httpLink]) | |
return new ApolloClient({ | |
link, | |
cache: new InMemoryCache().restore(window.__APOLLO_STATE__), | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment