Last active
April 30, 2019 15:47
-
-
Save jamesperi/7185e972d6364ac6b9d07f46e1a7b66e to your computer and use it in GitHub Desktop.
init-apollo with [email protected] in place of HttpLink
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 { ApolloClient, InMemoryCache, HttpLink } from 'apollo-boost' | |
import { createUploadLink } from 'apollo-upload-client' | |
import fetch from 'isomorphic-unfetch' | |
let apolloClient = null | |
function create(initialState) { | |
// Check out https://github.com/zeit/next.js/pull/4611 if you want to use the AWSAppSyncClient | |
return new ApolloClient({ | |
connectToDevTools: process.browser, | |
ssrMode: !process.browser, // Disables forceFetch on the server (so queries are only run once) | |
// link: new HttpLink({ | |
// uri: 'https://api.graph.cool/simple/v1/cixmkt2ul01q00122mksg82pn', // Server URL (must be absolute) | |
// credentials: 'same-origin', // Additional fetch() options like `credentials` or `headers` | |
// // Use fetch() polyfill on the server | |
// fetch: !process.browser && fetch | |
// }), | |
link: createUploadLink({ | |
uri: 'https://api.graph.cool/simple/v1/cixmkt2ul01q00122mksg82pn', // Server URL (must be absolute) | |
credentials: 'same-origin', // Additional fetch() options like `credentials` or `headers` | |
// Use fetch() polyfill on the server | |
fetch: !process.browser && fetch | |
}), | |
cache: new InMemoryCache().restore(initialState || {}) | |
}) | |
} | |
export default function initApollo(initialState) { | |
// Make sure to create a new client for every server-side request so that data | |
// isn't shared between connections (which would be bad) | |
if (!process.browser) { | |
return create(initialState) | |
} | |
// Reuse client on the client-side w | |
if (!apolloClient) { | |
apolloClient = create(initialState) | |
} | |
return apolloClient | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment