2018-09-11 14:11:24 +03:00
|
|
|
|
Nested object queries
|
|
|
|
|
=====================
|
|
|
|
|
|
2018-12-03 15:12:24 +03:00
|
|
|
|
.. contents:: Table of contents
|
|
|
|
|
:backlinks: none
|
|
|
|
|
:depth: 1
|
|
|
|
|
:local:
|
|
|
|
|
|
2018-09-11 14:11:24 +03:00
|
|
|
|
You can use the object (one-to-one) or array (one-to-many) :doc:`relationships <../schema/relationships/index>` 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.
|
|
|
|
|
|
2018-11-15 14:43:03 +03:00
|
|
|
|
|
2018-12-03 15:12:24 +03:00
|
|
|
|
Fetch nested object using an object relationship
|
|
|
|
|
------------------------------------------------
|
2018-12-17 14:58:29 +03:00
|
|
|
|
The following is an example of a nested object query using the **object relationship** between an article and an
|
2018-11-15 14:43:03 +03:00
|
|
|
|
author.
|
|
|
|
|
|
|
|
|
|
Fetch a list of articles and the name of each article’s author:
|
|
|
|
|
|
|
|
|
|
.. graphiql::
|
|
|
|
|
:view_only:
|
|
|
|
|
:query:
|
|
|
|
|
query {
|
|
|
|
|
article {
|
|
|
|
|
id
|
|
|
|
|
title
|
|
|
|
|
author {
|
|
|
|
|
name
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
:response:
|
|
|
|
|
{
|
|
|
|
|
"data": {
|
|
|
|
|
"article": [
|
|
|
|
|
{
|
|
|
|
|
"id": 1,
|
|
|
|
|
"title": "sit amet",
|
|
|
|
|
"author": {
|
|
|
|
|
"name": "Anjela"
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
"id": 2,
|
|
|
|
|
"title": "a nibh",
|
|
|
|
|
"author": {
|
|
|
|
|
"name": "Beltran"
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
"id": 3,
|
|
|
|
|
"title": "amet justo morbi",
|
|
|
|
|
"author": {
|
|
|
|
|
"name": "Anjela"
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
]
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-03 15:12:24 +03:00
|
|
|
|
Fetch nested objects using an array relationship
|
|
|
|
|
------------------------------------------------
|
2018-12-17 14:58:29 +03:00
|
|
|
|
The following is an example of a nested object query using the **array relationship** between an author and
|
2018-09-11 14:11:24 +03:00
|
|
|
|
articles.
|
|
|
|
|
|
2018-10-12 07:00:25 +03:00
|
|
|
|
Fetch a list of authors and a nested list of each author’s articles:
|
2018-09-11 14:11:24 +03:00
|
|
|
|
|
|
|
|
|
.. graphiql::
|
|
|
|
|
:view_only:
|
|
|
|
|
:query:
|
|
|
|
|
query {
|
|
|
|
|
author {
|
|
|
|
|
id
|
|
|
|
|
name
|
|
|
|
|
articles {
|
|
|
|
|
id
|
|
|
|
|
title
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
:response:
|
|
|
|
|
{
|
|
|
|
|
"data": {
|
|
|
|
|
"author": [
|
|
|
|
|
{
|
|
|
|
|
"id": 1,
|
|
|
|
|
"name": "Justin",
|
|
|
|
|
"articles": [
|
|
|
|
|
{
|
|
|
|
|
"id": 15,
|
|
|
|
|
"title": "vel dapibus at"
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
"id": 16,
|
|
|
|
|
"title": "sem duis aliquam"
|
|
|
|
|
}
|
|
|
|
|
]
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
"id": 2,
|
|
|
|
|
"name": "Beltran",
|
|
|
|
|
"articles": [
|
|
|
|
|
{
|
|
|
|
|
"id": 2,
|
|
|
|
|
"title": "a nibh"
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
"id": 9,
|
|
|
|
|
"title": "sit amet"
|
|
|
|
|
}
|
|
|
|
|
]
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
"id": 3,
|
|
|
|
|
"name": "Sidney",
|
|
|
|
|
"articles": [
|
|
|
|
|
{
|
|
|
|
|
"id": 6,
|
|
|
|
|
"title": "sapien ut"
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
"id": 11,
|
|
|
|
|
"title": "turpis eget"
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
"id": 14,
|
|
|
|
|
"title": "congue etiam justo"
|
|
|
|
|
}
|
|
|
|
|
]
|
|
|
|
|
}
|
|
|
|
|
]
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2018-11-15 14:43:03 +03:00
|
|
|
|
.. note::
|
|
|
|
|
|
|
|
|
|
The name of the nested object is the same as the name of the object or array relationship configured in the
|
|
|
|
|
console.
|
|
|
|
|
|
2018-12-03 15:12:24 +03:00
|
|
|
|
Fetch aggregated data on nested objects
|
|
|
|
|
---------------------------------------
|
2018-12-17 14:58:29 +03:00
|
|
|
|
The following is an example of a nested object query with aggregations on the **array relationship** between an author
|
|
|
|
|
and articles.
|
2018-11-15 14:43:03 +03:00
|
|
|
|
|
2018-12-03 15:12:24 +03:00
|
|
|
|
Fetch an author whose id is ``1`` and a nested list of articles with aggregated rating data:
|
2018-09-11 14:11:24 +03:00
|
|
|
|
|
|
|
|
|
.. graphiql::
|
|
|
|
|
:view_only:
|
|
|
|
|
:query:
|
|
|
|
|
query {
|
2018-11-15 14:43:03 +03:00
|
|
|
|
author (where: {id: {_eq: 1}}) {
|
2018-09-11 14:11:24 +03:00
|
|
|
|
id
|
2018-11-15 14:43:03 +03:00
|
|
|
|
name
|
|
|
|
|
articles_aggregate {
|
|
|
|
|
aggregate {
|
|
|
|
|
count
|
|
|
|
|
avg {
|
|
|
|
|
rating
|
|
|
|
|
}
|
|
|
|
|
max {
|
|
|
|
|
rating
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
nodes {
|
|
|
|
|
id
|
|
|
|
|
title
|
|
|
|
|
rating
|
|
|
|
|
}
|
2018-09-11 14:11:24 +03:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
:response:
|
|
|
|
|
{
|
|
|
|
|
"data": {
|
2018-11-15 14:43:03 +03:00
|
|
|
|
"author": [
|
2018-09-11 14:11:24 +03:00
|
|
|
|
{
|
|
|
|
|
"id": 1,
|
2018-11-15 14:43:03 +03:00
|
|
|
|
"name": "Justin",
|
|
|
|
|
"articles_aggregate": {
|
|
|
|
|
"aggregate": {
|
|
|
|
|
"count": 2,
|
|
|
|
|
"avg": {
|
|
|
|
|
"rating": 2.5
|
|
|
|
|
},
|
|
|
|
|
"max": {
|
|
|
|
|
"rating": 4
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
"nodes": [
|
|
|
|
|
{
|
|
|
|
|
"id": 15,
|
|
|
|
|
"title": "vel dapibus at",
|
|
|
|
|
"rating": 4
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
"id": 16,
|
|
|
|
|
"title": "sem duis aliquam",
|
|
|
|
|
"rating": 1
|
|
|
|
|
}
|
|
|
|
|
]
|
2018-09-11 14:11:24 +03:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
]
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-12-17 14:58:29 +03:00
|
|
|
|
|
|
|
|
|
.. note::
|
|
|
|
|
|
|
|
|
|
The name of the :ref:`aggregate field <aggregate_object>` is of the form ``field-name + _aggregate``
|