You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Aggregations can be used to compute metrics such as counts, sums, or grouped totals without fetching every document individually. Aggregations are exposed through <ExternalLink to="https://www.apollographql.com/docs/technotes/TN0029-relay-style-connections/" text="Relay-style"/> connection queries: every collection type includes an `aggregate` field under its `<plural>_connection` query.
431
+
432
+
```graphql title="Example: Total restaurants matching a filter"
Aggregations follow the same filters, locale, publication status, and permissions as the parent query. For example, setting `locale:"fr"` or `status:DRAFT` on the connection limits the aggregation to those documents, and users can only aggregate content they are allowed to read.
443
+
444
+
The table below lists all supported aggregation operators:
445
+
446
+
| Operator | Description | Supported field types |
447
+
| --- | --- | --- |
448
+
| `count` | Returns the number of documents that match the query. | All content-types |
449
+
| `avg` | Computes the arithmetic mean per numeric field. | Number, integer, decimal |
450
+
| `sum` | Computes the total per numeric field. | Number, integer, decimal |
451
+
| `min` | Returns the smallest value per field. | Number, integer, decimal, date, datetime |
452
+
| `max` | Returns the largest value per field. | Number, integer, decimal, date, datetime |
453
+
| `groupBy` | Buckets results by unique values and exposes nested aggregations for each bucket. | Scalar fields (string, number, boolean, date, datetime), relations |
454
+
455
+
:::note
456
+
Strapi ignores `null` values for `avg`, `sum`, `min`, and `max`. When aggregating relations, the operators run on the target documents and still respect their locales and permissions.
457
+
:::
458
+
459
+
:::info Performance & limits
460
+
Aggregations operate server-side, so they are generally faster than downloading and processing large result sets on the client. However, complex `groupBy` trees and wide projections can still be expensive. Use filters to restrict the data set and consider setting up `depthLimit` and `amountLimit` values accordingly (see [available options](/cms/plugins/graphql#available-options)) to protect your API. Errors such as `You are not allowed to perform this action` usually mean the requester lacks the `Read` permission on the target collection.
461
+
:::
462
+
463
+
### Aggregate multiple metrics in one request
464
+
465
+
Aggregations can be combined so that one network round trip returns several metrics:
466
+
467
+
```graphql title="Example: Average delivery time and minimum price"
Use `groupBy` to derive grouped metrics while optionally chaining further aggregations inside each group. Each group exposes the unique `key` and a nested connection that can be used for drilling down or counting the grouped items:
488
+
489
+
```graphql title="Example: Count restaurants per category"
490
+
{
491
+
restaurants_connection {
492
+
aggregate {
493
+
groupBy {
494
+
categories {
495
+
key
496
+
connection {
497
+
aggregate {
498
+
count
499
+
}
500
+
}
501
+
}
502
+
}
503
+
}
504
+
}
505
+
}
506
+
```
507
+
508
+
Groups inherit the top-level filters. To further refine a specific group, apply filters on the nested `connection`.
509
+
510
+
### Combine with pagination and sorting
511
+
512
+
Aggregations run on the entire result set that matches the query filters, not only on the current page. When a request includes pagination arguments and aggregations, the documents in `nodes` follow the pagination limits, but the values inside `aggregate` ignore `pageSize` or `limit` so they describe the whole set. You can still add sorting to order the documents returned with the aggregation results.
513
+
514
+
```graphql title="Example: Paginate takeaway restaurants and count all matches"
515
+
{
516
+
restaurants_connection(
517
+
filters: { takeAway: { eq:true } }
518
+
pagination: { page:2, pageSize:5 }
519
+
sort:"name:asc"
520
+
) {
521
+
nodes {
522
+
documentId
523
+
name
524
+
rating
525
+
}
526
+
pageInfo {
527
+
page
528
+
pageSize
529
+
total
530
+
}
531
+
aggregate {
532
+
count
533
+
avg {
534
+
rating
535
+
}
536
+
}
537
+
}
538
+
}
539
+
```
540
+
428
541
## Mutations
429
542
430
543
Mutations in GraphQL are used to modify data (e.g. create, update, and delete data).
Copy file name to clipboardExpand all lines: docusaurus/docs/cms/api/graphql/advanced-queries.md
+29Lines changed: 29 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -50,3 +50,32 @@ export default {
50
50
```
51
51
52
52
In this example the parent resolver fetches restaurants using the [Document Service API](/cms/api/document-service), then delegates to the generated `Restaurant` resolver provided by the plugin so default behavior such as field selection still applies.
53
+
54
+
## Aggregation drilldowns
55
+
56
+
<ExternalLinkto="https://www.apollographql.com/docs/technotes/TN0029-relay-style-connections/"text="Relay-style"/> connections expose an `aggregate` field. You can use it to show high-level metrics and still let users drill into a specific group. Combine `groupBy` with pagination variables to keep responses small:
Use query variables to adjust the nested pagination when a user selects a group. Strapi runs the aggregation on the server, which is why the client only downloads the summary rows it needs: this keeps dashboards responsive even with large data sets.
0 commit comments