Skip to content

Instantly share code, notes, and snippets.

@LawJolla
Created September 18, 2017 06:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save LawJolla/9c9f02d68782db3242deec9325343a30 to your computer and use it in GitHub Desktop.
Save LawJolla/9c9f02d68782db3242deec9325343a30 to your computer and use it in GitHub Desktop.
Gatsby + Apollo Browser API
import React from 'react'
import { Router } from 'react-router-dom'
import ApolloClient, { createNetworkInterface } from 'apollo-client'
import { ApolloProvider } from 'react-apollo'
import { SubscriptionClient, addGraphQLSubscriptions } from 'subscriptions-transport-ws'
const networkInterface = createNetworkInterface({
// endpoint comes from .env.development/production
uri: process.env.GRAPHCOOL_API
})
const wsClient = new SubscriptionClient(process.env.GRAPHCOOL_SUBSCRIPTION, {
reconnect: true
})
const networkInterfaceWithSubscriptions = addGraphQLSubscriptions(networkInterface, wsClient);
exports.replaceRouterComponent = ({ history }) => {
const client = new ApolloClient({
networkInterface: networkInterfaceWithSubscriptions,
dataIdFromObject: o => o.id,
initialState: window.__APOLLO_STATE__,
})
const ConnectedRouterWrapper = ({ children }) => (
<ApolloProvider client={client}>
<Router history={history}>{children}</Router>
</ApolloProvider>
)
return ConnectedRouterWrapper
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment