Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update aggregate documentation for 6.x #239

Open
wants to merge 2 commits into
base: 6.x
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
137 changes: 89 additions & 48 deletions modules/ROOT/pages/queries-aggregations/aggregations.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,32 @@ For which the following query fields are generated:
----
type Query {
posts(where: PostWhere, sort: [PostSort!]!, limit: Int, offset: Int,): [Post!]!
postsAggregate(where: PostWhere): PostAggregationSelection!
postsConnection(after: String, first: Int, sort: [PostSort], where: PostWhere): PostsConnection!

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

== Querying aggregations

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

[source, graphql, indent=0]
----
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:
Expand All @@ -56,28 +75,16 @@ a|
[source, graphql, indent=0]
----
query {
usersAggregate {
name {
longest
usersConnection {
aggregate {
name {
longest
}
}
}
}
----

| Numeric (e.g. `Int`, `Float`, `BigInt`)
| `min`, `max`, `average`, `sum`
a|
.Example query
[source, graphql, indent=0]
----
query {
usersAggregate {
age {
average
}
}
}
----

| Temporal (e.g. `DateTime`, `Time`, `LocalTime`, `LocalDateTime`, `Duration`)
| `min`, `max`
Expand All @@ -86,33 +93,28 @@ a|
[source, graphql, indent=0]
----
query {
postsAggregate {
createdAt {
min
postsConnection {
aggregate {
createdAt {
min
}
}
}
}
----
|===

[NOTE]
====
The argument `where` can also be used in aggregation queries for xref::queries-aggregations/filtering.adoc[filtering] data.
====

== 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.

.Counting User nodes
[source, graphql, indent=0]
----
query {
usersAggregate {
count
usersConnection {
aggregate {
count {
nodes
}
}
}
}
----
Expand All @@ -121,20 +123,38 @@ query {
[source, graphql, indent=0]
----
query {
usersAggregate(where: { name_STARTS_WITH: "J" }) {
count
usersConnection(where: { name_STARTS_WITH: "J" }) {
aggregate {
count {
nodes
}
}
}
}
----


== Aggregating related 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 before are available in relationship aggregations.
Copy link
Contributor

@rsill-neo4j rsill-neo4j Mar 6, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
The same selections and types as before are available in relationship aggregations.
The same selections and types as in xref:#_aggregation_fields[] are available in relationship aggregations.

what does "as before" refer to? asking because a link or even repeating the info might make it easier for readers

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TBH, this is not new documentation, just updating examples in this PR, but I presume it refers to the section "
== Aggregation fields"

Copy link
Contributor

@rsill-neo4j rsill-neo4j Mar 6, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(tricked myself with github views)
updated suggestion




.Counting all posts per User
[source, graphql, indent=0]
----
query {
users {
id
postsAggregate {
count
postsConnection {
aggregate {
count {
nodes
}
}
}
}
}
Expand All @@ -148,10 +168,12 @@ By using the `node` field, related nodes properties can be aggregated:
query {
users {
name
postsAggregate {
node {
content {
longest
postsConnection {
aggregate {
node {
content {
longest
}
}
}
}
Expand All @@ -163,17 +185,36 @@ query {

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

.Counting edges between posts and users
[source, graphql, indent=0]
----
query {
users {
id
postsConnection {
aggregate {
count {
edges
}
}
}
}
}
----

.Querying what User nodes posted up to a date
[source, graphql, indent=0]
----
query {
users {
name
postsAggregate {
edge {
date {
max
}
postsConnection {
aggregate {
edge {
date {
max
}
}
}
}
}
Expand Down