Skip to content

Instantly share code, notes, and snippets.

@sockol
Last active September 24, 2019 18:11
Show Gist options
  • Save sockol/528cdf1bf4975b3ac0fef26ac5f05ced to your computer and use it in GitHub Desktop.
Save sockol/528cdf1bf4975b3ac0fef26ac5f05ced to your computer and use it in GitHub Desktop.
Get GraphQL Apollo to work with NextJS Server Side Rendering within Docker
import {
ApolloClient,
ApolloLink,
InMemoryCache,
HttpLink
} from 'apollo-boost'
import fetch from 'isomorphic-unfetch'
const isServer = () => typeof window === `undefined`
const fetchHack = (url, ...args) => {
const shouldRewriteUri = isServer() && process.env.NODE_ENV === `development`
let adjustedUrl = shouldRewriteUri ? process.env.API_URI : process.env.API_URI_DOCKER
adjustedUrl = `${adjustedUrl}/graphql`
return fetch(adjustedUrl, ...args)
}
const httpLink = new HttpLink({
fetch: fetchHack,
credentials: `include`,
})
const create = (initialState = {}, cookies) => {
/* your middleware code here */
return new ApolloClient({
connectToDevTools: process.browser,
ssrMode: !process.browser,
link: httpLink,
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment