2018-09-11 14:11:24 +03:00
|
|
|
Schema design basics
|
|
|
|
====================
|
|
|
|
|
2018-12-03 15:12:24 +03:00
|
|
|
.. contents:: Table of contents
|
|
|
|
:backlinks: none
|
|
|
|
:depth: 1
|
|
|
|
:local:
|
|
|
|
|
2018-09-11 14:11:24 +03:00
|
|
|
The Hasura GraphQL engine creates GraphQL schema object types and corresponding query/mutation fields with resolvers
|
|
|
|
automatically as we create tables/views in the Postgres database.
|
|
|
|
|
|
|
|
Let's take a look at how to create tables using the Hasura console, a UI tool meant for doing exactly this, and what
|
|
|
|
GraphQL schema it generates.
|
|
|
|
|
|
|
|
Let's say we want to create two simple tables for an article/author schema:
|
|
|
|
|
|
|
|
.. code-block:: sql
|
|
|
|
|
|
|
|
author (
|
|
|
|
id INT PRIMARY KEY,
|
|
|
|
name TEXT
|
|
|
|
)
|
|
|
|
|
|
|
|
article (
|
|
|
|
id INT PRIMARY KEY,
|
|
|
|
title TEXT,
|
|
|
|
content TEXT,
|
|
|
|
rating INT,
|
|
|
|
author_id INT
|
|
|
|
)
|
|
|
|
|
2019-01-15 13:49:58 +03:00
|
|
|
.. _create-tables:
|
|
|
|
|
2018-09-11 14:11:24 +03:00
|
|
|
Create tables
|
|
|
|
-------------
|
|
|
|
|
|
|
|
Open the Hasura console and head to the ``Data`` tab and click the ``Create Table`` button to open up an interface to
|
|
|
|
create tables.
|
|
|
|
|
|
|
|
As soon as a table is created, the corresponding GraphQL schema types and query/mutation resolvers will be
|
|
|
|
automatically generated.
|
|
|
|
|
|
|
|
For example, here is the schema for the ``article`` table in this interface:
|
|
|
|
|
2019-03-13 13:03:45 +03:00
|
|
|
.. thumbnail:: ../../../img/graphql/manual/schema/create-table-graphql.png
|
2018-09-11 14:11:24 +03:00
|
|
|
|
2019-02-06 09:39:36 +03:00
|
|
|
The following object type and query/mutation fields are generated for the ``article`` table we just created:
|
2018-09-11 14:11:24 +03:00
|
|
|
|
|
|
|
.. code-block:: graphql
|
|
|
|
|
|
|
|
# Object type
|
|
|
|
type Article {
|
|
|
|
id: Int
|
|
|
|
title: String
|
|
|
|
content: String
|
|
|
|
rating: Int
|
|
|
|
author_id: Int
|
|
|
|
}
|
|
|
|
|
|
|
|
# Query field
|
|
|
|
article (
|
|
|
|
where: article_bool_exp
|
|
|
|
limit: Int
|
|
|
|
offset: Int
|
|
|
|
order_by: [article_order_by!]
|
|
|
|
): [article!]!
|
|
|
|
|
|
|
|
# insert/upsert mutation field
|
|
|
|
insert_article (
|
|
|
|
objects: [article_insert_input!]!
|
|
|
|
on_conflict: article_on_conflict
|
|
|
|
): article_mutation_response
|
|
|
|
|
|
|
|
# update mutation field
|
|
|
|
update_article (
|
|
|
|
where: article_bool_exp!
|
|
|
|
_inc: article_inc_input
|
|
|
|
_set: article_set_input
|
|
|
|
): article_mutation_response
|
|
|
|
|
|
|
|
# delete mutation field
|
|
|
|
delete_article (
|
|
|
|
where: article_bool_exp!
|
|
|
|
): article_mutation_response
|
|
|
|
|
2019-05-07 15:21:37 +03:00
|
|
|
See the :doc:`query <../api-reference/graphql-api/query>` and :doc:`mutation <../api-reference/graphql-api/mutation>`
|
2019-02-06 09:39:36 +03:00
|
|
|
API references for the full specifications
|
|
|
|
|
|
|
|
You can insert some sample data into the tables using the ``Insert Row`` tab of the created tables.
|
2018-09-11 14:11:24 +03:00
|
|
|
|
|
|
|
Try basic GraphQL queries
|
|
|
|
-------------------------
|
|
|
|
At this point, you should be able to try out basic GraphQL queries/mutations on the newly created tables using the
|
2019-02-06 09:39:36 +03:00
|
|
|
console ``GraphiQL`` tab (*you may want to add some sample data into the tables first*).
|
2018-09-11 14:11:24 +03:00
|
|
|
|
|
|
|
Here are a couple of examples:
|
|
|
|
|
|
|
|
- Query all rows in the ``article`` table
|
|
|
|
|
|
|
|
.. graphiql::
|
|
|
|
:view_only:
|
|
|
|
:query:
|
|
|
|
query {
|
|
|
|
article {
|
|
|
|
id
|
|
|
|
title
|
|
|
|
author_id
|
|
|
|
}
|
|
|
|
}
|
|
|
|
:response:
|
|
|
|
{
|
|
|
|
"data": {
|
|
|
|
"article": [
|
|
|
|
{
|
|
|
|
"id": 1,
|
|
|
|
"title": "sit amet",
|
|
|
|
"author_id": 4
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"id": 2,
|
|
|
|
"title": "a nibh",
|
|
|
|
"author_id": 2
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"id": 3,
|
|
|
|
"title": "amet justo morbi",
|
|
|
|
"author_id": 4
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"id": 4,
|
|
|
|
"title": "vestibulum ac est",
|
|
|
|
"author_id": 5
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
- Insert data in the ``author`` table
|
|
|
|
|
|
|
|
.. graphiql::
|
|
|
|
:view_only:
|
|
|
|
:query:
|
|
|
|
mutation add_author {
|
|
|
|
insert_author(
|
|
|
|
objects: [
|
|
|
|
{id: 11, name: "Jane"}
|
|
|
|
]
|
|
|
|
) {
|
|
|
|
affected_rows
|
|
|
|
}
|
|
|
|
}
|
|
|
|
:response:
|
|
|
|
{
|
|
|
|
"data": {
|
|
|
|
"insert_author": {
|
|
|
|
"affected_rows": 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|