London Dev Meetup Rescheduled! Due to unavoidable reasons, the event has been moved to 21st May. Speakers remain the same—any changes will be communicated. Seats are limited—register here to secure your spot!

Loading...

Parent Child Queries

When there is a parent-child relationship between GQL schemas, it is possible to create a single query to retrieve parent documents and their children documents. This is made possible with a special field in the projection called _children. With _children, the child query can be formulated with the child schema using arguments similarly as with regular GQL queries. 

Example:

Here the Parent query is used with the StartPage schema and the Child query is used with the BiographyPage schema.

{
  StartPage {
    total
    items {
      _children {
        BiographyPage {
          items { 
            Name
          }
        }
      }
      Name
    }
  }
}

And this query has this response:

{
  "data": {
    "StartPage": {
      "total": 4,
      "items": [
        {
          "_children": {
            "BiographyPage": {
              "items": [
                {
                  "Name": "Abraham Lincoln"
                }
              ]
            }
          },
          "Name": "Start"
        }
      ]
    }
  },
  "extensions": {
    "requestId": "4b45a5b3-f905-4824-b903-5b9180376837",
    "responseTime": 111
  }
}

Another example. Here the Parent query is used with the BloglistPageType schema and the Child query is used with the BlogitemPageType schema. So for every matching parent item in the Blog List, the items for each parent will be retrieved and shown in a single response. 

{
  BloglistPageType(where: {Title: {startsWith: "Optimizely"}}) {
    items {
      Name
      Language
      _children {
        BlogitemPageType(where: {Created: {lt: "2019"}}) {
          items {
            Name
          }
        }
      }
    }
  }
}

Last updated: Apr 08, 2022