Skip to content

Instantly share code, notes, and snippets.

@detrohutt
Last active January 23, 2020 02:21
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save detrohutt/3bddfe943a2f3ef2a797a6f7870049e8 to your computer and use it in GitHub Desktop.
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
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
}
@jakec-dev
Copy link

How does this fit in with example in the repo?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment