项目作者: kimberrypi

项目描述 :
Boilerplate for Gatsby with Hasura as backend
高级语言: JavaScript
项目地址: git://github.com/kimberrypi/gatsby-hasura-starter.git
创建时间: 2019-08-14T15:49:43Z
项目社区:https://github.com/kimberrypi/gatsby-hasura-starter

开源协议:MIT License

下载



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.

    1. # create a new Gatsby site using the gatsby-hasura starter
    2. 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:

    1. GATSBY_HASURA_GRAPHQL_URL=https://yourhasuraurl.com/v1/graphql
    2. 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.

    1. cd gatsby-hasura/
    2. 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

    1. import React from "react"
    2. import { useQuery } from "@apollo/react-hooks"
    3. import { gql } from "apollo-boost"
    4. const GET_TODOS = gql`
    5. {
    6. todo {
    7. id
    8. name
    9. }
    10. }
    11. `
    12. const TodoList = () => {
    13. const { loading, error, data } = useQuery(GET_TODOS)
    14. if (loading) return "loading..."
    15. if (error) return `Error: ${JSON.stringify(error)}`
    16. return (
    17. <div>
    18. {data.todo.map(item => (
    19. <p key={item.id}>{item.name}</p>
    20. ))}
    21. </div>
    22. )
    23. }
    24. export default TodoList
  2. Mutations

    Example usage with useMutation:

    AddTodo.js

    1. import React, { useState } from "react"
    2. import { useMutation } from "@apollo/react-hooks"
    3. import { gql } from "apollo-boost"
    4. const ADD_TODO = gql`
    5. mutation insert_todo($task: String!, $priority: String!, $done: Boolean!) {
    6. insert_todo(objects: { task: $task, priority: $priority, done: $done }) {
    7. returning {
    8. task
    9. priority
    10. }
    11. }
    12. }
    13. `
    14. const TodoForm = () => {
    15. const [values, setValue] = useState({ task: "", priority: "", done: false })
    16. const [insert_todo, { loading, error }] = useMutation(ADD_TODO, {
    17. onCompleted: () => setValue({ task: "", priority: "", done: false }),
    18. })
    19. if (loading) return "loading..."
    20. if (error) return `Error ${JSON.stringify(error, null, 2)}`
    21. return (
    22. <form
    23. onSubmit={event => {
    24. event.preventDefault()
    25. insert_todo({
    26. variables: {
    27. task: values.task,
    28. priority: values.priority,
    29. done: false,
    30. },
    31. })
    32. }}
    33. >
    34. <label htmlFor="task">
    35. Task
    36. <input
    37. name="task"
    38. type="text"
    39. value={values.task}
    40. onChange={event =>
    41. setValue({ ...values, task: event.target.value })
    42. }
    43. />
    44. </label>
    45. <span>Priority:</span>
    46. <label htmlFor="priority">
    47. <input
    48. name="priority"
    49. className="radio"
    50. type="radio"
    51. value="high"
    52. onChange={event =>
    53. setValue({ ...values, priority: event.target.value })
    54. }
    55. />
    56. High
    57. </label>
    58. <label htmlFor="priority">
    59. <input
    60. name="priority"
    61. className="radio"
    62. type="radio"
    63. value="low"
    64. onChange={event =>
    65. setValue({ ...values, priority: event.target.value })
    66. }
    67. />
    68. Low
    69. </label>
    70. </div>
    71. <button type="submit">
    72. Add Todo
    73. </button>
    74. </form>
    75. )
    76. }
    77. 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.