Skip to content

Nested entity queries

Igor Dianov edited this page May 29, 2024 · 1 revision

You can use the object (one-to-one) or array (one-to-many/many-to-many) ORM associations defined in your schema to make a nested query i.e. fetch data for a type along with data from a nested or related type.

The name of the nested object is the same as the name of the object/array relationship configured in the JPA entity class.

Fetch nested object using an object relationship

The following is an example of a nested object query using the object relationship between an book and an author.

Example: Fetch a list of books and the name of each article’s author:

query {
  Books {
    select {
      id
      title
      author {
        name
      }
    }
  }
}
{
  "data": {
    "Books": {
      "select": [
        {
          "id": 2,
          "title": "War and Peace",
          "author": {
            "name": "Leo Tolstoy"
          }
        },
        {
          "id": 3,
          "title": "Anna Karenina",
          "author": {
            "name": "Leo Tolstoy"
          }
        },
        {
          "id": 5,
          "title": "The Cherry Orchard",
          "author": {
            "name": "Anton Chekhov"
          }
        },
        {
          "id": 6,
          "title": "The Seagull",
          "author": {
            "name": "Anton Chekhov"
          }
        },
        {
          "id": 7,
          "title": "Three Sisters",
          "author": {
            "name": "Anton Chekhov"
          }
        }
      ]
    }
  }
}