add docs on setting default value for columns (#437)

This commit is contained in:
Rikin Kachhia 2018-09-13 17:10:17 +05:30 committed by Shahidh K Muhammed
parent 5929ec5be3
commit dae3410e4d
6 changed files with 63 additions and 5 deletions

View File

@ -34,7 +34,6 @@ Let's use this reference author/article schema to look at different types of mut
Update <update>
Delete <delete>
multiple-mutations
sql-functions

View File

@ -0,0 +1,51 @@
Setting default values for fields
=================================
Let's say you want certain fields to have their values set automatically by Postgres if
not explicitly passed. For example, an auto-incrementing id, a created_at timestamp, etc.
We can achieve this by setting a default value for the field which could either be a fixed value or a simple sql
function.
**Example:** Say we have a field ``created_at`` in a table ``article`` which we would want to be set to the current
timestamp whenever a new row is added to the table.
1) Modify the table
-------------------
Edit the ``created_at`` field and set its Default value as the SQL function ``now()``.
Open the console and head to ``Data -> article -> Modify``:
.. image:: ../../../img/graphql/manual/schema/add-default-value.png
.. admonition:: To set an auto-incrementing default value
To set a default value as an auto-incrementing integer you first need to set up a ``sequence`` which will be the
source of our default value.
Let's say we have a field called ``roll_number`` which we would like to be set by default as an auto-incremented
integer.
Head to ``Data -> SQL`` and run the following SQL command to create a new sequence.
.. code-block:: SQL
CREATE SEQUENCE roll_number_seq;
Now set the default value of the ``roll_number`` field as ``nextval('roll_number_seq')``
2) Run an insert mutation
-------------------------
Now if you do not pass the ``created_at`` field value while running an insert mutation on the ``article`` table, its
value will be set automatically by Postgres.
.. image:: ../../../img/graphql/manual/schema/default-value-response.png
.. note::
The default value is ignored when a value is explicity set to the field. To enforce the value set in a field is the
result of the defined SQL function, see: :doc:`sql-functions`

View File

@ -23,6 +23,8 @@ See:
Customise with schema stitching <schema-stitching>
Adding custom resolvers <custom-resolvers>
Enum type fields <enums>
Default field values <default-values>
Set values using SQL functions/stored procedures <sql-functions>
Using an existing database <using-existing-database>
Export GraphQL schema <export-graphql-schema>
How schema generation works <how-it-works>

View File

@ -1,11 +1,12 @@
Set values using SQL functions/stored procedures
================================================
Setting values using SQL functions/stored procedures
====================================================
Let's say you want to set the value of some fields as the output of some custom SQL functions or stored procedures.
This can be achieved by:
#. Modifying the table to allow the columns we want to be set by the SQL functions as nullable.
#. Modifying the table to allow the columns we want to be set by the SQL functions as nullable (to allow the initial
insert before the SQL function is run).
#. Creating an insert/update trigger on the table that calls your SQL function and sets the output values in the output
columns.
#. Making your mutation requests without setting the SQL function output columns.
@ -18,7 +19,7 @@ to set the value of the ``output`` column as the uppercased value of the string
Modify the table ``sql_function_table`` and make its ``output`` column nullable.
Head to ``Data -> sql_function_table -> Modify``
Open the console and head to ``Data -> sql_function_table -> Modify``
.. image:: ../../../img/graphql/manual/schema/modify-sql-fn-table.png
@ -78,3 +79,8 @@ value (output="YABBA DABBA DOO!") will be set automatically.
}
}
}
.. note::
This approach enforces the value set in the field to always be the result of the defined SQL function unlike
:doc:`setting a default value <default-values>` instead.

Binary file not shown.

After

Width:  |  Height:  |  Size: 12 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 14 KiB