Last active
January 23, 2020 02:21
-
-
Save detrohutt/3bddfe943a2f3ef2a797a6f7870049e8 to your computer and use it in GitHub Desktop.
zeit/next.js/examples/with-apollo/lib/initClient.js -- changed to support subscriptions
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, createNetworkInterface } from 'react-apollo' | |
import { SubscriptionClient, addGraphQLSubscriptions } from 'subscriptions-transport-ws' | |
const uri = 'http://localhost:4000/graphql' | |
const subscriptionsURI = 'ws://localhost:4000/' | |
let apolloClient = null | |
function _initClient (headers, initialState, subscriptionsInterface) { | |
return new ApolloClient({ | |
initialState, | |
ssrMode: !process.browser, | |
dataIdFromObject: result => result.id || null, | |
networkInterface: subscriptionsInterface || createNetworkInterface({ uri }) | |
}) | |
} | |
export const initClient = (headers, initialState = {}) => { | |
if (!process.browser) { | |
// Server uses a standard ApolloClient instance | |
return _initClient(headers, initialState) | |
} | |
if (!apolloClient) { | |
// Client uses an ApolloClient that supports subscriptions | |
apolloClient = _initClient(headers, initialState, addGraphQLSubscriptions( | |
createNetworkInterface({ uri }), | |
new SubscriptionClient(subscriptionsURI, { reconnect: true }) | |
)) | |
} | |
return apolloClient | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
How does this fit in with example in the repo?