Skip to content

Instantly share code, notes, and snippets.

@maticzav
Last active February 5, 2018 20:44
Show Gist options
  • Save maticzav/21f3e50d7dd66d91f94548dfca281aa5 to your computer and use it in GitHub Desktop.
Save maticzav/21f3e50d7dd66d91f94548dfca281aa5 to your computer and use it in GitHub Desktop.
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