Aggregate Queries

GraphQLize supports the standard aggregate functions count, max, min, sum & avg. We can also use the GROUP-BY operation along with these aggregate functions.

query {
courses {
countOfRating
avgOfRating
maxOfRating
minOfRating
sumOfRating
}
}

The aggregate functions over columns are available as fields in the corresponding type representing the table.

type Course {
# ...
rating: Int!
# ... aggregate functions as fields
avgOfRating: BigDecimal
countOfRating: Int!
maxOfRating: Int
minOfRating: Int
sumOfRating: Int
# ...
}

Naming Convention

As seen in the above example, GraphQLize uses the name of the aggregate function (in camel case) along with the preposition Of as prefix to represent the aggregate functions as fields.

Column Name Aggregate Function Field
first_name count countOfFirstName
payment sum sumOfPayment

Aggregates Over Relationships

We can use the aggregate functions over the relationship fields as well.

Let's assume that we have a schema like below

We can get the total courses count and the average course rating of all the authors along with their first-name and last-name, using the following GraphQL query

query {
authors {
id
firstName
courses {
countOfId
avgOfRating
}
}
}
note

The countOfId field translates to count(id) in the SQL's select clause. The count of all rows via count(*) will be added in a future release.

GROUP BY

The aggregate functions often paired along with the GROUP BY operation and GraphQLize supports it out of the box!

For the above schema, we can group the courses by their rating and get their count using the following query

query {
courses(groupBy: [rating]) {
rating
countOfRating
}
}