Created
June 26, 2019 19:01
-
-
Save eigenmannmartin/8b51549db154265f1d11bc05d60bb043 to your computer and use it in GitHub Desktop.
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
import withApollo from 'next-with-apollo' | |
import { ApolloClient, InMemoryCache, HttpLink } from 'apollo-boost'; | |
import { pathOr } from 'lodash/fp' | |
import fetch from 'isomorphic-unfetch' | |
export default withApollo(({ ctx, headers, initialState }) => { | |
const isBrowser = typeof window !== 'undefined' | |
const endpoint = '/graphql' | |
const proto = pathOr('https', 'x-forwarded-proto', headers); | |
const host = pathOr('', 'x-now-deployment-url', headers) || pathOr('', 'env.FRONTEND_URL', process) | |
const uri = isBrowser | |
? endpoint | |
: `${proto}://${host}${endpoint}` | |
return new ApolloClient({ | |
connectToDevTools: isBrowser, | |
ssrMode: !isBrowser, | |
link: new HttpLink({ | |
fetch: !isBrowser && fetch, | |
uri, | |
credentials: 'include' | |
}), | |
cache: new InMemoryCache().restore(initialState || {}), | |
}) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment