Skip to content

Instantly share code, notes, and snippets.

@alex-r89
Created July 5, 2021 12:46
Show Gist options
  • Save alex-r89/44520fb932bd1090b7379257106e6d9f to your computer and use it in GitHub Desktop.
Save alex-r89/44520fb932bd1090b7379257106e6d9f to your computer and use it in GitHub Desktop.
Apollo Client
import * as React from 'react'
import { ApolloProvider } from '@apollo/client'
import { initApolloClient } from './apollo-client'
/*
This wrapper helps to provide Apollo functionality during SSR, while rehydrating
on the client with a pre-populated cache of the query results.
Refer to https://github.com/zeit/next.js/blob/canary/examples/api-routes-apollo-server-and-client/apollo/client.js
for more source.
*/
export function withApollo(PageComponent) {
const WithApollo = ({ apolloStaticCache, ...pageProps }) => {
// initialApolloState prop gets set in getStaticProps on page views
const client = initApolloClient(apolloStaticCache)
return (
<ApolloProvider client={client}>
<PageComponent {...pageProps} />
</ApolloProvider>
)
}
// Set the correct displayName in development
if (process.env.NODE_ENV !== 'production') {
const displayName = PageComponent.displayName || PageComponent.name || 'Component'
WithApollo.displayName = `withApollo(${displayName})`
}
return WithApollo
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment