jaydenseric / graphql-react

A GraphQL client for React using modern context and hooks APIs that is lightweight (< 4 kB) but powerful; the first Relay and Apollo alternative with server side rendering.
https://npm.im/graphql-react
MIT License
717 stars 22 forks source link

Passing URL parameters #8

Closed murunwas closed 6 years ago

murunwas commented 6 years ago

Unable to get nextjs url parameters (query) on server-side using getInitialProps using the Provider

WITH PROVIDER image

WITHOUT PROVIDER image

jaydenseric commented 6 years ago

Is this the code you are using for the page decorator? If so, this issue would be better filed in the examples repo. It is a very simple example for the purposes of demo.

Here is a more advanced copy and paste from one of my apps:

import 'cross-fetch/polyfill'
import React, { Component } from 'react'
import getDisplayName from 'react-display-name'
import { GraphQL, Provider, preload } from 'graphql-react'
import { Router } from 'next/router'
import App from 'next/app'

// Shares the client GraphQL instance between pages.
let clientGraphql

export const withGraphQL = Composed =>
  class WithGraphQL extends Component {
    constructor(props) {
      super(props)

      // Set the GraphQL instance used in render().
      this.graphql =
        typeof window !== 'undefined'
          ? // Client: Shared instance, created at first render after SSR.
            (clientGraphql =
              clientGraphql || new GraphQL({ cache: props.cache }))
          : // Server: Private instance for SSR.
            new GraphQL({ cache: props.cache })
    }

    static displayName = `WithGraphQL(${getDisplayName(Composed)})`

    static async getInitialProps(ctx) {
      // Use initial props from nested page decorators.
      const props = Composed.getInitialProps
        ? await Composed.getInitialProps(ctx)
        : {}

      // If server environment, preload the page.
      if (ctx.req) {
        const graphql = new GraphQL()

        await preload(
          <App
            router={new Router(ctx.pathname, ctx.query, ctx.asPath)}
            pageProps={{ graphql, pageProps: props }}
            Component={this.renderPage}
          />
        )

        props.cache = graphql.cache
      }

      return props
    }

    static renderPage = ({ graphql, pageProps }) => (
      <Provider value={graphql}>
        <Composed {...pageProps} />
      </Provider>
    )

    render() {
      return this.constructor.renderPage({
        graphql: this.graphql,
        pageProps: this.props
      })
    }
  }
murunwas commented 6 years ago

Thank you for the response, I just tweak the provider as shown on image below:

image it's working fine but I'll try the advanced one, cheerz

jaydenseric commented 6 years ago

The issue seems resolved 🤞

jaydenseric commented 6 years ago

@murunwas you might be interested in the new way to to it with Next.js v7 and _app.js… https://github.com/jaydenseric/graphql-react-examples/blob/master/pages/_app.js#L3