You are looking at the docs for the unreleased master branch of Dgraph. The latest version is v20.07.
Report Issue Edit Page

Count Queries

Dgraph automatically generates count queries for a given GraphQL schema, enabling you to count on fields and to count nodes satisfying certain criteria specified using a filter. Count Queries are also compatible with the @auth directive and honor the same authorization rules as query.

Count at root

For every type defined in GraphQL, Dgraph generates an aggregate query aggregate<type name>. This query includes a count field.

Examples

Example - Fetch the number of posts.

   query {
     aggregatePost {
       count
     }
   }

Example - Fetch the number of posts whose titles contain GraphQL.

   query {
     aggregatePost(filter: {
       title: {
         anyofterms: "GraphQL"
         }
       }) {
       count
     }
   }

Count for a child

Besides the aggregate<type name> query, Dgraph defines <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.

Examples

Example - Fetch the number of posts for all authors along with their name.

   query {
     queryAuthor {
       name
       postsAggregate {
        count
       }
     }
   }

Example - Fetch the number of posts with a score greater than 10 for all authors along with their name

   query {
     queryAuthor {
       name
       postsAggregate(filter: {
         score: {
           gt: 10
         }
       }) {
        count
      }
    }
  }