Hasura-shape aggregations over Django querysets in Strawberry GraphQL — count, sum, avg, min, max, stddev, variance, array_agg + Odoo-grade group-by with timezone-correct date bucketing.
Project description
strawberry-django-aggregates
Hasura-shape aggregations over Django querysets in Strawberry GraphQL.
count · count_distinct · sum · avg · min · max · stddev · variance · bool_and · bool_or · array_agg · string_agg — composed with multi-level group_by, having filters, and ordering on aggregate aliases. Inspired by Hasura's <table>_aggregate, PostGraphile's pg-aggregates, and Odoo 18's _read_group. Built for strawberry-django over PostgreSQL and SQLite.
from decimal import Decimal
from datetime import datetime
from strawberry import auto
import strawberry, strawberry_django
from strawberry_django_aggregates import AggregateBuilder
from .models import Order
@strawberry_django.type(Order)
class OrderType:
id: auto
customer: "CustomerType"
total: Decimal
status: str
created_at: datetime
# One call wires count/sum/avg/min/max + group_by + having into the schema:
order_aggs = AggregateBuilder(
model=Order,
aggregate_fields=["total"],
group_by_fields=["customer", "status", "created_at"],
).build()
@strawberry.type
class Query:
orders_aggregate = order_aggs.aggregate_field
orders_group_by = order_aggs.group_by_field
Generates a fully-typed GraphQL surface:
type Query {
ordersAggregate(filter: OrderFilter): OrderAggregate!
ordersGroupBy(
filter: OrderFilter
groupBy: [OrderGroupBySpec!]!
having: OrderHaving
orderBy: [OrderGroupOrder!]
pagination: OffsetPagination
): OrderGroupedResult!
}
type OrderAggregate {
count: Int!
countDistinct(field: OrderCountableField!): Int!
sum: OrderSumFields
avg: OrderAvgFields
min: OrderMinFields
max: OrderMaxFields
stddev: OrderStddevFields # Postgres only
variance: OrderVarianceFields # Postgres only
}
type OrderGrouped {
key: OrderGroupKey! # composite — every requested groupBy field present
count: Int!
sum: OrderSumFields
# ... no recursive subgroups field — flat results
}
Features
- Hasura-canonical schema shape.
<Model>Aggregate { count, countDistinct, sum, avg, min, max, stddev, variance, boolAnd, boolOr, arrayAgg, stringAgg }. - Odoo-grade group-by. Multi-level via composite keys (flat result rows), dual date-granularity tracks (
date_truncreturningDateTimeANDdate_part::intreturningInt), timezone-correct bucketing. - HAVING with aggregate aliases.
{ countGt: 5, sumTotalGt: 1000 }— typed inputs generated per measure. - Ordering on aggregates.
[{ field: "total:sum", direction: DESC }]— fail-loud on unknown terms (Odoo's pre-17 silent-drop bug avoided). - Standalone backend primitive.
compute_aggregation(qs, group_by, aggregates, having, order_by, ...)is callable from any Python context — DRF view, Celery task, admin script, MCP tool — not just GraphQL resolvers. - Determinism. Type generation produces byte-identical SDL for the same inputs.
- No magic. Every operator, every granularity, every type is whitelisted.
Non-goals
- Cross-database aggregation. PostgreSQL + SQLite only. SQLite degrades gracefully on
array_agg/string_agg/stddev/variance— those operators raiseOperatorNotSupportedErrorat resolver entry. - Auto-traversal of one-to-many / many-to-many for measures. This is the silent row-multiplication footgun Odoo refuses to ship; we follow.
array_aggis the explicit escape hatch. - Permission integration. The library expects a pre-scoped queryset — the caller has already applied
accessible_by(user)or equivalent. This keeps the library compatible with django-guardian, django-rules, django-rebac (when shipped), or hand-rolled permission systems.
Status
Beta (v0.2.1). The schema shape, operator vocabulary, and compute_aggregation signature are stable for early adopters, but minor-level iteration is still expected before a 1.0 stability commitment — see docs/SPEC.md § 16. Runtime: Python 3.13, Django 6.0.
Documentation
- Full contract:
docs/SPEC.md— operator catalog, granularity tracks, HAVING semantics, ordering rules, timezone handling, and the Odoo-derived footgun audit. - Naming and wire vocabulary:
docs/TERMINOLOGY.md - Contributor quality gate:
CONTRIBUTING.md
License
BSD-3-Clause.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file strawberry_django_aggregates-0.2.2.tar.gz.
File metadata
- Download URL: strawberry_django_aggregates-0.2.2.tar.gz
- Upload date:
- Size: 124.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0050b5eabc759885035443693015e4aab7cd981ec8a1fbb49478b26d6e68125e
|
|
| MD5 |
d9455d4472e88161e108370e5bd75e82
|
|
| BLAKE2b-256 |
12cf1bcc5aac33d9faf44295780a4680e49a7bb1aa52bc651b0d3c028fa27ba0
|
File details
Details for the file strawberry_django_aggregates-0.2.2-py3-none-any.whl.
File metadata
- Download URL: strawberry_django_aggregates-0.2.2-py3-none-any.whl
- Upload date:
- Size: 88.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6acb7aef9f800c2b1bbd7d94d36982336e36251acad7eb4fdcb3e63df991850f
|
|
| MD5 |
901ccbb68f64689145226528318cd049
|
|
| BLAKE2b-256 |
91adbcbaa91fb373360a8c2459ea74cb53be6ff5d835521ea7aac79b332acef9
|