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!]!
postsAggregate(where: PostWhere): PostAggregationSelection!
users(where: UserWhere, sort: [UserSort!]!, limit: Int, offset: Int,): [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. |
|
Longest User name
|
Numeric (e.g. |
|
Example query
|
Temporal (e.g. |
|
First Post date
|
The argument |
Aggregate related nodes
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.
query {
usersAggregate {
count
}
}
query {
usersAggregate(where: { name_STARTS_WITH: "J" }) {
count
}
}
query {
users {
id
postsAggregate {
count
}
}
}
By using the node
field, related nodes properties can be aggregated:
query {
users {
name
postsAggregate {
node {
content {
longest
}
}
}
}
}