Skip to content

Instantly share code, notes, and snippets.

@davidroyer
Created March 16, 2018 06:43
Show Gist options
  • Save davidroyer/5c22b625d4638c49c46606eb1b4f536c to your computer and use it in GitHub Desktop.
Save davidroyer/5c22b625d4638c49c46606eb1b4f536c to your computer and use it in GitHub Desktop.
// import { HttpLink } from 'apollo-link-http'
// import { InMemoryCache } from 'apollo-cache-inmemory'
import { ApolloLink } from 'apollo-link'
import { HttpLink } from 'apollo-link-http'
import { InMemoryCache } from 'apollo-cache-inmemory'
const GRAPHCMS_API = 'https://api.graphcms.com/simple/v1/myBlog'
export default (ctx) => {
const httpLink = new HttpLink({ uri: GRAPHCMS_API })
// middleware
const middlewareLink = new ApolloLink((operation, forward) => {
const token = process.server ? ctx.req.session : window.__NUXT__.state.session
operation.setContext({
headers: { authorization: `Bearer ${token}` }
})
return forward(operation)
})
const link = middlewareLink.concat(httpLink)
return {
link,
cache: new InMemoryCache()
}
}
// Replace this with your project's endpoint
// const GRAPHCMS_API = 'https://api.graphcms.com/simple/v1/myBlog'
//
// export default () => ({
// link: new HttpLink({ uri: GRAPHCMS_API }),
// cache: new InMemoryCache()
// })
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment