Skip to content

Commit

Permalink
add docs on setting default value for columns (#437)
Browse files Browse the repository at this point in the history
  • Loading branch information
rikinsk authored and shahidhk committed Sep 13, 2018
1 parent 5929ec5 commit dae3410
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 5 deletions.
1 change: 0 additions & 1 deletion docs/graphql/manual/mutations/index.rst
Expand Up @@ -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



Expand Down
51 changes: 51 additions & 0 deletions docs/graphql/manual/schema/default-values.rst
@@ -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`
2 changes: 2 additions & 0 deletions docs/graphql/manual/schema/index.rst
Expand Up @@ -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>
@@ -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.
Expand All @@ -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

Expand Down Expand Up @@ -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.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit dae3410

Please sign in to comment.