2020-01-14 15:57:45 +03:00
|
|
|
.. meta::
|
|
|
|
:description: Manage computed fields with the Hasura schema/metadata API
|
|
|
|
:keywords: hasura, docs, schema/metadata API, API reference, computed field
|
|
|
|
|
2022-01-10 21:39:15 +03:00
|
|
|
.. _schema_metadata_api_computed_field:
|
2020-03-11 22:42:36 +03:00
|
|
|
|
2021-08-13 16:45:03 +03:00
|
|
|
Schema/Metadata API Reference: Computed Fields (Deprecated)
|
|
|
|
===========================================================
|
2019-10-18 11:29:47 +03:00
|
|
|
|
2022-02-16 21:33:05 +03:00
|
|
|
.. admonition:: Deprecation
|
|
|
|
|
|
|
|
In versions ``v2.0.0`` and above, the schema/metadata API is deprecated in favour of the :ref:`schema API <schema_apis>` and the
|
|
|
|
:ref:`metadata API <metadata_apis>`.
|
|
|
|
|
|
|
|
Though for backwards compatibility, the schema/metadata APIs will continue to function.
|
|
|
|
|
2019-10-18 11:29:47 +03:00
|
|
|
.. contents:: Table of contents
|
|
|
|
:backlinks: none
|
|
|
|
:depth: 1
|
|
|
|
:local:
|
|
|
|
|
2020-08-25 14:53:25 +03:00
|
|
|
Introduction
|
|
|
|
------------
|
|
|
|
|
2019-10-18 11:29:47 +03:00
|
|
|
**computed field** is an extra field added to a table, its value is
|
|
|
|
computed via an SQL function which has the table row type as an input argument.
|
|
|
|
Currenty, the Hasura GraphQL engine supports functions returning
|
|
|
|
`base types <https://www.postgresql.org/docs/current/extend-type-system.html#id-1.8.3.5.9>`__ or
|
|
|
|
`table row types <https://www.postgresql.org/docs/current/rowtypes.html#ROWTYPES-DECLARING>`__
|
|
|
|
as computed fields.
|
|
|
|
|
2022-01-10 21:39:15 +03:00
|
|
|
.. _schema_metadata_add_computed_field:
|
2019-10-18 11:29:47 +03:00
|
|
|
|
|
|
|
add_computed_field
|
|
|
|
------------------
|
|
|
|
|
|
|
|
``add_computed_field`` is used to define a computed field in a table.
|
|
|
|
There cannot be an existing column or relationship or computed field with
|
|
|
|
the same name.
|
|
|
|
|
2020-01-13 10:11:30 +03:00
|
|
|
Create a ``computed field`` called ``full_name`` on an ``author`` *table*, using
|
|
|
|
an SQL function called ``author_full_name``:
|
2019-10-18 11:29:47 +03:00
|
|
|
|
|
|
|
.. code-block:: http
|
|
|
|
|
|
|
|
POST /v1/query HTTP/1.1
|
|
|
|
Content-Type: application/json
|
|
|
|
X-Hasura-Role: admin
|
|
|
|
|
|
|
|
{
|
|
|
|
"type":"add_computed_field",
|
|
|
|
"args":{
|
|
|
|
"table":{
|
|
|
|
"name":"author",
|
|
|
|
"schema":"public"
|
|
|
|
},
|
2020-01-13 10:11:30 +03:00
|
|
|
"name":"full_name",
|
2019-10-18 11:29:47 +03:00
|
|
|
"definition":{
|
|
|
|
"function":{
|
2020-01-13 10:11:30 +03:00
|
|
|
"name":"author_full_name",
|
2019-10-18 11:29:47 +03:00
|
|
|
"schema":"public"
|
|
|
|
},
|
|
|
|
"table_argument":"author_row"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-10 21:39:15 +03:00
|
|
|
.. _schema_metadata_add_computed_field_syntax:
|
2019-10-18 11:29:47 +03:00
|
|
|
|
|
|
|
Args syntax
|
|
|
|
^^^^^^^^^^^
|
|
|
|
|
|
|
|
.. list-table::
|
|
|
|
:header-rows: 1
|
|
|
|
|
|
|
|
* - Key
|
|
|
|
- Required
|
|
|
|
- Schema
|
|
|
|
- Description
|
|
|
|
* - table
|
|
|
|
- true
|
|
|
|
- :ref:`TableName <TableName>`
|
|
|
|
- Name of the table
|
|
|
|
* - name
|
|
|
|
- true
|
|
|
|
- :ref:`ComputedFieldName <ComputedFieldName>`
|
|
|
|
- Name of the new computed field
|
|
|
|
* - definition
|
|
|
|
- true
|
2021-02-24 15:30:34 +03:00
|
|
|
- :ref:`ComputedFieldDefinition`
|
2019-10-18 11:29:47 +03:00
|
|
|
- The computed field definition
|
|
|
|
* - comment
|
|
|
|
- false
|
|
|
|
- text
|
|
|
|
- comment
|
|
|
|
|
2022-01-10 21:39:15 +03:00
|
|
|
.. _schema_metadata_drop_computed_field:
|
2019-10-18 11:29:47 +03:00
|
|
|
|
|
|
|
drop_computed_field
|
|
|
|
-------------------
|
|
|
|
|
|
|
|
``drop_computed_field`` is used to drop a computed field of a table. If
|
2020-06-24 20:30:08 +03:00
|
|
|
there are other objects dependent on this computed field, like permissions, the request will fail and report the
|
2020-01-13 10:11:30 +03:00
|
|
|
dependencies unless ``cascade`` is set to ``true``. If ``cascade`` is set to ``true``, the dependent objects
|
2019-10-18 11:29:47 +03:00
|
|
|
are also dropped.
|
|
|
|
|
2020-01-13 10:11:30 +03:00
|
|
|
Drop a computed field ``full_name`` from a table ``author``:
|
2019-10-18 11:29:47 +03:00
|
|
|
|
|
|
|
.. code-block:: http
|
|
|
|
|
|
|
|
POST /v1/query HTTP/1.1
|
|
|
|
Content-Type: application/json
|
|
|
|
X-Hasura-Role: admin
|
|
|
|
|
|
|
|
{
|
|
|
|
"type":"drop_computed_field",
|
|
|
|
"args":{
|
|
|
|
"table":{
|
|
|
|
"name":"author",
|
|
|
|
"schema":"public"
|
|
|
|
},
|
2020-01-13 10:11:30 +03:00
|
|
|
"name":"full_name",
|
2019-10-18 11:29:47 +03:00
|
|
|
"cascade": false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-10 21:39:15 +03:00
|
|
|
.. _schema_metadata_drop_computed_field_syntax:
|
2019-10-18 11:29:47 +03:00
|
|
|
|
|
|
|
Args syntax
|
|
|
|
^^^^^^^^^^^
|
|
|
|
|
|
|
|
.. list-table::
|
|
|
|
:header-rows: 1
|
|
|
|
|
|
|
|
* - Key
|
|
|
|
- Required
|
|
|
|
- Schema
|
|
|
|
- Description
|
|
|
|
* - table
|
|
|
|
- true
|
|
|
|
- :ref:`TableName <TableName>`
|
|
|
|
- Name of the table
|
|
|
|
* - name
|
|
|
|
- true
|
|
|
|
- :ref:`ComputedFieldName <ComputedFieldName>`
|
|
|
|
- Name of the computed field
|
|
|
|
* - cascade
|
|
|
|
- false
|
|
|
|
- Boolean
|
|
|
|
- When set to ``true``, all the dependent items (if any) on this computed fields are also dropped
|