Dgraph automatically generates aggregate queries for GraphQL schemas. These are compatible with the @auth directive
@auth
directive and follow the same
authorization rules as the query
keyword. You can also use filters with
aggregate queries, as shown in some of the examples provided below.
type
defined in a GraphQL schema, Dgraph generates an aggregate
query aggregate<type name>
. This query includes a count
field, as well as
advanced aggregate query fields.
Example: fetch the total number of posts
.
posts
whose titles contain GraphQL
.
<field name>Aggregate
fields for every field which is of
type List[Type/Interface]
inside query<type name>
queries, allowing you to
do a count
on fields, or to use the
advanced aggregate queries.
Example: fetch the number of posts
for all authors along with their name
.
posts
with a score
greater than 10
for all
authors, along with their name
type
defined in the GraphQL schema, Dgraph generates an aggregate
query aggregate<type name>
that includes advanced aggregate query fields, and
also includes a count
field (see
Count queries at root). Dgraph generates one or more
advanced aggregate query fields (<field-name>Min
, <field-name>Max
,
<field-name>Sum
and <field-name>Avg
) for fields in the schema that are typed
as Int
, Float
, String
and Datetime
.
Int
and Float
get the following query
fields: <field name>Max
, <field name>Min
, <field name>Sum
and
<field name>Avg
. Fields typed as String
and Datetime
only get the
<field name>Max
, <field name>Min
query fields. posts
written by authors:
posts
by all authors, and the maximum
number of posts
by any single Author
:
posts
for authors with more than 20
friends
:
<field name>Aggregate
fields for child nodes
within query<type name>
queries. This is done for each field of type
List[Type/Interface]
inside query<type name>
queries, letting you fetch
minimums, maximums, averages and sums for those fields.
Int
and Float
get the following query
fields:<field name>Max
, <field name>Min
, <field name>Sum
and
<field name>Avg
. Fields typed as String
and Datetime
only get the
<field name>Max
, <field name>Min
query fields. score
of the posts
for each
Author
, along with each author’s name
.
score
greater than 10
for all authors, along with the author’s name
.
null
. This is true for both the
<field name>Aggregate
fields and aggregate<type name>
queries generated by
Dgraph.
So, in these examples,the following is true:
Author
, the aggregateAuthor
query returns
null.Author
hasn’t written any posts, the field postsAggregate
is null
for that Author
.