Aggregations

Type definitions

Quries on this page assume the following type definitions:

type Post {
    id: ID! @id
    content: String!
    creator: User! @relationship(type: "HAS_POST", direction: IN, properties: "PostedAt")
    createdAt: DateTime!
}

type User {
    id: ID! @id
    name: String!
    age: Int!
    posts: [Post!]! @relationship(type: "HAS_POST", direction: OUT, properties: "PostedAt")
    friends: [User!]! @relationship(type: "FRIENDS_WITH", direction: OUT)
}

type PostedAt @relationshipProperties {
    date: DateTime
}

For which the following query fields are generated:

type Query {
    posts(where: PostWhere, options: PostOptions): [Post!]!
    postsAggregate(where: PostWhere): PostAggregationSelection!

    users(where: UserWhere, options: UserOptions): [User!]!
    usersAggregate(where: UserWhere): UserAggregationSelection!
}

Aggregation fields

Based on the type definitions, here is a list of fields that accept aggregations supported by Neo4j GraphQL:

Type Aggregating function Example

String (e.g. ID, String)

shortest, longest

Longest User name
query {
    usersAggregate {
        name {
            longest
        }
    }
}

Numeric (e.g. Int, Float, BigInt)

min, max, average, sum

Example query
query {
  usersAggregate {
    age {
      average
    }
  }
}

Temporal (e.g. DateTime, Time, LocalTime, LocalDateTime, Duration)

min, max

First Post date
query {
    postsAggregate {
        createdAt {
            min
        }
    }
}

The argument where can also be used in aggregation queries for filtering data.

Related nodes can also be aggregated within a query by accessing the aggregation fields in the node. In these fields, you can count, aggregate the nodes or edges fields.

The same selections and types as before are available in relationship aggregations.

Counting User nodes
query {
    usersAggregate {
        count
    }
}
Counting User nodes where name starts with "J"
query {
    usersAggregate(where: { name_STARTS_WITH: "J" }) {
        count
    }
}
Counting all posts per User
query {
    users {
        id
        postsAggregate {
            count
        }
    }
}

By using the node field, related nodes properties can be aggregated:

Finding longest post per User
query {
    users {
        name
        postsAggregate {
            node {
                content {
                  longest
                }
            }
        }
    }
}

Aggregate relationships

Relationship properties can be aggregated as well by using the edge field:

Querying what User nodes posted up to a date
query {
    users {
        name
        postsAggregate {
            edge {
              date {
                max
              }
            }
        }
    }
}

When performing an aggregation on related nodes, the query against the relationship can be defined as "undirected" by using the argument directed: false:

query {
    users {
        id
        postsAggregate(directed: false) {
            count
        }
    }
}