Postgres: Tables Basics
Introduction
Adding tables allows you to define the GraphQL types of your schema including their corresponding fields.
Creating tables
Let's say we want to create two simple tables for articles
and author
schema:
Console
CLI
API
Open the Hasura Console and head to the Data
tab and click on the button on the left side bar to open up an interface to create tables.
For example, here is the schema for the articles
table in this interface:
Tracking tables
Tables can be present in the underlying Postgres database without being exposed over the GraphQL API. In order to expose a table over the GraphQL API, it needs to be tracked.
Console
CLI
API
When a table is created via the Hasura Console, it gets tracked by default.
You can track any existing tables in your database from the Data -> Schema
page:
Generated GraphQL schema types
As soon as a table is created and tracked, the corresponding GraphQL schema types and query/mutation fields will be automatically generated.
The following object type is generated for the articles
table we just created and tracked:
Let's analyze the above type:
Articles
is the name of the typeid
,title
,content
,rating
andauthor_id
are fields of theArticles
typeInt
andString
are types that fields can have
The following query/mutation fields are generated for the articles
table we just created and tracked:
These auto-generated fields will allow you to query and mutate data in our table.
See the query and mutation API references for the full specifications.
GraphQL types documentation
Hasura automatically picks up any comments that might have been added to your tables and columns and adds them as GraphQL descriptions of the auto-generated types and fields.
Try out basic GraphQL requests
At this point, you should be able to try out basic GraphQL queries/mutations on the newly created tables from the API
tab in the Console. (You may want to add some sample data into the tables first)
Query all rows in the
articles
table:Query Variables
Request Headers
Insert data in the
author
table:Query Variables
Request Headers
Note
author's id
does not need to be passed as an input as it is of type serial
(auto incrementing integer).
Last updated