Giter VIP home page Giter VIP logo

gatsby-hasura-starter's Introduction

Gatsby-Hasura Starter Boilerplate

This is a boilerplate for a Gatsby app paired with a Hasura project as the backend.

The sample app is a simple to do list that demonstrates both query and mutation from the linked Hasura project.

⚙️ Setup

  1. Create a Gatsby site.

    Use the Gatsby CLI to create a new site, specifying the hello-world starter.

    # create a new Gatsby site using the gatsby-hasura starter
    gatsby new gatsby-hasura https://github.com/ksmorano/gatsby-hasura-starter
  2. Setup Hasura endpoint

    !! The sample app won't work unless Hasura is configured properly. To make the example run:

    1. Create a Hasura project. For more information, visit Hasura.io. They have a very easy setup with Heroku.
    2. Add a todo table using the Hasura console:
    column_name column_type
    id Integer (auto increment)
    task Text
    priority Text
  3. Add your env variables

    Create a .env file and add the following:

    GATSBY_HASURA_GRAPHQL_URL=https://yourhasuraurl.com/v1/graphql
    GATSBY_HASURA_GRAPHQL_ADMIN_SECRET=youradminsecret

    GATSBY_HASURA_GRAPHQL_URL is the graphql endpoint that can be found in your Hasura console. GATSBY_HASURA_GRAPHQL_ADMIN_SECRET I set to your Hasura variables (as HASURA_GRAPHQL_ADMIN_SECRET) to protect your endpoint. This is set via the Heroku variables. For more information, visit Hasura documentation.

  4. Start developing.

    Navigate into your new site’s directory and start it up.

    cd gatsby-hasura/
    gatsby develop
  5. Open the source code and start editing!

    Your site is now running at http://localhost:8000!

📊 Making Queries and Mutations

  1. Queries

    While gatsby develop is running, you may visit http://localhost:8000/___graphql to access the GraphiQL and test queries.

    Example usage with useQuery:

    TodoList.js

     import React from "react"
     import { useQuery } from "@apollo/react-hooks"
     import { gql } from "apollo-boost"
    
     const GET_TODOS = gql`
       {
         todo {
           id
           name
         }
       }
     `
    
     const TodoList = () => {
       const { loading, error, data } = useQuery(GET_TODOS)
    
       if (loading) return "loading..."
       if (error) return `Error: ${JSON.stringify(error)}`
    
       return (
         <div>
           {data.todo.map(item => (
             <p key={item.id}>{item.name}</p>
           ))}
         </div>
       )
     }
    
     export default TodoList
  2. Mutations

    Example usage with useMutation:

    AddTodo.js

     import React, { useState } from "react"
     import { useMutation } from "@apollo/react-hooks"
     import { gql } from "apollo-boost"
    
     const ADD_TODO = gql`
       mutation insert_todo($task: String!, $priority: String!, $done: Boolean!) {
         insert_todo(objects: { task: $task, priority: $priority, done: $done }) {
           returning {
             task
             priority
           }
         }
       }
     `
    
     const TodoForm = () => {
       const [values, setValue] = useState({ task: "", priority: "", done: false })
       const [insert_todo, { loading, error }] = useMutation(ADD_TODO, {
         onCompleted: () => setValue({ task: "", priority: "", done: false }),
       })
    
       if (loading) return "loading..."
       if (error) return `Error ${JSON.stringify(error, null, 2)}`
    
       return (
         <form
           onSubmit={event => {
             event.preventDefault()
             insert_todo({
               variables: {
                 task: values.task,
                 priority: values.priority,
                 done: false,
               },
             })
           }}
         >
             <label htmlFor="task">
               Task
               <input
                 name="task"
                 type="text"
                 value={values.task}
                 onChange={event =>
                   setValue({ ...values, task: event.target.value })
                 }
               />
             </label>
    
             <span>Priority:</span>
             <label htmlFor="priority">
               <input
                 name="priority"
                 className="radio"
                 type="radio"
                 value="high"
                 onChange={event =>
                   setValue({ ...values, priority: event.target.value })
                 }
               />
               High
             </label>
             <label htmlFor="priority">
               <input
                 name="priority"
                 className="radio"
                 type="radio"
                 value="low"
                 onChange={event =>
                   setValue({ ...values, priority: event.target.value })
                 }
               />
               Low
             </label>
           </div>
    
           <button type="submit">
             Add Todo
           </button>
         </form>
       )
     }
    
     export default TodoForm

Dependencies

  1. gatsby-source-graphql
  2. @apollo/react-hooks
  3. apollo-boost
  4. apollo-link-http

This is my first contribution to open source, please feel free to DM me at Twitter (@kimberrypi). Huge thanks to Jason Lengstorf and Vladimir Novick for the inspiration.

gatsby-hasura-starter's People

Contributors

dependabot[bot] avatar kimberrypi avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

pontuskarlsson

gatsby-hasura-starter's Issues

Field done is not found in type

Error:

Please make sure that your Hasura project is properly setup.
Error details: ${"graphQLErrors":[{"extensions":{"path":"$.selectionSet.todo.selectionSet.done","code":"validation-failed"},"message":"field \"done\" not found in type: 'todo'"}],"networkError":null,"message":"GraphQL error: field \"done\" not found in type: 'todo'"}

Cause: The GET_TODOS query includes a field done which is not included in the README

Fix: Update README.md to include the new row in the todo table.

This starter is awesome, thanks.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.