graphql-engine/docs/graphql/manual/schema/views.rst

75 lines
2.3 KiB
ReStructuredText
Raw Normal View History

.. meta::
:description: Customise the Hasura GraphQL schema with views
:keywords: hasura, docs, schema, view
2019-12-26 15:05:37 +03:00
.. _custom_views:
Customise schema with views
===========================
.. contents:: Table of contents
:backlinks: none
2019-12-26 15:05:37 +03:00
:depth: 2
:local:
2019-12-26 15:05:37 +03:00
What are views?
---------------
2019-12-26 15:05:37 +03:00
`Views <https://www.postgresql.org/docs/current/sql-createview.html>`_ can be used to expose the results of a custom
query as a virtual table. Views are not persisted physically i.e. the query defining a view is executed whenever
data is requested from the view.
2019-12-26 15:05:37 +03:00
Hasura GraphQL engine lets you expose views over the GraphQL API to allow querying them using both ``queries`` and
``subscriptions`` just like regular tables.
2019-12-26 15:05:37 +03:00
.. _create_views:
2019-12-26 15:05:37 +03:00
Creating & exposing views
-------------------------
Views can be created using SQL which can be run in the Hasura console:
- Head to the ``Data -> SQL`` section of the Hasura console
- Enter your `create view SQL statement <https://www.postgresql.org/docs/current/static/sql-createview.html>`__
- Select the ``Track this`` checkbox to expose the new view over the GraphQL API
- Hit the ``Run`` button
2019-12-26 15:05:37 +03:00
Use cases
---------
Views are ideal solutions for retrieving some derived data based on some custom business logic. If your custom logic
requires any user input, you should use :ref:`custom SQL functions <custom_sql_functions>` instead.
Let's see a few example use cases for views:
Example: Group by and then aggregate
************************************
Lets see how to fetch the average article rating for each author in our author/article schema.
A view that averages the rating of articles for each author can be created using the following SQL query:
.. code-block:: SQL
CREATE VIEW author_average_rating AS
SELECT author_id, avg(rating)
FROM article
GROUP BY author_id
Example: Hide certain fields of a table
***************************************
Say, we have some sensitive information in a table which we wouldn't want to expose. We can create a view that only
exposes the non-sensitive fields.
Let's say our ``author`` table has the fields ``id, name, city, email, phone, address`` and we want to hide the ``email``,
``phone`` and ``address`` fields. We can create the following view to achieve this:
2019-12-26 15:05:37 +03:00
.. code-block:: SQL
2019-12-26 15:05:37 +03:00
CREATE VIEW author_public AS
SELECT id, name, city
FROM author