mirror of
https://github.com/hasura/graphql-engine.git
synced 2024-12-15 09:22:43 +03:00
update create relationship docs (#1481)
This commit is contained in:
parent
31050af32c
commit
f5bf7c961b
@ -3,7 +3,7 @@ Creating relationships
|
||||
|
||||
.. contents:: Table of contents
|
||||
:backlinks: none
|
||||
:depth: 1
|
||||
:depth: 2
|
||||
:local:
|
||||
|
||||
A relationship from one table/view to another can be created by defining a link between a column of the table/view to a
|
||||
@ -18,26 +18,27 @@ as foreign-keys can't be created on views.
|
||||
Using Foreign Keys
|
||||
------------------
|
||||
|
||||
Say we created two tables, ``author(id, name)`` and ``article(id, title, content, rating, author_id)``.
|
||||
|
||||
Say we created two tables, ``author(id, name)`` and ``article(id, title, content, rating, author_id)``. Let us now
|
||||
connect these tables to enable nested queries using a foreign-key:
|
||||
Let us now connect these tables to enable nested queries using a foreign-key:
|
||||
|
||||
1) Add foreign-key constraint
|
||||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
Step 1: Add foreign-key constraint
|
||||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
|
||||
In the console, navigate to the ``Modify`` tab of the ``article`` table. Edit the ``author_id`` column and configure
|
||||
it as a foreign-key for the ``id`` column in the ``author`` table:
|
||||
|
||||
.. image:: ../../../../img/graphql/manual/schema/add-foreign-key.png
|
||||
|
||||
2) Create an object relationship
|
||||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
Step 2: Create an object relationship
|
||||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
|
||||
Each article has one author. This is an ``object relationship``. The console
|
||||
infers this using the foreign-key and recommends the potential relationship in the ``Relationships`` tab
|
||||
of the ``article`` table.
|
||||
Each article has one author. This is an ``object relationship``.
|
||||
|
||||
Add an ``object relationship`` named ``author`` as shown here:
|
||||
The console infers this using the foreign-key created above and recommends the potential relationship in the
|
||||
``Relationships`` tab of the ``article`` table.
|
||||
|
||||
Add an ``object relationship`` named ``author`` for the ``article`` table as shown here:
|
||||
|
||||
.. image:: ../../../../img/graphql/manual/schema/add-1-1-relationship.png
|
||||
|
||||
@ -90,12 +91,14 @@ Fetch a list of articles and each article's author:
|
||||
}
|
||||
}
|
||||
|
||||
3) Create an array relationship
|
||||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
Step 3: Create an array relationship
|
||||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
|
||||
An author can write multiple articles. This is an ``array relationship``.
|
||||
|
||||
You can add an ``array relationship`` exactly how you added an ``object relationship`` as shown above:
|
||||
You can add an ``array relationship`` in the same fashion as an ``object relationship`` as shown above.
|
||||
|
||||
Add an ``array relationship`` named ``articles`` for the ``author`` table as shown here:
|
||||
|
||||
.. image:: ../../../../img/graphql/manual/schema/add-1-many-relationship.png
|
||||
|
||||
@ -178,21 +181,32 @@ Using Manual Relationships
|
||||
Let's say you have a table ``author(id, name)`` and a view ``author_avg_rating(id, avg)`` which has the
|
||||
average rating of articles for each author.
|
||||
|
||||
To create an ``object relationship`` called ``avg_rating`` from the ``author`` table to the ``author_avg_rating`` view,
|
||||
navigate to the ``Relationships`` tab of the ``author`` table in the console and click on the ``+ Add a manual
|
||||
relationship`` button:
|
||||
Let us now create an ``object relationship`` called ``avg_rating`` from the ``author`` table to the
|
||||
``author_avg_rating`` view using a manual relationship:
|
||||
|
||||
Step 1: Open manual relationship section
|
||||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
|
||||
- Open the console and navigate to the ``Data -> author -> Relationships`` tab.
|
||||
- Click on the ``+ Add a manual relationship`` button:
|
||||
|
||||
.. image:: ../../../../img/graphql/manual/schema/manual-relationship-btn.png
|
||||
|
||||
This will open up a section as shown below:
|
||||
Step 2: Define the relationship
|
||||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
|
||||
The above step will open up a section as shown below:
|
||||
|
||||
.. image:: ../../../../img/graphql/manual/schema/manual-relationship-create.png
|
||||
|
||||
For our case:
|
||||
In this case:
|
||||
|
||||
- Relationship Type will be: ``Object Relationship``
|
||||
- Relationship Name can be: ``avg_rating``
|
||||
- Configuration: ``id :: author_avg_rating -> id``
|
||||
- **Relationship Type** will be: ``Object Relationship``
|
||||
- **Relationship Name** can be: ``avg_rating``
|
||||
- **Configuration** will be: ``id :: author_avg_rating -> id`` *(similar to defining a foreign-key)*
|
||||
|
||||
Step 3: Create the relationship
|
||||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
|
||||
Now click on the ``Add`` button to create the relationship.
|
||||
|
||||
@ -239,4 +253,4 @@ Fetch a list of authors with the average rating of their articles:
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user