Last active
September 24, 2019 18:11
-
-
Save sockol/528cdf1bf4975b3ac0fef26ac5f05ced to your computer and use it in GitHub Desktop.
Get GraphQL Apollo to work with NextJS Server Side Rendering within Docker
This file contains hidden or 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 { | |
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