Skip to content

Commit

Permalink
Add apolloClient to NextPageContext & NextPageContext
Browse files Browse the repository at this point in the history
  • Loading branch information
HaNdTriX committed Feb 5, 2020
1 parent dfe9901 commit 585df29
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions examples/with-apollo/lib/apollo.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,18 @@ export function withApollo(PageComponent, { ssr = true } = {}) {
if (ssr || PageComponent.getInitialProps) {
WithApollo.getInitialProps = async ctx => {
const { AppTree } = ctx
const inAppContext = Boolean(ctx.ctx)

// Initialize ApolloClient, add it to the ctx object so
// we can use it in `PageComponent.getInitialProp`.
const apolloClient = (ctx.apolloClient = initApolloClient())
// Initialize ApolloClient
const apolloClient = initApolloClient()

// Add apolloClient to NextPageContext & NextAppContext
// This allows us to consume the apolloClient inside our
// custom `getInitialProps({ apolloClient })`.
ctx.apolloClient = apolloClient
if (inAppContext) {
ctx.ctx.apolloClient = apolloClient
}

// Run wrapped getInitialProps methods
let pageProps = {}
Expand All @@ -64,7 +72,6 @@ export function withApollo(PageComponent, { ssr = true } = {}) {

// Since AppComponents and PageComponents have different context types
// we need to modify their props a little.
const inAppContext = Boolean(ctx.ctx)
let props
if (inAppContext) {
props = { ...pageProps, apolloClient }
Expand Down

0 comments on commit 585df29

Please sign in to comment.