Created
September 18, 2017 06:34
-
-
Save LawJolla/9c9f02d68782db3242deec9325343a30 to your computer and use it in GitHub Desktop.
Gatsby + Apollo Browser API
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 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