strawberry-alchemy¶
Batteries-included toolkit for building Strawberry GraphQL APIs backed by
SQLAlchemy. It turns the incoming selection set into one optimized query
(defer unrequested columns, selectinload nested relations, SQL annotations)
and ships the surrounding cast: filters, Relay pagination, repositories,
row-level ACL, permissions helpers, and input/output mapping.
What it gives you¶
| Area | What it does |
|---|---|
| QueryOptimizer | Selection set → one SQLAlchemy query: selectinload, defer(), EXISTS / COUNT / ancestry CTE annotations |
@optimize_field hints |
PrefetchRelated, SelectColumns, AnnotateExists, AnnotateAnyExists, AnnotateCount, AnnotateCustom, AnnotateAncestry |
| FilterBuilder | Strawberry filter inputs → WHERE, with AND / OR, relation joins, CUSTOM_FILTERS, JSONB key filters |
| Types | BaseNodeType (Relay Node), ListResult, OptimizedListConnection / Edge / PageInfo |
| Repository | Async CRUD, soft-delete, relation add/remove, DeletionReport / cascade handlers |
| Mapping & schema | BaseSchema, schema_from_input / from_input, SQLAlchemy → Strawberry mapping |
| Access control | AccessControlFilter (row ACL), RelationAccessFilter (per-edge ACL override) |
| Permissions | Auth helpers + prepare_mutation_resources for mutation loaders |
| Postgres RLS | Session GUC helpers for app-owned CREATE POLICY DDL |
| Models | Base (+ optional SoftDeleteMixin) |
Requirements¶
- Python
>= 3.13 strawberry-graphql >= 0.339.1sqlalchemy[asyncio] >= 2.1.6pydantic >= 2.14
Installation¶
How the pieces fit together¶
flowchart LR
Client[GraphQL client] --> Resolver[resolve_list / resolve_connection]
Resolver --> Opt[QueryOptimizer]
Opt --> ACL[AccessControlFilter]
Opt --> Filters[FilterBuilder]
Opt --> Hints["@optimize_field hints"]
Opt --> SQL[SQLAlchemy query]
SQL --> Map[map_sqlalchemy_to_type]
Map --> Type[BaseNodeType]
- Subclass
BaseNodeType, declare anaccess_filter, and field the columns / relationships the client may select. - Query resolvers call
resolve_node/resolve_list/resolve_connection. Those methods readinfo, build one optimized query, apply ACL + filters, and map rows back to Strawberry types. - Computed fields use
@optimize_field(...)so the optimizer knows what SQL to emit (EXISTS, counts, prefetches, …). - Mutations use
BaseRepository+BaseSchema/schema_from_input, optionally withprepare_mutation_resourcesand aBaseDeletionHandler.
Walk through a full blog API in Getting started. Production wrapping patterns (project repositories, tenancy, custom order) live in Recipes.
Documentation¶
- Getting started — models, ACL, types, filters, queries, mutations
- Blog example — runnable core-pattern sketch
- Types and models
- Queries
- Filtering
- Query optimizer
- Mapping and schema
- Repository
- Permissions
- Postgres RLS
- Recipes — production patterns
- API reference
- Roadmap
Development¶
git clone https://github.com/Alteian/strawberry-alchemy.git
cd strawberry-alchemy
mise install
make sync && make doctor
make ci
Docs: make docs / make docs-serve. See Contributing.