- initApollo
Created
October 23, 2018 14:47
-
-
Save davestacey/623608bbd5ee34e1f9683253c1e819be to your computer and use it in GitHub Desktop.
with-apollo-auth
This file contains 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
//examples/with-apollo-auth/lib/withApollo.js | |
static async getInitialProps (ctx) { | |
const { Component, router, ctx: { req, res } } = ctx | |
const token = parseCookies(req).token | |
const apollo = initApollo({}, { | |
getToken: () => token | |
}) | |
ctx.ctx.apolloClient = apollo |
This file contains 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
function create (initialState, { getToken }) { | |
const httpLink = createHttpLink({ | |
uri: 'https://api.graph.cool/simple/v1/cj5geu3slxl7t0127y8sity9r', | |
credentials: 'same-origin' | |
}) | |
const authLink = setContext((_, { headers }) => { | |
const token = getToken() | |
return { | |
headers: { | |
...headers, | |
authorization: token ? `Bearer ${token}` : '' | |
} | |
} | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment