2020-01-14 15:57:45 +03:00
.. meta ::
:description: Manage tables and views with the Hasura schema/metadata API
:keywords: hasura, docs, schema/metadata API, API reference, table, view
2020-03-11 22:42:36 +03:00
.. _api_tables_views:
2018-10-17 08:27:11 +03:00
Schema/Metadata API Reference: Tables/Views
2019-02-06 09:39:36 +03:00
===========================================
2018-12-03 15:12:24 +03:00
.. contents :: Table of contents
:backlinks: none
:depth: 1
:local:
2020-08-25 14:53:25 +03:00
Introduction
------------
2019-09-11 10:17:14 +03:00
Track/untrack a table/view in Hasura GraphQL engine.
2018-10-17 08:27:11 +03:00
2019-02-06 09:39:36 +03:00
Only tracked tables/views are available for querying/mutating/subscribing data over the GraphQL API.
2018-10-17 08:27:11 +03:00
.. _track_table:
track_table
-----------
2019-02-06 09:39:36 +03:00
`` track_table `` is used to add a table/view to the GraphQL schema.
2018-10-17 08:27:11 +03:00
Add a table/view `` author `` :
.. code-block :: http
POST /v1/query HTTP/1.1
Content-Type: application/json
X-Hasura-Role: admin
{
"type": "track_table",
"args": {
"schema": "public",
"name": "author"
}
}
2018-12-03 15:12:24 +03:00
.. _track_table_syntax:
Args syntax
^^^^^^^^^^^
.. list-table ::
:header-rows: 1
* - Key
- Required
- Schema
- Description
* - table
- true
- :ref: `TableName <TableName>`
- Name of the table
2019-08-16 00:19:47 +03:00
* - is_enum
- false
- Boolean
2019-09-03 17:17:36 +03:00
- When set to `` true `` , creates the table as an :ref: `enum table <create_enum_table>` .
2019-08-16 00:19:47 +03:00
.. _set_table_is_enum:
set_table_is_enum
-----------------
2019-09-03 17:17:36 +03:00
`` set_table_is_enum `` sets whether an already-tracked table should be used as an :ref: `enum table <create_enum_table>` .
2019-08-16 00:19:47 +03:00
Use table `` user_role `` as an enum table:
.. code-block :: http
POST /v1/query HTTP/1.1
Content-Type: application/json
X-Hasura-Role: admin
{
"type": "set_table_is_enum",
"args": {
"table": {
"schema": "public",
"name": "user_role"
},
"is_enum": true
}
}
.. _set_table_is_enum_syntax:
Args syntax
^^^^^^^^^^^
.. list-table ::
:header-rows: 1
* - Key
- Required
- Schema
- Description
* - table
- true
- :ref: `TableName <TableName>`
- Name of the table
* - is_enum
- true
- Boolean
- Whether or not the table should be used as an :ref: `enum table <enum table>` .
2018-12-03 15:12:24 +03:00
2019-09-19 07:47:36 +03:00
.. _track_table_v2:
track_table v2
--------------
Version 2 of `` track_table `` is used to add a table/view to the GraphQL schema with configuration. You can customise the root field names.
Add a table/view `` author `` :
.. code-block :: http
POST /v1/query HTTP/1.1
Content-Type: application/json
X-Hasura-Role: admin
{
"type": "track_table",
"version": 2,
"args": {
"table": "author",
"configuration": {
"custom_root_fields": {
"select": "Authors",
"select_by_pk": "Author",
"select_aggregate": "AuthorAggregate",
"insert": "AddAuthors",
2020-05-18 14:13:57 +03:00
"insert_one":"AddAuthor",
2019-09-19 07:47:36 +03:00
"update": "UpdateAuthors",
2020-05-18 14:13:57 +03:00
"update_by_pk": "UpdateAuthor",
"delete": "DeleteAuthors",
"delete_by_pk": "DeleteAuthor"
2019-09-19 07:47:36 +03:00
},
"custom_column_names": {
"id": "authorId"
}
}
}
}
2020-10-29 15:48:45 +03:00
A table can be tracked with a `` custom name `` . This can be useful when a table
name is not GraphQL compliant, like `` Users Address `` . A `` custom name `` like
`` users_address `` will complement the `` "Users Address" ``
table, so that it can be added to the GraphQL schema.
.. code-block :: http
POST /v1/query HTTP/1.1
Content-Type: application/json
X-Hasura-Role: admin
{
"type": "track_table",
"version": 2,
"args": {
"table": "Author Details",
"configuration": {
"custom_name": "author_details"
}
}
}
The GraphQL nodes and typenames
that are generated will be according to the `` identifier `` . For example, in this case,
the nodes generated will be:
- `` users_address ``
- `` users_address_one ``
- `` users_address_aggregate ``
- `` insert_users_address ``
- `` insert_users_address_one ``
- `` update_users_address ``
- `` update_users_address_by_pk ``
- `` delete_users_address ``
- `` delete_users_address_by_pk ``
.. note ::
graphql-engine requires the constraint names (if any) of a table to be GraphQL `compliant <https://spec.graphql.org/June2018/#sec-Names> `__ in order to be able to track it.
2019-09-19 07:47:36 +03:00
.. _track_table_args_syntax_v2:
Args syntax
^^^^^^^^^^^
.. list-table ::
:header-rows: 1
* - Key
- Required
- Schema
- Description
* - table
- true
- :ref: `TableName <TableName>`
- Name of the table
* - configuration
- false
- :ref: `Table Config <table_config>`
- Configuration for the table/view
.. _table_config:
Table Config
^^^^^^^^^^^^
.. list-table ::
:header-rows: 1
* - Key
- Required
- Schema
- Description
2020-10-29 15:48:45 +03:00
* - custom_name
- false
- `` String ``
- Customise the `` <table-name> `` with the provided custom name value
. The GraphQL nodes for the table will be generated according to the custom name.
2019-09-19 07:47:36 +03:00
* - custom_root_fields
- false
- :ref: `Custom Root Fields <custom_root_fields>`
- Customise the root fields
* - custom_column_names
- false
- :ref: `CustomColumnNames`
- Customise the column fields
.. _custom_root_fields:
Custom Root Fields
^^^^^^^^^^^^^^^^^^
.. list-table ::
:header-rows: 1
* - Key
- Required
- Schema
- Description
* - select
- false
2020-05-18 14:13:57 +03:00
- `` String ``
2019-09-19 07:47:36 +03:00
- Customise the `` <table-name> `` root field
* - select_by_pk
- false
2020-05-18 14:13:57 +03:00
- `` String ``
2019-09-19 07:47:36 +03:00
- Customise the `` <table-name>_by_pk `` root field
* - select_aggregate
- false
2020-05-18 14:13:57 +03:00
- `` String ``
2019-09-19 07:47:36 +03:00
- Customise the `` <table-name>_aggregete `` root field
* - insert
- false
2020-05-18 14:13:57 +03:00
- `` String ``
2019-09-19 07:47:36 +03:00
- Customise the `` insert_<table-name> `` root field
2020-05-18 14:13:57 +03:00
* - insert_one
- false
- `` String ``
- Customise the `` insert_<table-name>_one `` root field
2019-09-19 07:47:36 +03:00
* - update
- false
2020-05-18 14:13:57 +03:00
- `` String ``
2019-09-19 07:47:36 +03:00
- Customise the `` update_<table-name> `` root field
2020-05-18 14:13:57 +03:00
* - update_by_pk
- false
- `` String ``
- Customise the `` update_<table-name>_by_pk `` root field
2019-09-19 07:47:36 +03:00
* - delete
- false
2020-05-18 14:13:57 +03:00
- `` String ``
2019-09-19 07:47:36 +03:00
- Customise the `` delete_<table-name> `` root field
2020-05-18 14:13:57 +03:00
* - delete_by_pk
- false
- `` String ``
- Customise the `` delete_<table-name>_by_pk `` root field
2019-09-19 07:47:36 +03:00
.. _set_table_custom_fields:
2020-10-29 15:48:45 +03:00
set_table_custom_fields (deprecated)
------------------------------------
`` set_table_custom_fields `` has been deprecated. Use the
:ref: `set_table_configuration <set_table_configuration>` API to set the custom
table fields.
2019-09-19 07:47:36 +03:00
`` set_table_custom_fields `` in version `` 2 `` sets the custom root fields and
custom column names of already tracked table. This will **replace** the already
present custom fields configuration.
2020-10-29 15:48:45 +03:00
2019-09-19 07:47:36 +03:00
Set custom fields for table/view `` author `` :
.. code-block :: http
POST /v1/query HTTP/1.1
Content-Type: application/json
X-Hasura-Role: admin
{
"type": "set_table_custom_fields",
"version": 2,
"args": {
"table": "author",
"custom_root_fields": {
"select": "Authors",
"select_by_pk": "Author",
"select_aggregate": "AuthorAggregate",
"insert": "AddAuthors",
2020-05-18 14:13:57 +03:00
"insert_one":"AddAuthor",
2019-09-19 07:47:36 +03:00
"update": "UpdateAuthors",
2020-05-18 14:13:57 +03:00
"update_by_pk": "UpdateAuthor",
"delete": "DeleteAuthors",
"delete_by_pk": "DeleteAuthor"
2019-09-19 07:47:36 +03:00
},
"custom_column_names": {
"id": "authorId"
}
}
}
.. _set_table_custom_fields_args_syntax:
Args syntax
^^^^^^^^^^^
.. list-table ::
:header-rows: 1
* - Key
- Required
- Schema
- Description
* - table
- true
- :ref: `TableName <TableName>`
- Name of the table
* - custom_root_fields
- false
- :ref: `Custom Root Fields <custom_root_fields>`
- Customise the root fields
* - custom_column_names
- false
- :ref: `CustomColumnNames`
- Customise the column fields
2020-10-29 15:48:45 +03:00
.. _set_table_customization:
set_table_customization
-----------------------
`` set_table_customization `` allows to customize any given table with
a custom name, custom root fields and custom column names of an already tracked
table. This will **replace** the already present customization.
:ref: `set_table_custom_fields <set_table_custom_fields>` has been deprecated in
favour of this API.
Set configuration for table/view `` author `` :
.. code-block :: http
POST /v1/query HTTP/1.1
Content-Type: application/json
X-Hasura-Role: admin
{
"type": "set_table_customization",
"args": {
"table": "author_details",
"configuration": {
"identifier": "author",
"custom_root_fields": {
"select": "Authors",
"select_by_pk": "Author",
"select_aggregate": "AuthorAggregate",
"insert": "AddAuthors",
"insert_one":"AddAuthor",
"update": "UpdateAuthors",
"update_by_pk": "UpdateAuthor",
"delete": "DeleteAuthors",
"delete_by_pk": "DeleteAuthor"
},
"custom_column_names": {
"id": "authorId"
}
}
}
}
.. _set_table_customization_syntax:
Args syntax
^^^^^^^^^^^
.. list-table ::
:header-rows: 1
* - Key
- Required
- Schema
- Description
* - table
- true
- :ref: `TableName <TableName>`
- Name of the table
* - configuration
- false
- :ref: `TableConfig <table_config>`
- Configuration for the table/view
2019-09-19 07:47:36 +03:00
2018-10-17 08:27:11 +03:00
.. _untrack_table:
untrack_table
-------------
2019-02-06 09:39:36 +03:00
`` untrack_table `` is used to remove a table/view from the GraphQL schema.
2018-10-17 08:27:11 +03:00
Remove a table/view `` author `` :
.. code-block :: http
POST /v1/query HTTP/1.1
Content-Type: application/json
X-Hasura-Role: admin
{
"type": "untrack_table",
"args": {
"table": {
"schema": "public",
"name": "author"
},
"cascade": true
}
}
2019-08-16 00:19:47 +03:00
2018-10-17 08:27:11 +03:00
.. _untrack_table_syntax:
Args syntax
^^^^^^^^^^^
.. list-table ::
:header-rows: 1
* - Key
- Required
- Schema
- Description
* - table
- true
- :ref: `TableName <TableName>`
- Name of the table
* - cascade
- false
- Boolean
2019-09-11 10:17:14 +03:00
- When set to `` true `` , the effect (if possible) is cascaded to any metadata dependent objects (relationships, permissions, templates)