Aggregations

This is the documentation of the GraphQL Library version 6. For the long-term support (LTS) version 5, refer to GraphQL Library version 5 LTS.

Type definitions

Queries on this page assume the following type definitions:

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

type User @node {
    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, sort: [PostSort!]!, limit: Int, offset: Int,): [Post!]!
    postsConnection(after: String, first: Int, sort: [PostSort], where: PostWhere): PostsConnection!

    users(where: UserWhere, sort: [UserSort!]!, limit: Int, offset: Int,): [User!]!
    usersConnection(after: String, first: Int, sort: [UserSort], where: UserWhere): UsersConnection!
}

Querying aggregations

Aggregations on a type are provided in the field aggregate inside Connection:

query {
    usersConnection {
        aggregate {
            name {
                longest
            }
        }
    }
}

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. String)

shortest, longest

Longest User name
query {
    usersConnection {
        aggregate {
            name {
                longest
            }
        }
    }
}

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

min, max

First Post date
query {
    postsConnection {
        aggregate {
            createdAt {
                min
            }
        }
    }
}
Counting User nodes
query {
    usersConnection {
        aggregate {
            count {
                nodes
            }
        }
    }
}
Counting User nodes where name starts with "J"
query {
    usersConnection(where: { name_STARTS_WITH: "J" }) {
        aggregate {
            count {
                nodes
            }
        }
    }
}

You can aggregate related nodes in a query by accessing the aggregation fields in the connection. In these fields, you can count, aggregate the nodes or edges fields.

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

Counting all posts per User
query {
    users {
        id
        postsConnection {
            aggregate {
                count {
                    nodes
                }
            }
        }
    }
}

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

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

Aggregate relationships

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

Counting edges between posts and users
query {
    users {
        id
        postsConnection {
            aggregate {
                count {
                    edges
                }
            }
        }
    }
}
Querying what User nodes posted up to a date
query {
    users {
        name
        postsConnection {
            aggregate {
                edge {
                    date {
                        max
                    }
                }
            }
        }
    }
}