2020-01-14 15:57:45 +03:00
|
|
|
.. meta::
|
|
|
|
:description: Set default field values using role-based column presets
|
|
|
|
:keywords: hasura, docs, schema, default value, role-based, column preset
|
|
|
|
|
2020-03-11 22:42:36 +03:00
|
|
|
.. _column_presets:
|
|
|
|
|
2019-09-11 10:17:14 +03:00
|
|
|
Setting default values for fields using role-based column presets
|
2018-11-06 11:37:17 +03:00
|
|
|
=================================================================
|
|
|
|
|
2018-12-03 15:12:24 +03:00
|
|
|
.. contents:: Table of contents
|
|
|
|
:backlinks: none
|
|
|
|
:depth: 1
|
|
|
|
:local:
|
|
|
|
|
2018-11-06 11:37:17 +03:00
|
|
|
Let's say you want certain fields to have their values set automatically when not explicitly passed using session
|
2020-03-11 22:42:36 +03:00
|
|
|
variables or fixed values when a new row is created with a particular :ref:`user role <roles_variables>`.
|
2018-11-06 11:37:17 +03:00
|
|
|
|
2019-02-06 09:39:36 +03:00
|
|
|
Hasura GraphQL engine's column presets let you define role-based default values for any field/column. These values
|
|
|
|
can either be a session variable value or a static value.
|
2018-11-06 11:37:17 +03:00
|
|
|
|
|
|
|
.. admonition:: Column preset restricts mutation access for configured role
|
|
|
|
|
|
|
|
If a column has a preset defined for a given role, access to the column for mutations will be restricted for users
|
2019-02-06 09:39:36 +03:00
|
|
|
with that role.
|
2018-11-06 11:37:17 +03:00
|
|
|
|
|
|
|
**Example:** Say we have a field ``user_id`` in a table ``article`` which is to be set to the id of the user, from
|
2019-02-06 09:39:36 +03:00
|
|
|
the value of the user's session variable whenever a new row is added to the ``article`` table.
|
2018-11-06 11:37:17 +03:00
|
|
|
|
2018-12-03 15:12:24 +03:00
|
|
|
Step 1: Configure a column preset
|
|
|
|
---------------------------------
|
|
|
|
|
2018-11-06 11:37:17 +03:00
|
|
|
The column preset option is available under the ``Permissions`` tab of a table. Open the console and head to
|
|
|
|
``Data -> article -> Permissions``:
|
|
|
|
|
2019-03-13 13:03:45 +03:00
|
|
|
.. thumbnail:: ../../../../img/graphql/manual/schema/column-presets-option.png
|
2020-01-08 16:20:18 +03:00
|
|
|
:alt: Add a column preset in the permissions tab
|
2018-11-06 11:37:17 +03:00
|
|
|
|
|
|
|
Enable the column preset option to define presets for one or more columns. For each column, you can pick between
|
|
|
|
setting the preset using a static value or from a session variable.
|
|
|
|
|
2019-03-13 13:03:45 +03:00
|
|
|
.. thumbnail:: ../../../../img/graphql/manual/schema/column-presets-value-options.png
|
2020-01-08 16:20:18 +03:00
|
|
|
:alt: Configure the column preset
|
2018-11-06 11:37:17 +03:00
|
|
|
|
|
|
|
For our chosen example, we'll use the ``from session variable`` option and configure the ``user_id`` column to be
|
2019-09-11 10:17:14 +03:00
|
|
|
automatically populated based on the value of the ``X-Hasura-User-Id`` session variable.
|
2019-02-06 09:39:36 +03:00
|
|
|
|
|
|
|
.. note::
|
|
|
|
|
|
|
|
To set a column preset for a nested object's column, simply set the corresponding column preset in the remote
|
|
|
|
table.
|
2018-11-06 11:37:17 +03:00
|
|
|
|
2018-12-03 15:12:24 +03:00
|
|
|
Step 2: Run an insert mutation
|
|
|
|
------------------------------
|
2018-11-06 11:37:17 +03:00
|
|
|
|
|
|
|
Head to the GraphiQL interface in the console and try making an insert mutation on the ``article`` table with the
|
|
|
|
following headers (*to run through this example, don't forget to also grant the* ``user`` *role sufficient permissions
|
|
|
|
to select from the* ``article`` *table*):
|
|
|
|
|
2019-02-06 09:39:36 +03:00
|
|
|
- ``X-Hasura-Role`` --> ``user`` (*to test the behaviour for the configured role*)
|
2018-11-06 11:37:17 +03:00
|
|
|
- ``X-Hasura-User-Id`` --> ``1`` (*this is the value we should expect in the* ``user_id`` *field*)
|
|
|
|
|
2019-02-06 09:39:36 +03:00
|
|
|
As mentioned earlier, you'll notice when you add the ``X-Hasura-Role`` header that the field, ``user_id``, is no longer
|
2018-11-06 11:37:17 +03:00
|
|
|
available as the mutation type's field:
|
|
|
|
|
2019-03-13 13:03:45 +03:00
|
|
|
.. thumbnail:: ../../../../img/graphql/manual/schema/column-preset-schema-change-for-role.png
|
2020-01-08 16:20:18 +03:00
|
|
|
:alt: Write an insert mutation
|
2018-11-06 11:37:17 +03:00
|
|
|
|
|
|
|
Now, if we run the following insert mutation, we'll see that the ``user_id`` field is indeed being set with the value
|
2019-02-06 09:39:36 +03:00
|
|
|
passed in the ``X-Hasura-User-Id`` variable:
|
2018-11-06 11:37:17 +03:00
|
|
|
|
2019-03-13 13:03:45 +03:00
|
|
|
.. thumbnail:: ../../../../img/graphql/manual/schema/column-preset-mutation-result.png
|
2020-01-08 16:20:18 +03:00
|
|
|
:alt: Run the insert mutation
|
2018-11-06 11:37:17 +03:00
|
|
|
|
|
|
|
.. note::
|
|
|
|
|
|
|
|
Not passing the configured header will result in a run-time error:
|
|
|
|
|
|
|
|
.. code-block:: JSON
|
|
|
|
|
|
|
|
{
|
|
|
|
"errors": [
|
|
|
|
{
|
|
|
|
"path": "$",
|
|
|
|
"error": "\"x-hasura-user-id\" header is expected but not found",
|
|
|
|
"code": "not-found"
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-12-03 15:12:24 +03:00
|
|
|
Also see
|
|
|
|
--------
|
2018-11-06 11:37:17 +03:00
|
|
|
|
2020-03-11 22:42:36 +03:00
|
|
|
- :ref:`postgres_defaults`
|
|
|
|
- :ref:`sql_functions_as_default`
|