2019-01-17 13:27:28 +03:00
# gatsby-postgres-graphql
Boilerplate to get started with Gatsby, Hasura GraphQL engine as CMS and postgres as database using the awesome plugin [gatsby-source-graphql ](https://github.com/gatsbyjs/gatsby/tree/master/packages/gatsby-source-graphql ).
2019-06-14 12:07:00 +03:00
[![Edit gatsby-postgres-graphql ](https://codesandbox.io/static/img/play-codesandbox.svg )](https://codesandbox.io/s/github/hasura/graphql-engine/tree/master/community/sample-apps/gatsby-postgres-graphql?fontsize=14)
2019-01-17 13:27:28 +03:00
![Gatsby Postgres GraphQL ](./assets/gatsby-postgres-graphql.png )
# Tutorial
2021-03-02 11:31:35 +03:00
- Deploy GraphQL Engine on Hasura Cloud and setup PostgreSQL via Heroku:
2021-08-19 10:02:11 +03:00
[![Deploy to Hasura Cloud ](https://graphql-engine-cdn.hasura.io/img/deploy_to_hasura.png )](https://cloud.hasura.io/signup)
2019-01-17 13:27:28 +03:00
2021-03-02 11:31:35 +03:00
- Get the Hasura app URL (say `gatsby-graphql.hasura.app` )
2019-01-17 13:27:28 +03:00
2021-03-02 11:31:35 +03:00
- Clone this repo:
2019-01-17 13:27:28 +03:00
2019-12-02 13:27:03 +03:00
```bash
git clone https://github.com/hasura/graphql-engine
cd graphql-engine/community/sample-apps/gatsby-postgres-graphql
```
2021-03-02 11:31:35 +03:00
- Create `author` table:
2019-12-02 13:27:03 +03:00
2021-03-02 11:31:35 +03:00
Open Hasura console: visit https://gatsby-graphql.hasura.app on a browser
2019-12-02 13:27:03 +03:00
Navigate to `Data` section in the top nav bar and create a table as follows:
![Create author table ](./assets/add_table.jpg )
2021-03-02 11:31:35 +03:00
- Insert sample data into `author` table:
2019-01-17 13:27:28 +03:00
2019-12-02 13:27:03 +03:00
![Insert data into author table ](./assets/insert_data.jpg )
2019-01-17 13:27:28 +03:00
2019-12-02 13:27:03 +03:00
Verify if the row is inserted successfully
2019-01-17 13:27:28 +03:00
2019-12-02 13:27:03 +03:00
![Insert data into author table ](./assets/browse_rows.jpg )
2019-01-17 13:27:28 +03:00
2021-03-02 11:31:35 +03:00
- Install npm modules:
2019-01-17 13:27:28 +03:00
2019-12-02 13:27:03 +03:00
```bash
npm install
```
2021-03-02 11:31:35 +03:00
- Configure gatsby to use `gatsby-source-graphql` plugin and a connection GraphQL url to stitch the schema.
2019-01-17 13:27:28 +03:00
```js
{
plugins: [
{
2019-12-02 13:27:03 +03:00
resolve: "gatsby-source-graphql", // < - Configure plugin
2019-01-17 13:27:28 +03:00
options: {
2019-12-02 13:27:03 +03:00
typeName: "HASURA",
fieldName: "hasura", // < - fieldName under which schema will be stitched
2019-12-27 16:55:58 +03:00
url: process.env.GATSBY_HASURA_GRAPHQL_URL,
2019-12-02 13:27:03 +03:00
refetchInterval: 10 // Refresh every 10 seconds for new data
}
}
];
2019-01-17 13:27:28 +03:00
}
```
2021-03-02 11:31:35 +03:00
- Run the app:
2019-12-02 13:27:03 +03:00
```bash
2021-03-02 11:31:35 +03:00
GATSBY_HASURA_GRAPHQL_URL=https://gatsby-graphql.hasura.app/v1/graphql npm run develop
2019-12-02 13:27:03 +03:00
```
2021-03-02 11:31:35 +03:00
- Test the app
2019-12-02 13:27:03 +03:00
Visit [http://localhost:8000 ](http://localhost:8000 ) to view the app
![Demo app ](./assets/test_app.jpg )
# Make a GraphQL query from your component using hooks
1. Create a component named `AuthorList.js` :
```js
import React from "react";
import { useQuery } from "@apollo/react-hooks";
import { gql } from "apollo-boost";
const GET_AUTHORS = gql`
query {
author {
id
name
}
}
`;
const AuthorList = () => {
const { loading, error, data } = useQuery(GET_AUTHORS);
if (loading) return "loading...";
if (error) return `error: ${error.message}` ;
return (
< div >
{data.author.map((author, index) => (
< div key = {index} >
< h2 > {author.name}< / h2 >
< / div >
))}
< / div >
);
};
export default AuthorList;
export { GET_AUTHORS };
```
# Make a GraphQL mutation using hooks
Additional packages are needed to be added to support mutations: < br / >
`npm install @apollo/react-hooks apollo-boost isomorphic-fetch`
1. Create an `apollo.js` util file:
```js
import ApolloClient from "apollo-boost";
import fetch from "isomorphic-fetch";
export const client = new ApolloClient({
uri: process.env.GATSBY_HASURA_GRAPHQL_URL,
fetch
});
```
2. Create `gatsby-browser.js` and `gatsby-ssr.js`
2019-01-17 13:27:28 +03:00
```js
2019-12-02 13:27:03 +03:00
import React from "react";
import { ApolloProvider } from "@apollo/react-hooks";
import { client } from "./src/utils/apollo";
export const wrapRootElement = ({ element }) => (
< ApolloProvider client = {client} > {element}< / ApolloProvider >
);
```
3. Create an `AddAuthor.js` component to add mutations:
```js
import React, { useState } from "react";
import { useMutation } from "@apollo/react-hooks";
import { gql } from "apollo-boost";
import { GET_AUTHORS } from "./AuthorList";
const ADD_AUTHOR = gql`
mutation insert_author($name: String!) {
insert_author(objects: { name: $name }) {
returning {
2019-01-17 13:27:28 +03:00
id
name
}
}
}
2019-12-02 13:27:03 +03:00
`;
const AddAuthor = () => {
const [author, setAuthor] = useState("");
const [insert_author, { loading, error }] = useMutation(ADD_AUTHOR, {
update: (cache, { data }) => {
setAuthor("");
const existingAuthors = cache.readQuery({
query: GET_AUTHORS
});
// Add the new author to the cache
const newAuthor = data.insert_author.returning[0];
cache.writeQuery({
query: GET_AUTHORS,
data: {author: [newAuthor, ...existingAuthors.author]}
});
}
});
if (loading) return "loading...";
if (error) return `error: ${error.message}` ;
2019-01-17 13:27:28 +03:00
2019-12-02 13:27:03 +03:00
const handleSubmit = event => {
event.preventDefault();
insert_author({
variables: {
name: author
}
});
};
return (
< form onSubmit = {handleSubmit} >
< label htmlFor = "author" >
Add Author:
< input
name="author"
value={author}
onChange={event => setAuthor(event.target.value)}
/>
< / label >
< button type = "submit" > ADD< / button >
< / form >
);
};
export default AddAuthor;
```
2019-01-17 13:27:28 +03:00
2019-12-02 13:27:03 +03:00
4. Run the app and test mutation. New data will be added to the top via a cache update.
2019-01-17 13:27:28 +03:00
# Contributing
Checkout the [contributing guide ](../../../CONTRIBUTING.md#community-content ) for more details.