One-to-many relationships

In a one-to-many relationship, one record in a table can be associated with one or more records in another table. In the Hasura data APIs, one-to-many relationships are referred to as array relationships.

For example, in an article author schema, author can have multiple articles.

Creating a one-to-many relationship

To add a one-to-many relationship from author to article,

  1. Create a foreign key constraint on the article table to the author table.
  2. Open the API console, navigate to Data > author > Relationships section.
  3. Add the suggested array relationship and give it a desired name.
../../../../_images/one-to-many-rel.png

Fetching over a one-to-many relationship

To fetch an author with name = "Clara" and all the articles authored by her, use a query like so:

query fetch_author {
   author (where: { name: "Clara" ){
       name
       article {
           title
       }
   }
}
POST data.<cluster-name>.hasura-app.io/v1/query HTTP/1.1
Content-Type: application/json
Authorization: Bearer <auth-token> # optional if cookie is set
X-Hasura-Role: admin

{
    "type" : "select",
    "args" : {
        "table" : "author",
        "columns": [
            "name",
            {
                "name": "article",
                "columns": ["title"]
            }
        ],
        "where" : {
            "name": "Clara"
        }
    }
}