docs: pluralise query names, schema / table names

### 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
This commit is contained in:
Funmilayo E. Olaiya 2021-02-17 12:12:53 +01:00 committed by hasura-bot
parent a0995d1b30
commit 615922b63a
33 changed files with 284 additions and 282 deletions

View File

@ -51,17 +51,17 @@ Let's see access control in action using a simple example.
Create a table
^^^^^^^^^^^^^^
Head to your console and :ref:`create a table <create_tables>` called ``author`` with the following
Head to your console and :ref:`create a table <create_tables>` called ``authors`` with the following
schema:
.. code-block:: sql
author (
authors (
id INT PRIMARY KEY,
name TEXT
)
Now, insert some sample data into the table using the ``Insert Row`` tab of the ``author`` table.
Now, insert some sample data into the table using the ``Insert Row`` tab of the ``authors`` table.
Run a query **without** access control
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@ -71,7 +71,7 @@ Head to the ``GraphiQL`` tab in your console and try out the below query:
.. code-block:: graphql
query {
author {
authors {
id
name
}
@ -86,7 +86,7 @@ query is accepted with **admin** permissions.
Define access control rules
^^^^^^^^^^^^^^^^^^^^^^^^^^^
Now let's define an access control rule for the ``author`` table for a role ``users``.
Now let's define an access control rule for the ``authors`` table for a role ``user``.
.. rst-class:: api_tabs
.. tabs::
@ -109,7 +109,7 @@ Now let's define an access control rule for the ``author`` table for a role ``us
- table:
schema: public
name: author
name: authors
select_permissions:
- role: user
permission:
@ -139,7 +139,7 @@ Now let's define an access control rule for the ``author`` table for a role ``us
{
"type" : "create_select_permission",
"args" : {
"table" : "author",
"table" : "authors",
"role" : "user",
"permission" : {
"columns" : [
@ -153,7 +153,7 @@ Now let's define an access control rule for the ``author`` table for a role ``us
}
}
This permission rule reads as: "*For the role* ``user`` *, table* ``author`` *and operation* ``select``/``query``,
This permission rule reads as: "*For the role* ``user`` *, table* ``authors`` *and operation* ``select``/``query``,
allow access to those rows where the value in the ``id`` *column is the same as the value in the*
``X-Hasura-User-ID`` *session variable*".

View File

@ -20,12 +20,12 @@ Let's create a sample table and query data from it using the Hasura console, a U
Create a table
--------------
Head to the Hasura console, navigate to ``Data -> Create table`` and create a sample table called ``profile`` with
Head to the Hasura console, navigate to ``Data -> Create table`` and create a sample table called ``profiles`` with
the following columns:
.. code-block:: sql
profile (
profiles (
id SERIAL PRIMARY KEY, -- serial -> auto-incrementing integer
name TEXT
)
@ -33,7 +33,7 @@ the following columns:
.. thumbnail:: /img/graphql/core/getting-started/create-profile-table.png
:alt: Create a table
Now, insert some sample data into the table using the ``Insert Row`` tab of the ``profile`` table.
Now, insert some sample data into the table using the ``Insert Row`` tab of the ``profiles`` table.
Try out a query
---------------
@ -43,7 +43,7 @@ Head to the ``GraphiQL`` tab in the console and try running the following query:
.. code-block:: graphql
query {
profile {
profiles {
id
name
}

View File

@ -22,13 +22,13 @@ Say we have the following two tables in our database schema:
.. code-block:: sql
article (
articles (
id SERIAL PRIMARY KEY,
title TEXT
...
)
tag (
tags (
id SERIAL PRIMARY KEY,
tag_value TEXT
...
@ -57,8 +57,8 @@ This ``many-to-many`` relationship can be established in the database by:
2. Adding **foreign key constraints** from the ``article_tag`` table to:
- the ``article`` table using the ``article_id`` and ``id`` columns of the tables respectively
- the ``tag`` table using the ``tag_id`` and ``id`` columns of the tables respectively
- 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
@ -69,10 +69,10 @@ 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 ``article`` table using ``article_tag :: article_id -> id``
- Object relationship, ``tag`` from ``article_tag`` table using ``tag_id -> tag :: id``
- Array relationship, ``tag_articles`` from ``tag`` table using ``article_tag :: tag_id -> id``
- Object relationship, ``article`` from ``article_tag`` table using ``article_id -> article :: id``
- 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
--------------------------------------
@ -85,7 +85,7 @@ We can now:
:view_only:
:query:
query {
article {
articles {
id
title
article_tags {
@ -99,7 +99,7 @@ We can now:
:response:
{
"data": {
"article": [
"articles": [
{
"id": 1,
"title": "sit amet",
@ -146,7 +146,7 @@ We can now:
:view_only:
:query:
query {
tag {
tags {
id
tag_value
tag_articles {
@ -160,7 +160,7 @@ We can now:
:response:
{
"data": {
"tag": [
"tags": [
{
"id": 1,
"tag_value": "mystery",
@ -363,7 +363,7 @@ table which you can fetch as follows:
:view_only:
:query:
query {
article {
articles {
id
title
article_tags {
@ -378,7 +378,7 @@ table which you can fetch as follows:
:response:
{
"data": {
"article": [
"articles": [
{
"id": 1,
"title": "sit amet",
@ -434,19 +434,19 @@ query using relationships created on these views:
.. code-block:: sql
CREATE VIEW article_tags_view AS
SELECT article_id, tag.*
FROM article_tag LEFT JOIN tag
ON article_tag.tag_id = tag.id
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, article.*
FROM article_tag LEFT JOIN article
ON article_tag.article_id = article.id
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 ``article`` table using ``article_tags_view :: article_id -> id``
- Array relationship, ``articles`` from the ``tag`` table using ``tag_articles_view :: tag_id -> id``
- 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:
@ -456,7 +456,7 @@ We can now:
:view_only:
:query:
query {
article {
articles {
id
title
tags {
@ -468,7 +468,7 @@ We can now:
:response:
{
"data": {
"article": [
"articles": [
{
"id": 1,
"title": "sit amet",
@ -507,7 +507,7 @@ We can now:
:view_only:
:query:
query {
tag {
tags {
id
tag_value
articles {
@ -519,7 +519,7 @@ We can now:
:response:
{
"data": {
"tag": [
"tags": [
{
"id": 1,
"tag_value": "mystery",
@ -552,6 +552,6 @@ We can now:
**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 ``author`` table from the ``tag_articles_view`` view.
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.

View File

@ -21,12 +21,12 @@ Say we have the following two tables in our database schema:
.. code-block:: sql
author (
authors (
id SERIAL PRIMARY KEY,
name TEXT
)
article (
articles (
id SERIAL PRIMARY KEY,
author_id INT
title TEXT
@ -43,19 +43,19 @@ Step 1: Set up a table relationship in the database
This ``one-to-many`` relationship can be established in the database by:
1. Adding a **foreign key constraint** from the ``article`` table to the ``author`` table using the ``author_id`` and
1. Adding a **foreign key constraint** from the ``articles`` table to the ``authors`` table using the ``author_id`` and
``id`` columns of the tables respectively.
This will ensure that the value of ``author_id`` column in the ``article`` table is present in the ``id`` column of
the ``author`` table.
This will ensure that the value of ``author_id`` column in the ``articles`` table is present in the ``id`` column of
the ``authors`` table.
Step 2: Set up GraphQL relationships
------------------------------------
To access the nested objects via the GraphQL API, :ref:`create the following relationships <create_relationships>`:
- Array relationship, ``articles`` from ``author`` table using ``article :: author_id -> id``
- Object relationship, ``author`` from ``article`` table using ``author_id -> author :: id``
- Array relationship, ``articles`` from ``authors`` table using ``articles :: author_id -> id``
- Object relationship, ``author`` from ``articles`` table using ``author_id -> authors :: id``
Query using one-to-many relationships
-------------------------------------
@ -68,7 +68,7 @@ We can now:
:view_only:
:query:
query {
author {
authors {
id
name
articles {
@ -80,7 +80,7 @@ We can now:
:response:
{
"data": {
"author": [
"authors": [
{
"id": 1,
"name": "Justin",
@ -120,7 +120,7 @@ We can now:
:view_only:
:query:
query {
article {
articles {
id
title
author {
@ -132,7 +132,7 @@ We can now:
:response:
{
"data": {
"article": [
"articles": [
{
"id": 1,
"title": "sit amet",

View File

@ -21,7 +21,7 @@ Say we have the following two tables in our database schema:
.. code-block:: sql
author (
authors (
id SERIAL PRIMARY KEY,
name TEXT
)
@ -43,21 +43,21 @@ Step 1: Set up a table relationship in the database
This ``one-to-one`` relationship can be established in the database by:
1. Adding a **foreign key constraint** from the ``passport_info`` table to the ``author`` table using the ``owner_id``
1. Adding a **foreign key constraint** from the ``passport_info`` table to the ``authors`` table using the ``owner_id``
and ``id`` columns of the tables respectively
2. Adding a **unique constraint** to the ``owner_id`` column for the ``passport_info`` table
This will ensure that the value of the ``owner_id`` column in ``passport_info`` table is present in the ``id`` column of
the ``author`` table and there will be only one row with a particular ``owner_id``.
the ``authors`` table and there will be only one row with a particular ``owner_id``.
Step 2: Set up GraphQL relationships
------------------------------------
To access the nested objects via the GraphQL API, :ref:`create the following relationships <create_relationships>`:
- Object relationship, ``passport_info`` from the ``author`` table using ``id -> passport_info :: owner_id``
- Object relationship, ``owner`` from the ``passport_info`` table using ``owner_id -> author :: id``
- Object relationship, ``passport_info`` from the ``authors`` table using ``id -> passport_info :: owner_id``
- Object relationship, ``owner`` from the ``passport_info`` table using ``owner_id -> authors :: id``
Query using one-to-one relationships
------------------------------------
@ -70,7 +70,7 @@ We can now:
:view_only:
:query:
query {
author {
authors {
id
name
passport_info {
@ -82,7 +82,7 @@ We can now:
:response:
{
"data": {
"author": [
"authors": [
{
"id": 1,
"name": "Justin",
@ -210,4 +210,4 @@ This is due to the way Hasura GraphQL engine currently handles nested mutations
will be attempted to be inserted first and the value of its ``owner_id`` will be attempted to be set as the
``id`` of the ``author``. Due to this, based on whether the ``owner_id`` of ``passport_info`` is nullable or not, a
``Not-NULL violation`` error will be thrown either for the ``owner_id`` field of ``passport_info`` or the ``id``
field of ``author``.
field of ``authors``.

View File

@ -36,7 +36,7 @@ Fetch aggregated data of an object
:view_only:
:query:
query {
article_aggregate {
articles_aggregate {
aggregate {
count
sum {
@ -59,7 +59,7 @@ Fetch aggregated data of an object
:response:
{
"data": {
"article_aggregate": {
"articles_aggregate": {
"aggregate": {
"count": 10,
"sum": {
@ -141,7 +141,7 @@ and articles.
:view_only:
:query:
query {
author (where: {id: {_eq: 1}}) {
authors (where: {id: {_eq: 1}}) {
id
name
articles_aggregate {
@ -165,7 +165,7 @@ and articles.
:response:
{
"data": {
"author": [
"authors": [
{
"id": 1,
"name": "Justin",

View File

@ -24,13 +24,13 @@ See :ref:`sort queries <sort_query_results>` for more info on using ``order_by``
.. code-block:: graphql
employee (
distinct_on: [employee_select_column]
order_by: [employee_order_by]
): [employee]!
employees (
distinct_on: [employees_select_column]
order_by: [employees_order_by]
): [employees]!
# select column enum type for "employee" table
enum employee_select_column {
# select column enum type for "employees" table
enum employees_select_column {
id
name
department
@ -48,7 +48,7 @@ Fetch results with distinct values of a particular field
:view_only:
:query:
query {
employee (
employees (
distinct_on: [department]
order_by: [
{department: asc},
@ -64,7 +64,7 @@ Fetch results with distinct values of a particular field
:response:
{
"data": {
"employee": [
"employees": [
{
"id": 5,
"name": "Kamila",

View File

@ -24,7 +24,7 @@ of publication:
:view_only:
:query:
query {
author {
authors {
id
name
articles(
@ -42,7 +42,7 @@ of publication:
:response:
{
"data": {
"author": [
"authors": [
{
"id": 1,
"name": "Justin",

View File

@ -28,11 +28,11 @@ Run multiple top level queries in the same request
:view_only:
:query:
query {
author(limit: 2) {
authors(limit: 2) {
id
name
}
article(limit: 2) {
articles(limit: 2) {
id
title
}
@ -40,7 +40,7 @@ Run multiple top level queries in the same request
:response:
{
"data": {
"author": [
"authors": [
{
"id": 1,
"name": "Justin"
@ -50,7 +50,7 @@ Run multiple top level queries in the same request
"name": "Beltran"
}
],
"article": [
"articles": [
{
"id": 1,
"title": "sit amet"

View File

@ -35,7 +35,7 @@ author.
:view_only:
:query:
query {
article {
articles {
id
title
author {
@ -46,7 +46,7 @@ author.
:response:
{
"data": {
"article": [
"articles": [
{
"id": 1,
"title": "sit amet",
@ -83,7 +83,7 @@ articles.
:view_only:
:query:
query {
author {
authors {
id
name
articles {
@ -95,7 +95,7 @@ articles.
:response:
{
"data": {
"author": [
"authors": [
{
"id": 1,
"name": "Justin",

View File

@ -33,7 +33,7 @@ Limit results
:view_only:
:query:
query {
author(
authors(
limit: 5
) {
id
@ -43,7 +43,7 @@ Limit results
:response:
{
"data": {
"author": [
"authors": [
{
"id": 1,
"name": "Justin"
@ -76,7 +76,7 @@ Limit results from an offset
:view_only:
:query:
query {
author(
authors(
limit: 5,
offset:5
) {
@ -87,7 +87,7 @@ Limit results from an offset
:response:
{
"data": {
"author": [
"authors": [
{
"id": 6,
"name": "Corny"
@ -122,7 +122,7 @@ Limit results in a nested object
:view_only:
:query:
query {
author {
authors {
id
name
articles (
@ -137,7 +137,7 @@ Limit results in a nested object
:response:
{
"data": {
"author": [
"authors": [
{
"id": 1,
"name": "Justin",
@ -215,7 +215,7 @@ position of the row in the dataset as done by ``offset``, and that duplicate rec
:view_only:
:query:
query {
author(
authors(
limit: 5,
where: { id: {_gt: 5} }
) {
@ -226,7 +226,7 @@ position of the row in the dataset as done by ``offset``, and that duplicate rec
:response:
{
"data": {
"author": [
"authors": [
{
"id": 6,
"name": "Corny"

View File

@ -25,7 +25,7 @@ For example, to fetch data for an author whose name is "Sidney":
:emphasize-lines: 3
query {
author(
authors(
where: {name: {_eq: "Sidney"}}
) {
id
@ -41,7 +41,7 @@ For example, to fetch a list of authors who have articles with a rating greater
:emphasize-lines: 2,5
query {
author (where: {articles: {rating: {_gt: 4}}}) {
authors (where: {articles: {rating: {_gt: 4}}}) {
id
name
articles (where: {rating: {_gt: 4}}) {
@ -75,13 +75,13 @@ The following are examples of using the equality operators on different types.
**Example: Integer (works with Double, Float, Numeric, etc.)**
Fetch data about author whose ``id`` *(an integer field)* is equal to 3:
Fetch data about an author whose ``id`` *(an integer field)* is equal to 3:
.. graphiql::
:view_only:
:query:
query {
author(
authors(
where: {id: {_eq: 3}}
) {
id
@ -91,7 +91,7 @@ Fetch data about author whose ``id`` *(an integer field)* is equal to 3:
:response:
{
"data": {
"author": [
"authors": [
{
"id": 3,
"name": "Sidney"
@ -108,7 +108,7 @@ Fetch a list of authors with ``name`` *(a text field)* as "Sidney":
:view_only:
:query:
query {
author(
authors(
where: {name: {_eq: "Sidney"}}
) {
id
@ -118,7 +118,7 @@ Fetch a list of authors with ``name`` *(a text field)* as "Sidney":
:response:
{
"data": {
"author": [
"authors": [
{
"id": 3,
"name": "Sidney"
@ -135,7 +135,7 @@ Fetch a list of articles that have not been published (``is_published`` is a boo
:view_only:
:query:
query {
article(
articles(
where: {is_published: {_eq: false}}
) {
id
@ -146,7 +146,7 @@ Fetch a list of articles that have not been published (``is_published`` is a boo
:response:
{
"data": {
"article": [
"articles": [
{
"id": 5,
"title": "ut blandit",
@ -180,7 +180,7 @@ Fetch a list of articles that were published on a certain date (``published_on``
:view_only:
:query:
query {
article(
articles(
where: {published_on: {_eq: "2017-05-26"}}
) {
id
@ -191,7 +191,7 @@ Fetch a list of articles that were published on a certain date (``published_on``
:response:
{
"data": {
"article": [
"articles": [
{
"id": 3,
"title": "amet justo morbi",
@ -221,7 +221,7 @@ Fetch a list of articles rated 4 or more (``rating`` is an integer field):
:view_only:
:query:
query {
article(
articles(
where: {rating: {_gte: 4}}
) {
id
@ -232,7 +232,7 @@ Fetch a list of articles rated 4 or more (``rating`` is an integer field):
:response:
{
"data": {
"article": [
"articles": [
{
"id": 3,
"title": "amet justo morbi",
@ -261,7 +261,7 @@ dictionary sort)*:
:view_only:
:query:
query {
author(
authors(
where: {name: {_gt: "M"}}
) {
id
@ -271,7 +271,7 @@ dictionary sort)*:
:response:
{
"data": {
"author": [
"authors": [
{
"id": 3,
"name": "Sidney"
@ -292,7 +292,7 @@ Fetch a list of articles that were published on or after date "01/01/2018":
:view_only:
:query:
query {
article(
articles(
where: {published_on: {_gte: "2018-01-01"}}
) {
id
@ -303,7 +303,7 @@ Fetch a list of articles that were published on or after date "01/01/2018":
:response:
{
"data": {
"article": [
"articles": [
{
"id": 2,
"title": "a nibh",
@ -347,7 +347,7 @@ Fetch a list of articles rated 1, 3 or 5:
:view_only:
:query:
query {
article(
articles(
where: {rating: {_in: [1,3,5]}}
) {
id
@ -358,7 +358,7 @@ Fetch a list of articles rated 1, 3 or 5:
:response:
{
"data": {
"article": [
"articles": [
{
"id": 1,
"title": "sit amet",
@ -391,7 +391,7 @@ Fetch a list of those authors whose names are NOT part of a list:
:view_only:
:query:
query {
author(
authors(
where: {name: {_nin: ["Justin","Sidney","April"]}}
) {
id
@ -401,7 +401,7 @@ Fetch a list of those authors whose names are NOT part of a list:
:response:
{
"data": {
"author": [
"authors": [
{
"id": 2,
"name": "Beltran"
@ -438,7 +438,7 @@ Fetch a list of articles whose titles contain the word “amet”:
:view_only:
:query:
query {
article(
articles(
where: {title: {_like: "%amet%"}}
) {
id
@ -448,7 +448,7 @@ Fetch a list of articles whose titles contain the word “amet”:
:response:
{
"data": {
"article": [
"articles": [
{
"id": 1,
"title": "sit amet"
@ -476,7 +476,7 @@ Fetch a list of authors whose names begin with A or C:
:view_only:
:query:
query {
author(
authors(
where: {name: {_similar: "(A|C)%"}}
) {
id
@ -486,7 +486,7 @@ Fetch a list of authors whose names begin with A or C:
:response:
{
"data": {
"author": [
"authors": [
{
"id": 4,
"name": "Anjela"
@ -527,7 +527,7 @@ Fetch all authors living within a particular pincode (present in ``address`` JSO
:view_only:
:query:
query get_authors_in_pincode ($jsonFilter: jsonb){
author(
authors(
where: {
address: {_contains: $jsonFilter }
}
@ -540,7 +540,7 @@ Fetch all authors living within a particular pincode (present in ``address`` JSO
:response:
{
"data": {
"author": [
"authors": [
{
"id": 1,
"name": "Ash",
@ -570,7 +570,7 @@ Fetch authors if the ``phone`` key is present in their JSONB ``address`` column:
:view_only:
:query:
query get_authors_if_phone {
author(
authors(
where: {
address: {_has_key: "phone" }
}
@ -583,7 +583,7 @@ Fetch authors if the ``phone`` key is present in their JSONB ``address`` column:
:response:
{
"data": {
"author": [
"authors": [
{
"id": 1,
"name": "Ash",
@ -727,7 +727,7 @@ Fetch a list of articles that have a value in the ``published_on`` field:
:view_only:
:query:
query {
article(
articles(
where: {published_on: {_is_null: false}}
) {
id
@ -738,7 +738,7 @@ Fetch a list of articles that have a value in the ``published_on`` field:
:response:
{
"data": {
"article": [
"articles": [
{
"id": 1,
"title": "sit amet",
@ -930,7 +930,7 @@ Fetch all authors who don't have any published articles:
:view_only:
:query:
{
author(
authors(
where: {
_not: {
articles: { is_published: {_eq: true} }
@ -947,7 +947,7 @@ Fetch all authors who don't have any published articles:
:response:
{
"data": {
"author": [
"authors": [
{
"id": 7,
"name": "Berti",
@ -995,7 +995,7 @@ Fetch a list of articles published in a specific time-frame (for example: in yea
:view_only:
:query:
query {
article (
articles (
where: {
_and: [
{ published_on: {_gte: "2017-01-01"}},
@ -1012,7 +1012,7 @@ Fetch a list of articles published in a specific time-frame (for example: in yea
:response:
{
"data": {
"article": [
"articles": [
{
"id": 1,
"title": "sit amet",
@ -1050,7 +1050,7 @@ Fetch a list of articles rated more than 4 or published after "01/01/2018":
:view_only:
:query:
query {
article (
articles (
where: {
_or: [
{rating: {_gte: 4}},
@ -1068,7 +1068,7 @@ Fetch a list of articles rated more than 4 or published after "01/01/2018":
:response:
{
"data": {
"article": [
"articles": [
{
"id": 2,
"title": "a nibh",
@ -1118,7 +1118,7 @@ Fetch all authors with only their 5 rated articles:
:view_only:
:query:
{
author {
authors {
id
name
articles(where: {rating: {_eq: 5}}) {
@ -1130,7 +1130,7 @@ Fetch all authors with only their 5 rated articles:
:response:
{
"data": {
"author": [
"authors": [
{
"id": 1,
"name": "Justin",
@ -1172,7 +1172,7 @@ For example:
:emphasize-lines: 2
query {
article (where: {author: {name: {_eq: "Sidney"}}}) {
articles (where: {author: {name: {_eq: "Sidney"}}}) {
id
title
}
@ -1199,7 +1199,7 @@ Fetch all articles whose author's name starts with "A":
:view_only:
:query:
{
article (
articles (
where: {
author: {
name: { _similar: "A%"}
@ -1216,7 +1216,7 @@ Fetch all articles whose author's name starts with "A":
:response:
{
"data": {
"article": [
"articles": [
{
"id": 1,
"title": "sit amet",
@ -1268,7 +1268,7 @@ Fetch all authors which have written at least one article which is rated 1:
:view_only:
:query:
{
author(
authors(
where: {
articles: {rating: {_eq: 1}}
}
@ -1284,7 +1284,7 @@ Fetch all authors which have written at least one article which is rated 1:
:response:
{
"data": {
"author": [
"authors": [
{
"id": 1,
"name": "Justin",
@ -1360,7 +1360,7 @@ Fetch all authors which have all of their articles published i.e. have ``{is_pub
:view_only:
:query:
{
author (
authors (
where: {
_not: {
articles: {is_published: {_neq: true}}
@ -1378,7 +1378,7 @@ Fetch all authors which have all of their articles published i.e. have ``{is_pub
:response:
{
"data": {
"author": [
"authors": [
{
"id": 1,
"name": "Justin",
@ -1461,7 +1461,7 @@ Fetch all authors which have none of their articles published i.e. have ``{is_pu
:view_only:
:query:
{
author (
authors(
where: {
_not: {
articles: {is_published: {_eq: true}}
@ -1479,7 +1479,7 @@ Fetch all authors which have none of their articles published i.e. have ``{is_pu
:response:
{
"data": {
"author": [
"authors": [
{
"id": 7,
"name": "Berti",
@ -1519,7 +1519,7 @@ Fetch all authors which have at least one article written by them:
:view_only:
:query:
{
author (
authors (
where: {
articles: {}
}
@ -1536,7 +1536,7 @@ Fetch all authors which have at least one article written by them:
:response:
{
"data": {
"author": [
"authors": [
{
"id": 1,
"name": "Justin",
@ -1567,7 +1567,7 @@ Fetch all authors which have not written any articles:
:view_only:
:query:
{
author (
authors (
where: {
_not: {
articles: {}
@ -1586,7 +1586,7 @@ Fetch all authors which have not written any articles:
:response:
{
"data": {
"author": [
"authors": [
{
"id": 2,
"name": "Beltran",

View File

@ -25,7 +25,7 @@ Fetch list of objects
:view_only:
:query:
query {
author {
authors {
id
name
}
@ -33,7 +33,7 @@ Fetch list of objects
:response:
{
"data": {
"author": [
"authors": [
{
"id": 1,
"name": "Justin"
@ -63,7 +63,7 @@ Fetch an object using its primary key
:view_only:
:query:
query {
author_by_pk(id: 1) {
authors_by_pk(id: 1) {
id
name
}
@ -71,7 +71,7 @@ Fetch an object using its primary key
:response:
{
"data": {
"author_by_pk": {
"authors_by_pk": {
"id": 1,
"name": "Justin"
}
@ -87,7 +87,7 @@ address column:
:view_only:
:query:
query {
author_by_pk(id: 1) {
authors_by_pk(id: 1) {
id
name
address
@ -98,7 +98,7 @@ address column:
:response:
{
"data": {
"author_by_pk": {
"authors_by_pk": {
"id": 1,
"name": "Justin",
"address": {

View File

@ -44,7 +44,7 @@ Sorting objects
:view_only:
:query:
query {
author (
authors (
order_by: {name: asc}
) {
id
@ -54,7 +54,7 @@ Sorting objects
:response:
{
"data": {
"author": [
"authors": [
{
"id": 5,
"name": "Amii"
@ -94,7 +94,7 @@ their rating:
:view_only:
:query:
query {
author (order_by: {name: asc}) {
authors (order_by: {name: asc}) {
id
name
articles(order_by: {rating: desc}) {
@ -107,7 +107,7 @@ their rating:
:response:
{
"data": {
"author": [
"authors": [
{
"id": 5,
"name": "Amii",
@ -179,7 +179,7 @@ For object relationships only columns can be used for sorting.
:view_only:
:query:
query {
article (
articles (
order_by: {author: {id: desc}}
) {
id
@ -194,7 +194,7 @@ For object relationships only columns can be used for sorting.
:response:
{
"data": {
"article": [
"articles": [
{
"id": 3,
"title": "Article 3",
@ -236,7 +236,7 @@ For array relationships only aggregates can be used for sorting.
:view_only:
:query:
query {
author (
authors (
order_by: {
articles_aggregate: {count: desc}
}
@ -253,7 +253,7 @@ For array relationships only aggregates can be used for sorting.
:response:
{
"data": {
"author": [
"authors": [
{
"id": 5,
"name": "Amii",
@ -291,7 +291,7 @@ For array relationships only aggregates can be used for sorting.
:view_only:
:query:
query {
author(
authors(
order_by: {
articles_aggregate: {
max: {rating: asc_nulls_last}
@ -310,7 +310,7 @@ For array relationships only aggregates can be used for sorting.
:response:
{
"data": {
"author": [
"authors": [
{
"id": 7,
"name": "Berti",
@ -391,7 +391,7 @@ date (ascending with nulls first):
:view_only:
:query:
query {
article (
articles (
order_by: [
{rating: desc},
{published_on: asc_nulls_first}
@ -405,7 +405,7 @@ date (ascending with nulls first):
:response:
{
"data": {
"article": [
"articles": [
{
"id": 17,
"rating": 5,

View File

@ -58,26 +58,26 @@ Let's say we have the following schema:
.. code-block:: plpgsql
author(id integer, first_name text, last_name text)
authors(id integer, first_name text, last_name text)
:ref:`Define an SQL function <create_sql_functions>` called ``author_full_name``:
.. code-block:: plpgsql
CREATE FUNCTION author_full_name(author_row author)
CREATE FUNCTION author_full_name(author_row authors)
RETURNS TEXT AS $$
SELECT author_row.first_name || ' ' || author_row.last_name
$$ LANGUAGE sql STABLE;
:ref:`Add a computed field <add-computed-field>` called ``full_name`` to the ``author`` table using the SQL function above.
:ref:`Add a computed field <add-computed-field>` called ``full_name`` to the ``authors`` table using the SQL function above.
Query data from the ``author`` table:
Query data from the ``authors`` table:
.. graphiql::
:view_only:
:query:
query {
author {
authors {
id
first_name
last_name
@ -87,7 +87,7 @@ Query data from the ``author`` table:
:response:
{
"data": {
"author": [
"authors": [
{
"id": 1,
"first_name": "Chris",
@ -110,11 +110,11 @@ Let's say we have the following schema:
.. code-block:: plpgsql
author(id integer, first_name text, last_name text)
authors(id integer, first_name text, last_name text)
article(id integer, title text, content text, author_id integer)
articles(id integer, title text, content text, author_id integer)
Now we can define a :ref:`table relationship <table_relationships>` on the ``author``
Now we can define a :ref:`table relationship <table_relationships>` on the ``authors``
table to fetch authors along with their articles.
We can make use of computed fields to fetch the author's articles with a search parameter.
@ -123,25 +123,25 @@ We can make use of computed fields to fetch the author's articles with a search
.. code-block:: plpgsql
CREATE FUNCTION filter_author_articles(author_row author, search text)
RETURNS SETOF article AS $$
CREATE FUNCTION filter_author_articles(author_row authors, search text)
RETURNS SETOF articles AS $$
SELECT *
FROM article
FROM articles
WHERE
( title ilike ('%' || search || '%')
OR content ilike ('%' || search || '%')
) AND author_id = author_row.id
$$ LANGUAGE sql STABLE;
:ref:`Add a computed field <add-computed-field>` called ``filtered_articles`` to the ``author`` table using the SQL function above.
:ref:`Add a computed field <add-computed-field>` called ``filtered_articles`` to the ``authors`` table using the SQL function above.
Query data from the ``author`` table:
Query data from the ``authors`` table:
.. graphiql::
:view_only:
:query:
query {
author {
authors {
id
first_name
last_name
@ -155,7 +155,7 @@ Query data from the ``author`` table:
:response:
{
"data": {
"author": [
"authors": [
{
"id": 1,
"first_name": "Chris",
@ -201,7 +201,7 @@ Adding a computed field to a table/view
- table:
schema: public
name: author
name: authors
computed_fields:
- name: full_name
definition:
@ -231,7 +231,7 @@ Adding a computed field to a table/view
"type": "add_computed_field",
"args": {
"table": {
"name": "author",
"name": "authors",
"schema": "public"
},
"name": "full_name",
@ -261,9 +261,9 @@ Accessing Hasura session variables in computed fields
It can be useful to have access to the session variable from the SQL function defining a computed field.
For instance, suppose we want to record which users have liked which articles. We can do so using a table
``article_likes`` that specifies a many-to-many relationship between ``article`` and ``user``. In such a
``article_likes`` that specifies a many-to-many relationship between ``articles`` and ``users``. In such a
case it can be useful to know if the current user has liked a specific article, and this information can be
exposed as a *Boolean* computed field on ``article``.
exposed as a *Boolean* computed field on ``articles``.
Create a function with an argument for session variables and add it with the :ref:`add_computed_field` API with the
``session_argument`` key set. The session argument is a JSON object where keys are session variable names
@ -273,7 +273,7 @@ as shown in the following example.
.. code-block:: plpgsql
-- 'hasura_session' will be the session argument
CREATE OR REPLACE FUNCTION article_liked_by_user(article_row article, hasura_session json)
CREATE OR REPLACE FUNCTION article_liked_by_user(article_row articles, hasura_session json)
RETURNS boolean AS $$
SELECT EXISTS (
SELECT 1
@ -292,7 +292,7 @@ as shown in the following example.
"type":"add_computed_field",
"args":{
"table":{
"name":"article",
"name":"articles",
"schema":"public"
},
"name":"liked_by_user",
@ -311,7 +311,7 @@ as shown in the following example.
:view_only:
:query:
query {
article(where: {id: {_eq: 3}}) {
articles(where: {id: {_eq: 3}}) {
id
liked_by_user
}
@ -319,7 +319,7 @@ as shown in the following example.
:response:
{
"data": {
"article": [
"articles": [
{
"id": "3",
"liked_by_user": true

View File

@ -159,23 +159,23 @@ Let's take a look at an example where the ``SETOF`` table is already part of the
.. code-block:: plpgsql
article(id integer, title text, content text)
articles(id integer, title text, content text)
Let's say we've created and tracked a custom function, ``search_articles``, with the following definition:
.. code-block:: plpgsql
CREATE FUNCTION search_articles(search text)
RETURNS SETOF article AS $$
RETURNS SETOF articles AS $$
SELECT *
FROM article
FROM articles
WHERE
title ilike ('%' || search || '%')
OR content ilike ('%' || search || '%')
$$ LANGUAGE sql STABLE;
This function filters rows from the ``article`` table based on the input text argument, ``search`` i.e. it
returns ``SETOF article``. Assuming the ``article`` table is being tracked, you can use the custom function
This function filters rows from the ``articles`` table based on the input text argument, ``search`` i.e. it
returns ``SETOF articles``. Assuming the ``articles`` table is being tracked, you can use the custom function
as follows:
.. graphiql::
@ -223,17 +223,17 @@ Next create a GIN (or GIST) index in your database for the columns you'll be que
.. code-block:: sql
CREATE INDEX address_gin_idx ON property
CREATE INDEX address_gin_idx ON properties
USING GIN ((unit || ' ' || num || ' ' || street || ' ' || city || ' ' || region || ' ' || postcode) gin_trgm_ops);
And finally create the custom SQL function in the Hasura console:
.. code-block:: plpgsql
CREATE FUNCTION search_property(search text)
RETURNS SETOF property AS $$
CREATE FUNCTION search_properties(search text)
RETURNS SETOF properties AS $$
SELECT *
FROM property
FROM properties
WHERE
search <% (unit || ' ' || num || ' ' || street || ' ' || city || ' ' || region || ' ' || postcode)
ORDER BY
@ -241,13 +241,13 @@ And finally create the custom SQL function in the Hasura console:
LIMIT 5;
$$ LANGUAGE sql STABLE;
Assuming the ``property`` table is being tracked, you can use the custom function as follows:
Assuming the ``properties`` table is being tracked, you can use the custom function as follows:
.. graphiql::
:view_only:
:query:
query {
search_property(
search_properties(
args: {search: "Unit 2, 25 Foobar St, Sydney NSW 2000"}
){
id
@ -262,7 +262,7 @@ Assuming the ``property`` table is being tracked, you can use the custom functio
:response:
{
"data": {
"search_property": [
"search_properties": [
{
"id": 1,
"unit": "UNIT 2",

View File

@ -27,23 +27,23 @@ 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, ``authors(id, name)`` and ``articles(id, title, content, rating, author_id)``.
Let us now connect these tables to enable nested queries using a foreign-key:
Step 1: Add foreign-key constraint
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Let's add a foreign-key constraint to the ``author_id`` column in the ``article`` table.
Let's add a foreign-key constraint to the ``author_id`` column in the ``articles`` table.
.. rst-class:: api_tabs
.. tabs::
.. tab:: Console
In the console, navigate to the ``Modify`` tab of the ``article`` table. Click the ``Add`` button in
In the console, navigate to the ``Modify`` tab of the ``articles`` table. Click the ``Add`` button in
the Foreign Keys section and configure the ``author_id`` column as a foreign-key for the ``id`` column in
the ``author`` table:
the ``authors`` table:
.. thumbnail:: /img/graphql/core/schema/add-foreign-key.png
:alt: Add foreign-key constraint
@ -54,15 +54,15 @@ Let's add a foreign-key constraint to the ``author_id`` column in the ``article`
.. code-block:: sql
ALTER TABLE article
ADD FOREIGN KEY (author_id) REFERENCES author(id);
ALTER TABLE articles
ADD FOREIGN KEY (author_id) REFERENCES authors(id);
Add the following statement to the ``down.sql`` file in case you need to :ref:`roll back <roll_back_migrations>` the above statement:
.. code-block:: sql
ALTER TABLE article
DROP CONSTRAINT article_author_id_fkey;
ALTER TABLE articles
DROP CONSTRAINT articles_author_id_fkey;
Apply the migration by running:
@ -83,7 +83,7 @@ Let's add a foreign-key constraint to the ``author_id`` column in the ``article`
{
"type": "run_sql",
"args": {
"sql": "ALTER TABLE article ADD FOREIGN KEY (author_id) REFERENCES author(id);"
"sql": "ALTER TABLE articles ADD FOREIGN KEY (author_id) REFERENCES authors(id);"
}
}
@ -98,9 +98,9 @@ Each article has one author. This is an ``object relationship``.
.. tab:: Console
The console infers potential relationships using the foreign-key created above and recommends these in the
``Relationships`` tab of the ``article`` table.
``Relationships`` tab of the ``articles`` table.
Add an ``object relationship`` named ``author`` for the ``article`` table as shown here:
Add an ``object relationship`` named ``author`` for the ``articles`` table as shown here:
.. thumbnail:: /img/graphql/core/schema/add-1-1-relationship.png
:alt: Create an object relationship
@ -114,14 +114,14 @@ Each article has one author. This is an ``object relationship``.
- table:
schema: public
name: article
name: articles
object_relationships:
- name: author
using:
foreign_key_constraint_on: author_id
- table:
schema: public
name: author
name: authors
Apply the metadata by running:
@ -142,7 +142,7 @@ Each article has one author. This is an ``object relationship``.
{
"type": "create_object_relationship",
"args": {
"table": "article",
"table": "articles",
"name": "author",
"using": {
"foreign_key_constraint_on": "author_id"
@ -158,7 +158,7 @@ Fetch a list of articles and each article's author:
:view_only:
:query:
query {
article {
articles {
id
title
author {
@ -170,7 +170,7 @@ Fetch a list of articles and each article's author:
:response:
{
"data": {
"article": [
"articles": [
{
"id": 1,
"title": "sit amet",
@ -211,7 +211,7 @@ You can add an ``array relationship`` in the same fashion as an ``object relatio
.. tab:: Console
On the console, add an ``array relationship`` named ``articles`` for the ``author`` table as shown here:
On the console, add an ``array relationship`` named ``articles`` for the ``authors`` table as shown here:
.. thumbnail:: /img/graphql/core/schema/add-1-many-relationship.png
:alt: Create an array relationship
@ -227,14 +227,14 @@ You can add an ``array relationship`` in the same fashion as an ``object relatio
- table:
schema: public
name: article
name: articles
object_relationships:
- name: author
using:
foreign_key_constraint_on: author_id
- table:
schema: public
name: author
name: authors
array_relationships:
- name: articles
using:
@ -242,7 +242,7 @@ You can add an ``array relationship`` in the same fashion as an ``object relatio
column: author_id
table:
schema: public
name: article
name: articles
Apply the metadata by running:
@ -263,11 +263,11 @@ You can add an ``array relationship`` in the same fashion as an ``object relatio
{
"type": "create_array_relationship",
"args": {
"table": "author",
"table": "authors",
"name": "articles",
"using": {
"foreign_key_constraint_on" : {
"table" : "article",
"table" : "articles",
"column" : "author_id"
}
}
@ -280,7 +280,7 @@ Fetch a list of authors and a nested list of each author's articles:
:view_only:
:query:
query {
author {
authors {
id
name
articles {
@ -292,7 +292,7 @@ Fetch a list of authors and a nested list of each author's articles:
:response:
{
"data": {
"author": [
"authors": [
{
"id": 1,
"name": "Justin",
@ -348,10 +348,10 @@ Fetch a list of authors and a nested list of each author's articles:
Using manual relationships
--------------------------
Let's say you have a table ``author (id, name)`` and a :ref:`view <custom_views>` ``author_avg_rating (id, avg)`` which has the
Let's say you have a table ``authors (id, name)`` and a :ref:`view <custom_views>` ``author_avg_rating (id, avg)`` which has the
average rating of articles for each author.
Let us now create an ``object relationship`` called ``avg_rating`` from the ``author`` table to the
Let us now create an ``object relationship`` called ``avg_rating`` from the ``authors`` table to the
``author_avg_rating`` view using a manual relationship:
.. rst-class:: api_tabs
@ -361,7 +361,7 @@ Let us now create an ``object relationship`` called ``avg_rating`` from the ``au
**Step 1: Open the manual relationship section**
- Open the console and navigate to the ``Data -> author -> Relationships`` tab.
- Open the console and navigate to the ``Data -> authors -> Relationships`` tab.
- Click on the ``Configure`` button:
.. thumbnail:: /img/graphql/core/schema/manual-relationship-btn.png
@ -393,10 +393,10 @@ Let us now create an ``object relationship`` called ``avg_rating`` from the ``au
- table:
schema: public
name: article
name: articles
- table:
schema: public
name: author
name: authors
object_relationships:
- name: avg_rating
using:
@ -429,7 +429,7 @@ Let us now create an ``object relationship`` called ``avg_rating`` from the ``au
{
"type": "create_object_relationship",
"args": {
"table": "author",
"table": "authors",
"name": "avg_rating",
"using": {
"manual_configuration": {
@ -450,7 +450,7 @@ Fetch a list of authors with the average rating of their articles:
:view_only:
:query:
query {
author {
authors {
id
name
avg_rating {
@ -461,7 +461,7 @@ Fetch a list of authors with the average rating of their articles:
:response:
{
"data": {
"author": [
"authors": [
{
"id": 1,
"name": "Justin",
@ -514,14 +514,14 @@ As mentioned in the Introduction section above, relationships can be inferred vi
- table:
schema: public
name: article
name: articles
object_relationships:
- name: author
using:
foreign_key_constraint_on: author_id
- table:
schema: public
name: author
name: authors
array_relationships:
- name: articles
using:
@ -529,7 +529,7 @@ As mentioned in the Introduction section above, relationships can be inferred vi
column: author_id
table:
schema: public
name: article
name: articles
Apply the metadata by running:
@ -554,7 +554,7 @@ As mentioned in the Introduction section above, relationships can be inferred vi
{
"type": "create_object_relationship",
"args": {
"table": "article",
"table": "articles",
"name": "author",
"using": {
"foreign_key_constraint_on": "author_id"
@ -564,11 +564,11 @@ As mentioned in the Introduction section above, relationships can be inferred vi
{
"type": "create_array_relationship",
"args": {
"table": "author",
"table": "authors",
"name": "articles",
"using": {
"foreign_key_constraint_on" : {
"table" : "article",
"table" : "articles",
"column" : "author_id"
}
}

View File

@ -18,7 +18,7 @@ Introduction
To make :ref:`nested object queries <nested_object_queries>`, the tables/views in your database need to
be connected via relationships.
Let's say we have the following tables in our database: ``author``, ``passport_info``, ``article`` and ``tag``.
Let's say we have the following tables in our database: ``authors``, ``passport_infos``, ``articles`` and ``tags``.
Table relationships
-------------------
@ -26,18 +26,18 @@ Table relationships
The tables/views in any relational database are typically related to each other via one of the
following types of table relationships:
+------------------+-----------------------------------+------------------------------------------------+
| Type | Example | Meaning |
+==================+===================================+================================================+
| ``one-to-one`` | ``author`` and ``passport_info`` | - an ``author`` can have one ``passport_info`` |
| | | - a ``passport_info`` can have one ``owner`` |
+------------------+-----------------------------------+------------------------------------------------+
| ``one-to-many`` | ``author`` and ``article`` | - an ``author`` can have many ``articles`` |
| | | - an ``article`` can have one ``author`` |
+------------------+-----------------------------------+------------------------------------------------+
| ``many-to-many`` | ``article`` and ``tag`` | - an ``article`` can have many ``tags`` |
| | | - a ``tag`` can have many ``articles`` |
+------------------+-----------------------------------+------------------------------------------------+
+------------------+-------------------------------------+------------------------------------------------+
| Type | Example | Meaning |
+==================+=====================================+================================================+
| ``one-to-one`` | ``authors`` and ``passport_infos`` | - an ``author`` can have one ``passport_info`` |
| | | - a ``passport_info`` can have one ``owner`` |
+------------------+-------------------------------------+------------------------------------------------+
| ``one-to-many`` | ``authors`` and ``articles`` | - an ``author`` can have many ``articles`` |
| | | - an ``article`` can have one ``author`` |
+------------------+-------------------------------------+------------------------------------------------+
| ``many-to-many`` | ``articles`` and ``tags`` | - an ``article`` can have many ``tags`` |
| | | - a ``tag`` can have many ``articles`` |
+------------------+-------------------------------------+------------------------------------------------+
.. _graphql_relationships:

View File

@ -32,14 +32,14 @@ An existing relationship can be renamed as follows:
- table:
schema: public
name: article
name: articles
object_relationships:
- name: author
using:
foreign_key_constraint_on: author_id
- table:
schema: public
name: author
name: authors
Apply the metadata by running:
@ -60,7 +60,7 @@ An existing relationship can be renamed as follows:
{
"type": "rename_relationship",
"args": {
"table": "article",
"table": "articles",
"name": "article_details",
"new_name": "article_detail"
}

View File

@ -22,7 +22,7 @@ Adding tables allows you to define the GraphQL types of your schema including th
Creating tables
---------------
Let's say we want to create two simple tables for an article/author schema:
Let's say we want to create two simple tables for ``articles`` and ``author`` schema:
.. code-block:: sql
@ -31,7 +31,7 @@ Let's say we want to create two simple tables for an article/author schema:
name TEXT
)
article (
articles (
id SERIAL PRIMARY KEY,
title TEXT,
content TEXT,
@ -47,7 +47,7 @@ Let's say we want to create two simple tables for an article/author schema:
Open the Hasura console and head to the ``Data`` tab and click the ``Create Table`` button to open up an interface to
create tables.
For example, here is the schema for the ``article`` table in this interface:
For example, here is the schema for the ``articles`` table in this interface:
.. thumbnail:: /img/graphql/core/schema/create-table-graphql.png
:alt: Schema for an article table
@ -58,13 +58,13 @@ Let's say we want to create two simple tables for an article/author schema:
.. code-block:: sql
CREATE TABLE article(id serial NOT NULL, title text NOT NULL, content text NOT NULL, rating integer NOT NULL, author_id serial NOT NULL, PRIMARY KEY (id));
CREATE TABLE articles(id serial NOT NULL, title text NOT NULL, content text NOT NULL, rating integer NOT NULL, author_id serial NOT NULL, PRIMARY KEY (id));
2. Add the following statement to the ``down.sql`` file in case you need to :ref:`roll back <roll_back_migrations>` the above statement:
.. code-block:: sql
DROP TABLE article;
DROP TABLE articles;
3. Apply the migration by running:
@ -85,7 +85,7 @@ Let's say we want to create two simple tables for an article/author schema:
{
"type": "run_sql",
"args": {
"sql": "CREATE TABLE article(id serial NOT NULL, title text NOT NULL, content text NOT NULL, rating integer NOT NULL, author_id serial NOT NULL, PRIMARY KEY (id));"
"sql": "CREATE TABLE articles(id serial NOT NULL, title text NOT NULL, content text NOT NULL, rating integer NOT NULL, author_id serial NOT NULL, PRIMARY KEY (id));"
}
}
@ -116,10 +116,10 @@ In order to expose a table over the GraphQL API, it needs to be **tracked**.
- table:
schema: public
name: author
name: authors
- table:
schema: public
name: article
name: articles
2. Apply the metadata by running:
@ -141,7 +141,7 @@ In order to expose a table over the GraphQL API, it needs to be **tracked**.
"type": "track_table",
"args": {
"schema": "public",
"name": "article"
"name": "articles"
}
}
@ -151,13 +151,13 @@ Generated GraphQL schema types
As soon as a table is created and tracked, the corresponding GraphQL schema types
and query/mutation fields will be automatically generated.
The following object type is generated for the ``article``
The following object type is generated for the ``articles``
table we just created and tracked:
.. code-block:: graphql
# Object type
type Article {
type Articles {
id: Int
title: String
content: String
@ -167,40 +167,40 @@ table we just created and tracked:
Let's analyze the above type:
- ``Article`` is the name of the type
- ``id``, ``title``, ``content``, ``rating`` and ``author_id`` are fields of the ``Article`` type
- ``Articles`` is the name of the type
- ``id``, ``title``, ``content``, ``rating`` and ``author_id`` are fields of the ``Articles`` type
- ``Int`` and ``String`` are types that fields can have
The following query/mutation fields are generated for the ``article``
The following query/mutation fields are generated for the ``articles``
table we just created and tracked:
.. code-block:: graphql
# Query field
article (
where: article_bool_exp
articles (
where: articles_bool_exp
limit: Int
offset: Int
order_by: [article_order_by!]
): [article!]!
order_by: [articles_order_by!]
): [articles!]!
# insert/upsert mutation field
insert_article (
objects: [article_insert_input!]!
on_conflict: article_on_conflict
): article_mutation_response
insert_articles (
objects: [articles_insert_input!]!
on_conflict: articles_on_conflict
): articles_mutation_response
# update mutation field
update_article (
where: article_bool_exp!
_inc: article_inc_input
_set: article_set_input
): article_mutation_response
update_articles (
where: articles_bool_exp!
_inc: articles_inc_input
_set: articles_set_input
): articles_mutation_response
# delete mutation field
delete_article (
where: article_bool_exp!
): article_mutation_response
delete_articles (
where: articles_bool_exp!
): articles_mutation_response
These auto-generated fields will allow you to query and mutate data
in our table.
@ -215,13 +215,13 @@ At this point, you should be able to try out basic GraphQL queries/mutations on
the newly created tables from the GraphiQL tab in the console. *(You may want to add some
sample data into the tables first)*
- Query all rows in the ``article`` table:
- Query all rows in the ``articles`` table:
.. graphiql::
:view_only:
:query:
query {
article {
articles {
id
title
author_id
@ -230,7 +230,7 @@ sample data into the tables first)*
:response:
{
"data": {
"article": [
"articles": [
{
"id": 1,
"title": "sit amet",
@ -288,4 +288,6 @@ sample data into the tables first)*
}
}
Note that the author's ``id`` does not need to passed as an input as it is of type ``serial`` (auto incrementing integer).
.. note::
author's ``id`` does not need to be passed as an input as it is of type ``serial`` (auto incrementing integer).

Binary file not shown.

Before

Width:  |  Height:  |  Size: 24 KiB

After

Width:  |  Height:  |  Size: 56 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 27 KiB

After

Width:  |  Height:  |  Size: 64 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 19 KiB

After

Width:  |  Height:  |  Size: 41 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 50 KiB

After

Width:  |  Height:  |  Size: 49 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 20 KiB

After

Width:  |  Height:  |  Size: 57 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 9.5 KiB

After

Width:  |  Height:  |  Size: 19 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 9.5 KiB

After

Width:  |  Height:  |  Size: 19 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 11 KiB

After

Width:  |  Height:  |  Size: 23 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 13 KiB

After

Width:  |  Height:  |  Size: 31 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 23 KiB

After

Width:  |  Height:  |  Size: 42 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 11 KiB

After

Width:  |  Height:  |  Size: 23 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 9.9 KiB

After

Width:  |  Height:  |  Size: 17 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 10 KiB

After

Width:  |  Height:  |  Size: 19 KiB