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(locale: en) {
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 ;ist, the items for each parent will be retrieved and shown in a single response.
{
BloglistPageType(locale: en, where: {Title: {startsWith: "Optimizely"}}) {
items {
Name
Language
_children {
BlogitemPageType(where: {Created: {lt: "2019"}}) {
items {
Name
}
}
}
}
}
}
Updated about 2 months ago