Skip to content

Commit

Permalink
add cli and api workflow
Browse files Browse the repository at this point in the history
  • Loading branch information
marionschleifer committed Oct 8, 2020
1 parent 75ff57c commit 46961aa
Showing 1 changed file with 100 additions and 3 deletions.
103 changes: 100 additions & 3 deletions docs/graphql/core/schema/table-relationships/track.rst
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@ 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.
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.

Tracking a relationship
-----------------------
.. _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:

Expand All @@ -36,8 +38,97 @@ Assuming the relevant tables are tracked, but not the relationships, a single re

.. 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
--------------------------

Expand All @@ -56,4 +147,10 @@ Assuming the relevant tables are tracked, but not the relationships, all availab

.. 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.

0 comments on commit 46961aa

Please sign in to comment.