Only fields decorated with
@FilterableField will be exposed in aggregate queries.TodoItem DTO. All fields except age, because it is not decorated with @FilterableField or @IDField, will be exposed in aggregate queries.
todo-item/dto/todo-item.dto.ts
Enabling Aggregate Queries
To enable aggregate queries you can set theenableAggregate option in your resolver
todo-item/todo-item.module.ts
${objectName}Aggregate.
Below is a fragment from the generated schema for TodoItem
nestjs-query will only expose number fields for avg and sum.Examples
Basic
- GraphQL
- Response
With GroupBy
To group your aggregate queries you can add agroupBy to specify one or more fields to group on.
- GraphQL
- Response
With Filter
You can also provide a filter to only aggregate on a subset of data.- GraphQL
- Response
count aggregate only non-null fields will be counted.
For example assume description is null for all todo items you will get 0 back.
- GraphQL
- Response
Aggregating Relations
When using theenableAggregate option any defined many relations will also expose a aggregate query {relationName}Aggregate
Building on the previous example assume TodoItem has a subTasks connection.
The following schema fragment will be created
Examples
Basic
In this example we’ll aggregate on all relatedsubTasks.
- GraphQL
- Response
With GroupBy
In this example we’ll aggregate on all relatedsubTasks and group by completed.
- GraphQL
- Response
With Filter
This example will aggregate all relatedsubTasks that are not completed.
- GraphQL
- Response
Advanced
Enabling Aggregates Only For Root
When using theenableAggregate option it will enable aggregates on the root type as well as all relations. If you only want to expose aggregate functionality on the root type you can specify the aggregate option.
Disable Aggregate for Single Relation
You can also selectively disable aggregates on an individual relation by specifying the enableAggregate option when defining the relation.Group aggregate on DAY/WEEK/MONTH
You can change how datetime fields are grouped by passingWEEK, MONTH or YEAR to the date fields by.