mirror of
https://github.com/hasura/graphql-engine.git
synced 2024-12-17 04:24:35 +03:00
3ad42ed540
Co-authored-by: Rikin Kachhia <54616969+rikinsk@users.noreply.github.com> GitOrigin-RevId: d5b680ce50bfa331bbead32ffe8ad7ba95a6b51f
145 lines
3.6 KiB
Plaintext
145 lines
3.6 KiB
Plaintext
.. meta::
|
|
:description: Hasura action examples
|
|
:keywords: hasura, docs, actions, examples
|
|
|
|
.. _action_examples:
|
|
|
|
Action examples
|
|
===============
|
|
|
|
.. contents:: Table of contents
|
|
:backlinks: none
|
|
:depth: 1
|
|
:local:
|
|
|
|
Introduction
|
|
------------
|
|
|
|
This page provides reference examples of typical action use cases.
|
|
|
|
..
|
|
Relationships
|
|
-------------
|
|
|
|
Object relationship from an action
|
|
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
|
|
|
Let's say we have the following two tables in our schema:
|
|
|
|
.. code-block:: sql
|
|
|
|
authors (id int, name text, is_active boolean, rating int, address_id)
|
|
|
|
addresses (id int, street text, zip text, city text)
|
|
|
|
Now we have an action ``updateAddress`` that looks as follows:
|
|
|
|
.. thumbnail:: /img/graphql/core/actions/update-address-action-definition.png
|
|
:alt: Update address action
|
|
:width: 65%
|
|
|
|
We can now add an object relationship from the ``updateAddress`` action to the ``authors`` table in our schema.
|
|
We call it ``updatedAddressAuthor``.
|
|
|
|
.. thumbnail:: /img/graphql/core/actions/action-object-relationship.png
|
|
:alt: Object relationship from action
|
|
:width: 65%
|
|
|
|
It's now possible to return the author from the ``updateAddress`` action through the newly created relationship:
|
|
|
|
.. graphiql::
|
|
:view_only:
|
|
:query:
|
|
mutation {
|
|
updateAddress(address:
|
|
{
|
|
id: 1
|
|
street: "New York Avenue"
|
|
zip: "98000"
|
|
city: "New Orleans"
|
|
}
|
|
) {
|
|
id
|
|
updatedAddressAuthor {
|
|
name
|
|
}
|
|
}
|
|
}
|
|
:response:
|
|
{
|
|
"data": {
|
|
"updateAddress": {
|
|
"id": 1,
|
|
"updatedAddressAuthor": {
|
|
"name": "J.K. Rowling"
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
Array relationship from an action
|
|
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
|
|
|
Let's say we have the following two tables in our schema:
|
|
|
|
.. code-block:: sql
|
|
|
|
authors (id int, name text, is_active boolean, rating int)
|
|
|
|
articles (id int, title text, content text, author_id int)
|
|
|
|
Now we have an action ``updateAuthor`` that looks as follows:
|
|
|
|
.. thumbnail:: /img/graphql/core/actions/update-author-action-definition.png
|
|
:alt: Update author action
|
|
:width: 65%
|
|
|
|
We can now add an array relationship from the ``updateAuthor`` action to the ``articles`` table in our schema.
|
|
We call it ``updatedAuthorArticles``.
|
|
|
|
.. thumbnail:: /img/graphql/core/actions/action-array-relationship.png
|
|
:alt: Array relationship from action
|
|
:width: 65%
|
|
|
|
It's now possible to return articles from the ``updateAuthor`` action through the newly created relationship:
|
|
|
|
.. graphiql::
|
|
:view_only:
|
|
:query:
|
|
mutation {
|
|
updateAuthor(author:
|
|
{
|
|
id: 442,
|
|
name: "Joanne K. Rowling",
|
|
is_active: true,
|
|
rating: 10
|
|
}
|
|
) {
|
|
id
|
|
updatedAuthorArticles {
|
|
title
|
|
}
|
|
}
|
|
}
|
|
:response:
|
|
{
|
|
"data": {
|
|
"updateAuthor": {
|
|
"id": 442,
|
|
"updatedAuthorArticles": [
|
|
{
|
|
"title": "Harry Potter and the Philosopher's Stone"
|
|
},
|
|
{
|
|
"title": "Harry Potter and the Chamber of Secrets"
|
|
}
|
|
]
|
|
}
|
|
}
|
|
}
|
|
|
|
.. admonition:: Additional Resources
|
|
|
|
Introduction to Hasura Actions - `View Recording <https://hasura.io/events/webinar/hasura-actions/?pg=docs&plcmt=body&cta=view-recording&tech=>`__.
|
|
|