|
1 | | - |
2 | 1 | In our API, each SQL table is reflected as a set of GraphQL types. At a high level, tables become types and columns/foreign keys become fields on those types. |
3 | 2 |
|
4 | 3 |
|
@@ -167,6 +166,9 @@ Connections wrap a result set with some additional metadata. |
167 | 166 | # Result set |
168 | 167 | edges: [BlogEdge!]! |
169 | 168 |
|
| 169 | + # Aggregate functions |
| 170 | + aggregate: BlogAggregate |
| 171 | + |
170 | 172 | } |
171 | 173 | ``` |
172 | 174 |
|
@@ -264,8 +266,176 @@ Connections wrap a result set with some additional metadata. |
264 | 266 |
|
265 | 267 | The `totalCount` field is disabled by default because it can be expensive on large tables. To enable it use a [comment directive](configuration.md#totalcount) |
266 | 268 |
|
| 269 | +#### Aggregates |
| 270 | + |
| 271 | +Aggregate functions are available on the collection's `aggregate` field when enabled via [comment directive](configuration.md#aggregate). These allow you to perform calculations on the collection of records that match your filter criteria. |
| 272 | + |
| 273 | +The supported aggregate operations are: |
| 274 | + |
| 275 | +- **count**: Always available, returns the number of records matching the query |
| 276 | +- **sum**: Available for numeric fields, returns the sum of values |
| 277 | +- **avg**: Available for numeric fields, returns the average (mean) of values |
| 278 | +- **min**: Available for numeric, string, boolean, and date/time fields, returns the minimum value |
| 279 | +- **max**: Available for numeric, string, boolean, and date/time fields, returns the maximum value |
| 280 | + |
| 281 | +**Example** |
| 282 | + |
| 283 | +=== "Query" |
| 284 | + |
| 285 | + ```graphql |
| 286 | + { |
| 287 | + blogCollection( |
| 288 | + filter: { rating: { gt: 3 } } |
| 289 | + ) { |
| 290 | + aggregate { |
| 291 | + count |
| 292 | + sum { |
| 293 | + rating |
| 294 | + visits |
| 295 | + } |
| 296 | + avg { |
| 297 | + rating |
| 298 | + } |
| 299 | + min { |
| 300 | + createdAt |
| 301 | + title |
| 302 | + } |
| 303 | + max { |
| 304 | + rating |
| 305 | + updatedAt |
| 306 | + } |
| 307 | + } |
| 308 | + } |
| 309 | + } |
| 310 | + ``` |
| 311 | + |
| 312 | +=== "Response" |
| 313 | + |
| 314 | + ```json |
| 315 | + { |
| 316 | + "data": { |
| 317 | + "blogCollection": { |
| 318 | + "aggregate": { |
| 319 | + "count": 5, |
| 320 | + "sum": { |
| 321 | + "rating": 23, |
| 322 | + "visits": 1250 |
| 323 | + }, |
| 324 | + "avg": { |
| 325 | + "rating": 4.6 |
| 326 | + }, |
| 327 | + "min": { |
| 328 | + "createdAt": "2022-01-15T08:30:00Z", |
| 329 | + "title": "A Blog Post" |
| 330 | + }, |
| 331 | + "max": { |
| 332 | + "rating": 5, |
| 333 | + "updatedAt": "2023-04-22T14:15:30Z" |
| 334 | + } |
| 335 | + } |
| 336 | + } |
| 337 | + } |
| 338 | + } |
| 339 | + ``` |
| 340 | + |
| 341 | +**GraphQL Types** |
| 342 | +=== "BlogAggregate" |
| 343 | + |
| 344 | + ```graphql |
| 345 | + """Aggregate results for `Blog`""" |
| 346 | + type BlogAggregate { |
| 347 | + """The number of records matching the query""" |
| 348 | + count: Int! |
| 349 | + |
| 350 | + """Summation aggregates for `Blog`""" |
| 351 | + sum: BlogSumAggregateResult |
| 352 | + |
| 353 | + """Average aggregates for `Blog`""" |
| 354 | + avg: BlogAvgAggregateResult |
| 355 | + |
| 356 | + """Minimum aggregates for comparable fields""" |
| 357 | + min: BlogMinAggregateResult |
| 358 | + |
| 359 | + """Maximum aggregates for comparable fields""" |
| 360 | + max: BlogMaxAggregateResult |
| 361 | + } |
| 362 | + ``` |
| 363 | + |
| 364 | +=== "BlogSumAggregateResult" |
| 365 | + |
| 366 | + ```graphql |
| 367 | + """Result of summation aggregation for `Blog`""" |
| 368 | + type BlogSumAggregateResult { |
| 369 | + """Sum of rating values""" |
| 370 | + rating: BigFloat |
| 371 | + |
| 372 | + """Sum of visits values""" |
| 373 | + visits: BigInt |
| 374 | + |
| 375 | + # Other numeric fields... |
| 376 | + } |
| 377 | + ``` |
| 378 | + |
| 379 | +=== "BlogAvgAggregateResult" |
| 380 | + |
| 381 | + ```graphql |
| 382 | + """Result of average aggregation for `Blog`""" |
| 383 | + type BlogAvgAggregateResult { |
| 384 | + """Average of rating values""" |
| 385 | + rating: BigFloat |
| 386 | + |
| 387 | + """Average of visits values""" |
| 388 | + visits: BigFloat |
| 389 | + |
| 390 | + # Other numeric fields... |
| 391 | + } |
| 392 | + ``` |
| 393 | + |
| 394 | +=== "BlogMinAggregateResult" |
267 | 395 |
|
| 396 | + ```graphql |
| 397 | + """Result of minimum aggregation for `Blog`""" |
| 398 | + type BlogMinAggregateResult { |
| 399 | + """Minimum rating value""" |
| 400 | + rating: Float |
| 401 | + |
| 402 | + """Minimum title value""" |
| 403 | + title: String |
| 404 | + |
| 405 | + """Minimum createdAt value""" |
| 406 | + createdAt: Datetime |
| 407 | + |
| 408 | + # Other comparable fields... |
| 409 | + } |
| 410 | + ``` |
| 411 | + |
| 412 | +=== "BlogMaxAggregateResult" |
| 413 | + |
| 414 | + ```graphql |
| 415 | + """Result of maximum aggregation for `Blog`""" |
| 416 | + type BlogMaxAggregateResult { |
| 417 | + """Maximum rating value""" |
| 418 | + rating: Float |
| 419 | + |
| 420 | + """Maximum title value""" |
| 421 | + title: String |
| 422 | + |
| 423 | + """Maximum updatedAt value""" |
| 424 | + updatedAt: Datetime |
| 425 | + |
| 426 | + # Other comparable fields... |
| 427 | + } |
| 428 | + ``` |
| 429 | + |
| 430 | +!!! note |
| 431 | + |
| 432 | + - The return type for `sum` depends on the input type: integer fields return `BigInt`, while other numeric fields return `BigFloat`. |
| 433 | + - The return type for `avg` is always `BigFloat`. |
| 434 | + - The return types for `min` and `max` match the original field types. |
| 435 | + |
| 436 | +!!! note |
268 | 437 |
|
| 438 | + The `aggregate` field is disabled by default because it can be expensive on large tables. To enable it use a [comment directive](configuration.md#Aggregate) |
269 | 439 |
|
270 | 440 | #### Pagination |
271 | 441 |
|
|
0 commit comments