1
0
Fork 0
mirror of https://github.com/terribleplan/next.js.git synced 2024-01-19 02:48:18 +00:00
next.js/examples/with-apollo/lib/init-apollo.js
HaNdTriX 5ff7c0742c Lint examples (#4985)
* 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
2018-08-20 08:31:24 +02:00

38 lines
1.2 KiB
JavaScript

import { ApolloClient, InMemoryCache, HttpLink } from 'apollo-boost'
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
}