mirror of
https://github.com/hasura/graphql-engine.git
synced 2024-12-25 00:13:11 +03:00
615922b63a
### Description This PR pluralises all sample query names in all sections of the docs, and these changes affect some schema, images, CLI commands and Apis too. _A warning was also fixed in the API reference section._ ### Changelog - [x] `CHANGELOG.md` is updated with user-facing content relevant to this PR. If no changelog is required, then add the `no-changelog-required` label. ### Affected components - [x] Docs ### Related Issues https://github.com/hasura/graphql-engine-internal/issues/75 ### Affected pages **Getting Started:** 1. https://deploy-preview-312--hasura-docs-mono.netlify.app/graphql/core/getting-started/first-graphql-query.html **Schema:** 1. https://deploy-preview-312--hasura-docs-mono.netlify.app/graphql/core/schema/tables.html 2. https://deploy-preview-312--hasura-docs-mono.netlify.app/graphql/core/schema/table-relationships/create.html 3. https://deploy-preview-312--hasura-docs-mono.netlify.app/graphql/core/schema/table-relationships/rename.html 4. https://deploy-preview-312--hasura-docs-mono.netlify.app/graphql/core/schema/custom-functions.html 5. https://deploy-preview-312--hasura-docs-mono.netlify.app/graphql/core/schema/computed-fields.html **Queries:** 1. https://deploy-preview-312--hasura-docs-mono.netlify.app/graphql/core/queries/simple-object-queries.html 2. https://deploy-preview-312--hasura-docs-mono.netlify.app/graphql/core/queries/nested-object-queries.html 3. https://deploy-preview-312--hasura-docs-mono.netlify.app/graphql/core/queries/aggregation-queries.html 4. https://deploy-preview-312--hasura-docs-mono.netlify.app/graphql/core/queries/query-filters.html 5. https://deploy-preview-312--hasura-docs-mono.netlify.app/graphql/core/queries/sorting.html 6. https://deploy-preview-312--hasura-docs-mono.netlify.app/graphql/core/queries/distinct-queries.html 7. https://deploy-preview-312--hasura-docs-mono.netlify.app/graphql/core/queries/pagination.html 8. https://deploy-preview-312--hasura-docs-mono.netlify.app/graphql/core/queries/multiple-arguments.html 9. https://deploy-preview-312--hasura-docs-mono.netlify.app/graphql/core/queries/multiple-queries.html **Authentication/Authorization:** 1. https://deploy-preview-312--hasura-docs-mono.netlify.app/graphql/core/auth/authorization/basics.html# **Data Modelling Guides** 1. https://deploy-preview-312--hasura-docs-mono.netlify.app/graphql/core/guides/data-modelling/one-to-one.html 2. https://deploy-preview-312--hasura-docs-mono.netlify.app/graphql/core/guides/data-modelling/one-to-many.html 3. https://deploy-preview-312--hasura-docs-mono.netlify.app/graphql/core/guides/data-modelling/many-to-many.html GitOrigin-RevId: e02e279466909e0bbd48d908b1b6fa0a5d5e47cf
558 lines
13 KiB
ReStructuredText
558 lines
13 KiB
ReStructuredText
.. meta::
|
|
:description: Model many-to-many relationships in Hasura
|
|
:keywords: hasura, docs, schema, relationship, many-to-many, n-m
|
|
|
|
.. _many_to_many_modelling:
|
|
|
|
Modelling many-to-many table relationships
|
|
==========================================
|
|
|
|
.. contents:: Table of contents
|
|
:backlinks: none
|
|
:depth: 1
|
|
:local:
|
|
|
|
Introduction
|
|
------------
|
|
|
|
A ``many-to-many`` relationship between two tables can be established by creating a table typically called as
|
|
**bridge/junction/join table** and adding **foreign-key constraints** from it to the original tables.
|
|
|
|
Say we have the following two tables in our database schema:
|
|
|
|
.. code-block:: sql
|
|
|
|
articles (
|
|
id SERIAL PRIMARY KEY,
|
|
title TEXT
|
|
...
|
|
)
|
|
|
|
tags (
|
|
id SERIAL PRIMARY KEY,
|
|
tag_value TEXT
|
|
...
|
|
)
|
|
|
|
These two tables are related via a ``many-to-many`` relationship. i.e:
|
|
|
|
- an ``article`` can have many ``tags``
|
|
- a ``tag`` has many ``articles``
|
|
|
|
Step 1: Set up a table relationship in the database
|
|
---------------------------------------------------
|
|
|
|
This ``many-to-many`` relationship can be established in the database by:
|
|
|
|
1. Creating a **bridge table** called ``article_tag`` with the following structure:
|
|
|
|
.. code-block:: sql
|
|
|
|
article_tag (
|
|
article_id INT
|
|
tag_id INT
|
|
PRIMARY KEY (article_id, tag_id)
|
|
...
|
|
)
|
|
|
|
2. Adding **foreign key constraints** from the ``article_tag`` table to:
|
|
|
|
- the ``articles`` table using the ``article_id`` and ``id`` columns of the tables respectively
|
|
- the ``tags`` table using the ``tag_id`` and ``id`` columns of the tables respectively
|
|
|
|
|
|
The table ``article_tag`` sits between the two tables involved in the many-to-many relationship and captures possible
|
|
permutations of their association via the foreign keys.
|
|
|
|
Step 2: Set up GraphQL relationships
|
|
------------------------------------
|
|
|
|
To access the nested objects via the GraphQL API, :ref:`create the following relationships <create_relationships>`:
|
|
|
|
- Array relationship, ``article_tags`` from ``articles`` table using ``article_tag :: article_id -> id``
|
|
- Object relationship, ``tag`` from ``article_tag`` table using ``tag_id -> tags :: id``
|
|
- Array relationship, ``tag_articles`` from ``tags`` table using ``article_tag :: tag_id -> id``
|
|
- Object relationship, ``article`` from ``article_tag`` table using ``article_id -> articles :: id``
|
|
|
|
Query using many-to-many relationships
|
|
--------------------------------------
|
|
|
|
We can now:
|
|
|
|
- fetch a list of ``articles`` with their ``tags``:
|
|
|
|
.. graphiql::
|
|
:view_only:
|
|
:query:
|
|
query {
|
|
articles {
|
|
id
|
|
title
|
|
article_tags {
|
|
tag {
|
|
id
|
|
tag_value
|
|
}
|
|
}
|
|
}
|
|
}
|
|
:response:
|
|
{
|
|
"data": {
|
|
"articles": [
|
|
{
|
|
"id": 1,
|
|
"title": "sit amet",
|
|
"article_tags": [
|
|
{
|
|
"tag": {
|
|
"id": 1,
|
|
"tag_value": "mystery"
|
|
}
|
|
},
|
|
{
|
|
"tag": {
|
|
"id": 2,
|
|
"tag_value": "biography"
|
|
}
|
|
}
|
|
]
|
|
},
|
|
{
|
|
"id": 2,
|
|
"title": "a nibh",
|
|
"article_tags": [
|
|
{
|
|
"tag": {
|
|
"id": 2,
|
|
"tag_value": "biography"
|
|
}
|
|
},
|
|
{
|
|
"tag": {
|
|
"id": 5,
|
|
"tag_value": "technology"
|
|
}
|
|
}
|
|
]
|
|
}
|
|
]
|
|
}
|
|
}
|
|
|
|
- fetch a list of ``tags`` with their ``articles``:
|
|
|
|
.. graphiql::
|
|
:view_only:
|
|
:query:
|
|
query {
|
|
tags {
|
|
id
|
|
tag_value
|
|
tag_articles {
|
|
article {
|
|
id
|
|
title
|
|
}
|
|
}
|
|
}
|
|
}
|
|
:response:
|
|
{
|
|
"data": {
|
|
"tags": [
|
|
{
|
|
"id": 1,
|
|
"tag_value": "mystery",
|
|
"tag_articles": [
|
|
{
|
|
"article": {
|
|
"id": 1,
|
|
"title": "sit amet"
|
|
}
|
|
}
|
|
]
|
|
},
|
|
{
|
|
"id": 2,
|
|
"tag_value": "biography",
|
|
"tag_articles": [
|
|
{
|
|
"article": {
|
|
"id": 1,
|
|
"title": "sit amet"
|
|
}
|
|
},
|
|
{
|
|
"article": {
|
|
"id": 2,
|
|
"title": "a nibh"
|
|
}
|
|
}
|
|
]
|
|
}
|
|
]
|
|
}
|
|
}
|
|
|
|
|
|
Insert using many-to-many relationships
|
|
---------------------------------------
|
|
|
|
We can now:
|
|
|
|
- insert an ``article`` with ``tags`` where the ``tag`` might already exist (assume unique ``value`` for ``tag``):
|
|
|
|
.. graphiql::
|
|
:view_only:
|
|
:query:
|
|
mutation insertArticleWithTags {
|
|
insert_article(objects: [
|
|
{
|
|
title: "Article 1",
|
|
content: "Article 1 content",
|
|
author_id: 1,
|
|
article_tags: {
|
|
data: [
|
|
{
|
|
tag: {
|
|
data: {
|
|
value: "Recipes"
|
|
},
|
|
on_conflict: {
|
|
constraint: tag_value_key,
|
|
update_columns: [value]
|
|
}
|
|
}
|
|
}
|
|
{
|
|
tag: {
|
|
data: {
|
|
value: "Cooking"
|
|
},
|
|
on_conflict: {
|
|
constraint: tag_value_key,
|
|
update_columns: [value]
|
|
}
|
|
}
|
|
}
|
|
]
|
|
}
|
|
}
|
|
]) {
|
|
returning {
|
|
title
|
|
article_tags {
|
|
tag {
|
|
value
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
:response:
|
|
{
|
|
"data": {
|
|
"insert_article": {
|
|
"returning": [
|
|
{
|
|
"title": "Article 1",
|
|
"article_tags": [
|
|
{
|
|
"tag": {
|
|
"value": "Recipes"
|
|
}
|
|
},
|
|
{
|
|
"tag": {
|
|
"value": "Cooking"
|
|
}
|
|
}
|
|
]
|
|
}
|
|
]
|
|
}
|
|
}
|
|
}
|
|
|
|
- insert a ``tag`` with ``articles`` where the ``tag`` might already exist (assume unique ``value`` for ``tag``):
|
|
|
|
.. graphiql::
|
|
:view_only:
|
|
:query:
|
|
mutation insertTagWithArticles {
|
|
insert_tag(objects: [
|
|
{
|
|
value: "Recipes",
|
|
article_tags: {
|
|
data: [
|
|
{
|
|
article: {
|
|
data: {
|
|
title: "Article 1",
|
|
content: "Article 1 content",
|
|
author_id: 1
|
|
}
|
|
}
|
|
},
|
|
{
|
|
article: {
|
|
data: {
|
|
title: "Article 2",
|
|
content: "Article 2 content",
|
|
author_id: 1
|
|
}
|
|
}
|
|
}
|
|
]
|
|
}
|
|
}
|
|
],
|
|
on_conflict: {
|
|
constraint: tag_value_key,
|
|
update_columns: [value]
|
|
}
|
|
) {
|
|
returning {
|
|
value
|
|
article_tags {
|
|
article {
|
|
title
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
:response:
|
|
{
|
|
"data": {
|
|
"insert_tag": {
|
|
"returning": [
|
|
{
|
|
"value": "Recipes",
|
|
"article_tags": [
|
|
{
|
|
"article": {
|
|
"title": "Article 1"
|
|
}
|
|
},
|
|
{
|
|
"article": {
|
|
"title": "Article 2"
|
|
}
|
|
}
|
|
]
|
|
}
|
|
]
|
|
}
|
|
}
|
|
}
|
|
|
|
.. note::
|
|
|
|
You can avoid the ``on_conflict`` clause if you will never have conflicts.
|
|
|
|
Fetching relationship information
|
|
---------------------------------
|
|
|
|
The intermediate fields ``article_tags`` & ``tag_articles`` can be used to fetch extra
|
|
information about the relationship. For example, you can have a column like ``tagged_at`` in the ``article_tag``
|
|
table which you can fetch as follows:
|
|
|
|
.. graphiql::
|
|
:view_only:
|
|
:query:
|
|
query {
|
|
articles {
|
|
id
|
|
title
|
|
article_tags {
|
|
tagged_at
|
|
tag {
|
|
id
|
|
tag_value
|
|
}
|
|
}
|
|
}
|
|
}
|
|
:response:
|
|
{
|
|
"data": {
|
|
"articles": [
|
|
{
|
|
"id": 1,
|
|
"title": "sit amet",
|
|
"article_tags": [
|
|
{
|
|
"tagged_at": "2018-11-19T18:01:17.292828+05:30",
|
|
"tag": {
|
|
"id": 1,
|
|
"tag_value": "mystery"
|
|
}
|
|
},
|
|
{
|
|
"tagged_at": "2018-11-18T18:01:17.292828+05:30",
|
|
"tag": {
|
|
"id": 3,
|
|
"tag_value": "romance"
|
|
}
|
|
}
|
|
]
|
|
},
|
|
{
|
|
"id": 2,
|
|
"title": "a nibh",
|
|
"article_tags": [
|
|
{
|
|
"tagged_at": "2018-11-19T15:01:17.292828+05:30",
|
|
"tag": {
|
|
"id": 5,
|
|
"tag_value": "biography"
|
|
}
|
|
},
|
|
{
|
|
"tagged_at": "2018-11-16T14:01:17.292828+05:30",
|
|
"tag": {
|
|
"id": 3,
|
|
"tag_value": "romance"
|
|
}
|
|
}
|
|
]
|
|
}
|
|
]
|
|
}
|
|
}
|
|
|
|
|
|
Flattening a many-to-many relationship query
|
|
--------------------------------------------
|
|
|
|
In case you would like to flatten the above queries and avoid the intermediate fields ``article_tags`` &
|
|
``tag_articles``, you can :ref:`create the following views <custom_views>` additionally and then
|
|
query using relationships created on these views:
|
|
|
|
.. code-block:: sql
|
|
|
|
CREATE VIEW article_tags_view AS
|
|
SELECT article_id, tags.*
|
|
FROM article_tag LEFT JOIN tags
|
|
ON article_tag.tag_id = tags.id
|
|
|
|
CREATE VIEW tag_articles_view AS
|
|
SELECT tag_id, articles.*
|
|
FROM article_tag LEFT JOIN articles
|
|
ON article_tag.article_id = articles.id
|
|
|
|
Now :ref:`create the following relationships <create_relationships>`:
|
|
|
|
- Array relationship, ``tags`` from the ``articles`` table using ``article_tags_view :: article_id -> id``
|
|
- Array relationship, ``articles`` from the ``tags`` table using ``tag_articles_view :: tag_id -> id``
|
|
|
|
We can now:
|
|
|
|
- fetch articles with their tags without an intermediate field:
|
|
|
|
.. graphiql::
|
|
:view_only:
|
|
:query:
|
|
query {
|
|
articles {
|
|
id
|
|
title
|
|
tags {
|
|
id
|
|
tag_value
|
|
}
|
|
}
|
|
}
|
|
:response:
|
|
{
|
|
"data": {
|
|
"articles": [
|
|
{
|
|
"id": 1,
|
|
"title": "sit amet",
|
|
"tags": [
|
|
{
|
|
"id": 1,
|
|
"tag_value": "mystery"
|
|
},
|
|
{
|
|
"id": 3,
|
|
"tag_value": "romance"
|
|
}
|
|
]
|
|
},
|
|
{
|
|
"id": 2,
|
|
"title": "a nibh",
|
|
"tags": [
|
|
{
|
|
"id": 5,
|
|
"tag_value": "biography"
|
|
},
|
|
{
|
|
"id": 3,
|
|
"tag_value": "romance"
|
|
}
|
|
]
|
|
}
|
|
]
|
|
}
|
|
}
|
|
|
|
- fetch tags with their articles without an intermediate field:
|
|
|
|
.. graphiql::
|
|
:view_only:
|
|
:query:
|
|
query {
|
|
tags {
|
|
id
|
|
tag_value
|
|
articles {
|
|
id
|
|
title
|
|
}
|
|
}
|
|
}
|
|
:response:
|
|
{
|
|
"data": {
|
|
"tags": [
|
|
{
|
|
"id": 1,
|
|
"tag_value": "mystery",
|
|
"articles": [
|
|
{
|
|
"id": 1,
|
|
"title": "sit amet"
|
|
}
|
|
]
|
|
},
|
|
{
|
|
"id": 2,
|
|
"tag_value": "biography",
|
|
"articles": [
|
|
{
|
|
"id": 1,
|
|
"title": "sit amet"
|
|
},
|
|
{
|
|
"id": 2,
|
|
"title": "a nibh"
|
|
}
|
|
]
|
|
}
|
|
]
|
|
}
|
|
}
|
|
|
|
.. note::
|
|
|
|
**We do not recommend this** flattening pattern of modelling as this introduces an additional overhead of managing
|
|
permissions and relationships on the newly created views. e.g. You cannot query for the author of the nested articles
|
|
without setting up a new relationship to the ``authors`` table from the ``tag_articles_view`` view.
|
|
|
|
In our opinion, the cons of this approach seem to outweigh the pros.
|