Skip to content

Instantly share code, notes, and snippets.

@eigenmannmartin
Created June 26, 2019 19:01
Show Gist options
  • Save eigenmannmartin/8b51549db154265f1d11bc05d60bb043 to your computer and use it in GitHub Desktop.
Save eigenmannmartin/8b51549db154265f1d11bc05d60bb043 to your computer and use it in GitHub Desktop.
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