graphql-engine/community/sample-apps/nextjs-postgres-graphql
Sai Krishna Prasad Kandula d7a52b9342 readme: use cloud signup endpoint for "Deploy to Hasura" CTA
https://github.com/hasura/graphql-engine-mono/pull/2142

GitOrigin-RevId: 212d766978841d5609d726f8359586185f3cbb59
2021-08-19 07:03:05 +00:00
..
assets add github workflow to compress new images in PRs 2021-03-10 20:55:02 +00:00
pages update package versions for next-postgres-graphql (close #2714) (#2894) 2019-09-17 13:13:04 +05:30
.gitignore refactor community content and folder structure (#1353) 2019-01-17 15:57:28 +05:30
config.js update package versions for next-postgres-graphql (close #2714) (#2894) 2019-09-17 13:13:04 +05:30
package.json update package versions for next-postgres-graphql (close #2714) (#2894) 2019-09-17 13:13:04 +05:30
README.md readme: use cloud signup endpoint for "Deploy to Hasura" CTA 2021-08-19 07:03:05 +00:00

nextjs-postgres-graphql

Boilerplate to get started with Nextjs, Hasura GraphQL engine as CMS and postgres as database using this awesome library: withData.

Edit nextjs-postgres-graphql

Nextjs Postgres GraphQL

Tutorial

  • Deploy GraphQL Engine on Hasura Cloud and setup PostgreSQL via Heroku:

    Deploy to Hasura Cloud

  • Get the Hasura app URL (say nextjs-graphql.hasura.app)

  • Create author table:

    Open Hasura console: visit https://nextjs-graphql.hasura.app on a browser
    Navigate to Data section in the top nav bar and create a table as follows:

    Create author table

  • Insert sample data into author table:

    Insert data into author table

    Verify if the row is inserted successfully

    Insert data into author table

  • Clone this repo:

    git clone https://github.com/hasura/graphql-engine
    cd graphql-engine/community/sample-apps/nextjs-postgres-graphql
    
  • Install npm modules:

    npm install
    
  • Create config.js as follows, in this step we are configuring withData with an httpLink to connect to a valid GraphQL server URL.

    import { withData } from 'next-apollo'
    import { HttpLink } from 'apollo-link-http'
    
    // can also be a function that accepts a `context` object (SSR only) and returns a config
    const config = {
      link: new HttpLink({
        uri: 'https://my-app.hasura.app/v1/graphql', // <- Configure GraphQL Server URL (must be absolute)
      })
    }
    
    export default withData(config)
    
  • Wrap your page component with Query component from react-apollo so that appropriate data can be fetched while the page is SSRed

    • GraphQL query

      
      const query = gql`
      	query {
      	  author {
      	    id
      	    name
      	  }
      	}
      `
      
      
    • Wrap your component with Query

      
        <Query    // <- Wrap the component which requires data with Query component from react-apollo
          query={ query }
          fetchPolicy={ 'cache-and-network' }
        >
          {({ loading, data, error }) => {
            if(error) {
              return (<div>Error..</div>);
            }
            return (
              <div>
                <h1>My Authors </h1>
                <AuthorList authors={data ? data.author: []} />
              </div>
            );
        }}
        </Query>
      
      
  • Run the app:

    npm run dev -- -p 8000
    
  • Test the app Visit http://localhost:8000 to view the app

    Demo app

How it works

It uses next-apollo underneath which ensures that data requirement is satisfied before it is rendered on the server and next.js takes care of the rest.

Contributing

Checkout the contributing guide for more details.