mirror of
https://github.com/terribleplan/next.js.git
synced 2024-01-19 02:48:18 +00:00
5ff7c0742c
* Lint examples/with-apollo-and-redux-saga * Lint examples/with-apollo-auth * Lint examples/with-apollo * Lint exampels/with-google-analytics * Lint examples/with-higher-order-component * Lint examples/with-react-i18next * Lint exampels/with-redux * Lint exampels/with-relay-modern * Lint examples/with-universal-configuration-runtime * Add **/examples/**/lib/** to linter
40 lines
1.3 KiB
JavaScript
40 lines
1.3 KiB
JavaScript
import { ApolloClient } from 'apollo-client'
|
|
import { HttpLink } from 'apollo-link-http'
|
|
import { InMemoryCache } from 'apollo-cache-inmemory'
|
|
import fetch from 'isomorphic-unfetch'
|
|
|
|
let apolloClient = null
|
|
|
|
// Polyfill fetch() on the server (used by apollo-client)
|
|
if (!process.browser) {
|
|
global.fetch = fetch
|
|
}
|
|
|
|
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`
|
|
}),
|
|
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
|
|
if (!apolloClient) {
|
|
apolloClient = create(initialState)
|
|
}
|
|
|
|
return apolloClient
|
|
}
|