Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: page on tracking relationships (close #2726) #5955

Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/graphql/core/schema/table-relationships/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -82,5 +82,6 @@ The following guides will help you model the different types of table relationsh
:hidden:

create
track
rename
Database modelling <database-modelling/index>
156 changes: 156 additions & 0 deletions docs/graphql/core/schema/table-relationships/track.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
.. meta::
:description: Track relationships in Hasura
:keywords: hasura, docs, schema, relationship, track

.. _track_relationships:

Tracking relationships
======================

.. contents:: Table of contents
:backlinks: none
:depth: 2
:local:

Introduction
------------

It is possible to have relationships in your underlying database without having them tracked and exposed over the GraphQL API.
For example, this can be the case when you import a database schema into Hasura. This page explains how to track relationships and therefore expose them over the GraphQL API.

.. _track_single_relationships:

Tracking a single relationship
------------------------------

Assuming the relevant tables are tracked, but not the relationships, a single relationship can be tracked as follows:

.. rst-class:: api_tabs
.. tabs::

.. tab:: Console

On the ``Data`` tab in the Hasura console, click the ``Track`` button for the relationships you'd like to track:

.. thumbnail:: /img/graphql/core/schema/track-single-relationship.png
:alt: Track single relationship
:width: 700px

.. tab:: CLI

You can track relationships in the ``tables.yaml`` file inside the ``metadata`` directory:

**Object relationship**

.. code-block:: yaml
:emphasize-lines: 4-7

- table:
schema: public
name: article
object_relationships:
- name: author
using:
foreign_key_constraint_on: author_id
- table:
schema: public
name: author

**Array relationship**

.. code-block:: yaml
:emphasize-lines: 11-18

- table:
schema: public
name: article
object_relationships:
- name: author
using:
foreign_key_constraint_on: author_id
- table:
schema: public
name: author
array_relationships:
- name: articles
using:
foreign_key_constraint_on:
column: author_id
table:
schema: public
name: article

Apply the metadata by running:

.. code-block:: bash

hasura metadata apply

.. tab:: API

You can track an object relationship by using the :ref:`create_object_relationship metadata API <create_object_relationship>`:

.. code-block:: http

POST /v1/query HTTP/1.1
Content-Type: application/json
X-Hasura-Role: admin

{
"type": "create_object_relationship",
"args": {
"table": "article",
"name": "author",
"using": {
"foreign_key_constraint_on": "author_id"
}
}
}

You can track an array relationship by using the :ref:`create_array_relationship metadata API <create_array_relationship>`:

.. code-block:: http

POST /v1/query HTTP/1.1
Content-Type: application/json
X-Hasura-Role: admin

{
"type": "create_array_relationship",
"args": {
"table": "author",
"name": "articles",
"using": {
"foreign_key_constraint_on" : {
"table" : "article",
"column" : "author_id"
}
}
}
}

Tracking all relationships
--------------------------

Assuming the relevant tables are tracked, but not the relationships, all available relationships can be tracked as follows:

.. rst-class:: api_tabs
.. tabs::

.. tab:: Console

On the ``Data`` tab in the Hasura console, click the button ``Track All``:

.. thumbnail:: /img/graphql/core/schema/track-all-relationships.png
:alt: Track all relationships
:width: 700px

.. tab:: CLI

When working with the CLI, all relationships must be added manually, as described in the :ref:`previous section <track_single_relationships>`.
You can use a script to automate this process.

.. tab:: API

When working with the API, all relationships must be added manually, as described in the :ref:`previous section <track_single_relationships>`.
You can use a script to automate this process.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.