2020-01-14 15:57:45 +03:00
|
|
|
.. meta::
|
|
|
|
:description: Manage custom functions with the Hasura schema/metadata API
|
|
|
|
:keywords: hasura, docs, schema/metadata API, API reference, custom function
|
|
|
|
|
2020-03-11 22:42:36 +03:00
|
|
|
.. _api_custom_functions:
|
|
|
|
|
2019-01-25 06:31:54 +03:00
|
|
|
Schema/Metadata API Reference: Custom Functions
|
|
|
|
===============================================
|
|
|
|
|
|
|
|
.. contents:: Table of contents
|
|
|
|
:backlinks: none
|
|
|
|
:depth: 1
|
|
|
|
:local:
|
|
|
|
|
2019-09-11 10:17:14 +03:00
|
|
|
Track/untrack a custom SQL function in the Hasura GraphQL engine.
|
2019-01-25 06:31:54 +03:00
|
|
|
|
2019-02-06 09:39:36 +03:00
|
|
|
Only tracked custom functions are available for querying/mutating/subscribing data over the GraphQL API.
|
2019-01-25 06:31:54 +03:00
|
|
|
|
|
|
|
.. _track_function:
|
|
|
|
|
|
|
|
track_function
|
|
|
|
--------------
|
|
|
|
|
2019-02-06 09:39:36 +03:00
|
|
|
``track_function`` is used to add a custom SQL function to the GraphQL schema.
|
2019-11-20 09:47:06 +03:00
|
|
|
Also refer a note :ref:`here <note>`.
|
2019-01-25 06:31:54 +03:00
|
|
|
|
2019-09-11 10:17:14 +03:00
|
|
|
Add an SQL function ``search_articles``:
|
2019-01-25 06:31:54 +03:00
|
|
|
|
|
|
|
.. code-block:: http
|
|
|
|
|
|
|
|
POST /v1/query HTTP/1.1
|
|
|
|
Content-Type: application/json
|
|
|
|
X-Hasura-Role: admin
|
|
|
|
|
|
|
|
{
|
|
|
|
"type": "track_function",
|
|
|
|
"args": {
|
|
|
|
"schema": "public",
|
|
|
|
"name": "search_articles"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-20 09:47:06 +03:00
|
|
|
.. _track_function_v2:
|
|
|
|
|
|
|
|
track_function v2
|
|
|
|
-----------------
|
|
|
|
|
|
|
|
Version 2 of ``track_function`` is used to add a custom SQL function to the GraphQL schema with configuration.
|
|
|
|
Also refer a note :ref:`here <note>`.
|
|
|
|
|
|
|
|
Add an SQL function called ``search_articles`` with a Hasura session argument.
|
|
|
|
|
|
|
|
.. code-block:: http
|
|
|
|
|
|
|
|
POST /v1/query HTTP/1.1
|
|
|
|
Content-Type: application/json
|
|
|
|
X-Hasura-Role: admin
|
|
|
|
|
|
|
|
{
|
|
|
|
"type": "track_function",
|
|
|
|
"version": 2,
|
|
|
|
"args": {
|
|
|
|
"function": {
|
|
|
|
"schema": "public",
|
|
|
|
"name": "search_articles"
|
|
|
|
},
|
|
|
|
"configuration": {
|
|
|
|
"session_argument": "hasura_session"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
.. _track_function_args_syntax_v2:
|
|
|
|
|
|
|
|
Args syntax
|
|
|
|
^^^^^^^^^^^
|
|
|
|
|
|
|
|
.. list-table::
|
|
|
|
:header-rows: 1
|
|
|
|
|
|
|
|
* - Key
|
|
|
|
- Required
|
|
|
|
- Schema
|
|
|
|
- Description
|
|
|
|
* - function
|
|
|
|
- true
|
|
|
|
- :ref:`FunctionName <FunctionName>`
|
|
|
|
- Name of the SQL function
|
|
|
|
* - configuration
|
|
|
|
- false
|
|
|
|
- :ref:`Function Configuration <function_configuration>`
|
|
|
|
- Configuration for the SQL function
|
|
|
|
|
|
|
|
.. _function_configuration:
|
|
|
|
|
|
|
|
Function Configuration
|
|
|
|
^^^^^^^^^^^^^^^^^^^^^^
|
|
|
|
|
|
|
|
.. list-table::
|
|
|
|
:header-rows: 1
|
|
|
|
|
|
|
|
* - Key
|
|
|
|
- Required
|
|
|
|
- Schema
|
|
|
|
- Description
|
|
|
|
* - session_argument
|
|
|
|
- false
|
|
|
|
- `String`
|
|
|
|
- Function argument which accepts session info JSON
|
|
|
|
|
|
|
|
.. _note:
|
|
|
|
|
|
|
|
.. note::
|
|
|
|
|
|
|
|
Currently, only functions which satisfy the following constraints can be exposed over the GraphQL API
|
|
|
|
(*terminology from* `Postgres docs <https://www.postgresql.org/docs/current/sql-createfunction.html>`__):
|
|
|
|
|
|
|
|
- **Function behaviour**: ONLY ``STABLE`` or ``IMMUTABLE``
|
|
|
|
- **Return type**: MUST be ``SETOF <table-name>``
|
|
|
|
- **Argument modes**: ONLY ``IN``
|
|
|
|
|
2019-01-25 06:31:54 +03:00
|
|
|
.. _untrack_function:
|
|
|
|
|
|
|
|
untrack_function
|
|
|
|
----------------
|
|
|
|
|
2019-02-06 09:39:36 +03:00
|
|
|
``untrack_function`` is used to remove a SQL function from the GraphQL schema.
|
2019-01-25 06:31:54 +03:00
|
|
|
|
2019-09-11 10:17:14 +03:00
|
|
|
Remove an SQL function ``search_articles``:
|
2019-01-25 06:31:54 +03:00
|
|
|
|
|
|
|
.. code-block:: http
|
|
|
|
|
|
|
|
POST /v1/query HTTP/1.1
|
|
|
|
Content-Type: application/json
|
|
|
|
X-Hasura-Role: admin
|
|
|
|
|
|
|
|
{
|
|
|
|
"type": "untrack_function",
|
|
|
|
"args": {
|
|
|
|
"schema": "public",
|
|
|
|
"name": "search_articles"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
.. _args_syntax:
|
|
|
|
|
|
|
|
Args syntax
|
|
|
|
^^^^^^^^^^^
|
|
|
|
|
|
|
|
.. list-table::
|
|
|
|
:header-rows: 1
|
|
|
|
|
|
|
|
* - Key
|
|
|
|
- Required
|
|
|
|
- Schema
|
|
|
|
- Description
|
|
|
|
* - table
|
|
|
|
- true
|
|
|
|
- :ref:`FunctionName <FunctionName>`
|
|
|
|
- Name of the SQL function
|