Skip to main content

strawberry-alchemy

Batteries-included toolkit for building Strawberry GraphQL APIs backed by SQLAlchemy

CI PyPI version Python versions License: MIT

Ask DeepWiki


Source Code: https://github.com/Alteian/strawberry-alchemy

Documentation: docs/index.md · API reference


What is it?

strawberry-alchemy turns Strawberry GraphQL selection sets into a single optimized SQLAlchemy query — automatic selectinload for relationships, column deferral, and SQL EXISTS/COUNT annotations — and ships the supporting cast: filter inputs, Relay pagination, CRUD repositories, row-level security, permissions, and mapping helpers.

  • No N+1 queries: nested selections become eager loads automatically
  • Unrequested columns are deferred — you only fetch what the client asked for
  • Declarative computed fields (hasComments, counts, ancestry CTEs) via @optimize_field
  • AND/OR filters, relationship joins, custom filters, and Postgres JSONB lookups
  • Relay Connection pagination with totalCount
  • Async repositories with deletion reports, cascade hooks, and optional soft-delete
  • Row-level AccessControlFilter on root queries / totalCount, plus Postgres RLS session GUCs

Installation

pip install strawberry-alchemy
# or
uv add strawberry-alchemy

Requires Python >= 3.13, Strawberry GraphQL >= 0.339.1, sqlalchemy[asyncio] >= 2.1.6, Pydantic >= 2.14.

Quick example

# models.py
import uuid

from sqlalchemy import ForeignKey
from sqlalchemy.dialects.postgresql import UUID
from sqlalchemy.orm import Mapped, mapped_column

from strawberry_alchemy.models import Base


class Post(Base):
    user_id: Mapped[uuid.UUID] = mapped_column(UUID(as_uuid=True), ForeignKey("user.id"))
    title: Mapped[str]
    body: Mapped[str]
# access_filters.py
from typing import Any
from models import Post
from strawberry_alchemy.filtering import AccessControlFilter


class PostAccessFilter(AccessControlFilter):
    model_class = Post

    @staticmethod
    async def apply_filter(query: Any, model: type[Any], context_user: Any) -> Any:
        return query.where(model.user_id == context_user.id)
# types.py
import strawberry
from typing import ClassVar
from strawberry_alchemy import BaseNodeType


@strawberry.type
class PostType(BaseNodeType):
    access_filter: ClassVar[type[PostAccessFilter]] = PostAccessFilter

    title: str | None = strawberry.UNSET
    body: str | None = strawberry.UNSET
# queries.py
import strawberry
from strawberry.types import Info
from strawberry_alchemy import ListResult


@strawberry.type
class Query:
    @strawberry.field
    async def posts(self, info: Info, limit: int | None = None) -> ListResult[PostType]:
        return await PostType.resolve_list(info=info, limit=limit)
# schema.py
import strawberry
from strawberry.schema.config import StrawberryConfig

schema = strawberry.Schema(
    query=Query,
    config=StrawberryConfig(auto_camel_case=True, relay_max_results=100),
)
query {
  posts(limit: 10) {
    items { id title body }
    totalCount
  }
}

Your request context needs three things: get_session(), an identity/user attribute, and a db_execution_lock (see docs/getting-started.md#context-contract).

Documentation

Page Topic
docs/index.md Overview, features, architecture
docs/getting-started.md Full walkthrough: models, types, filters, queries, mutations
docs/types-and-models.md Base, BaseNodeType, ListResult, connections, ordering
docs/queries.md resolve_node / resolve_list / resolve_connection, pagination
docs/query-optimizer.md Load strategies, @optimize_field hints, QueryAnalyzer
docs/filtering.md Filter inputs, operators, AND/OR, custom & access-control filters
docs/repository.md CRUD, soft-delete, deletion reports / handlers
docs/mapping-and-schema.md BaseSchema, SQLAlchemy → Strawberry mapping
docs/permissions.md Permission classes, resource checks
docs/postgres-rls.md Session GUC helpers for app-owned RLS policies
docs/api-reference.md Every public export with signatures

Example

A runnable blog sketch covering core patterns (models, ACL, OR filters, @optimize_field, ClassVar repositories, deletion handler) lives in examples/blog. It is a focused walkthrough — not every 0.3/0.4 API (ancestry, soft-delete, JSONB, RLS) is demonstrated there yet.

make test ARGS="tests/examples"

Development

Tool versions are pinned in mise.toml (same pins CI uses via jdx/mise-action). Tasks stay in Make.

git clone https://github.com/Alteian/strawberry-alchemy.git
cd strawberry-alchemy
# https://mise.jdx.dev — then:
mise install
make sync
make doctor

# Lint, typecheck, test, build (same Make targets CI runs)
make ci

See docs/roadmap.md for planned work. See make help for targets.

Contributing

See CONTRIBUTING.md for guidelines.

License

MIT

Metadata

Release files for strawberry-alchemy 0.5.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for strawberry-alchemy 0.5.2
File Size Uploaded Provenance Actions
strawberry_alchemy-0.5.2.tar.gz 41.4 kB With provenance Details for strawberry_alchemy-0.5.2.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for strawberry-alchemy 0.5.2
File Interpreter ABI Platform Actions
strawberry_alchemy-0.5.2-py3-none-any.whl Python 3 none any Details for strawberry_alchemy-0.5.2-py3-none-any.whl

Total release size: 94.8 kB

Release files / strawberry_alchemy-0.5.2.tar.gz

Download URL strawberry_alchemy-0.5.2.tar.gz
Size 41.4 kB
Tags Source
SHA-256 checksum
How to use checksums
a3382339fddd5bb1ed983d7d935056bd04ba57de6791a9368e46af9767ebcc64
BLAKE2b-256 checksum
How to use checksums
07a0b5512b99b78b43689005e2ba1d627c321c7f9cd98d97df34a3850ba2d37f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 12, 2026.

Transparency log

Release files / strawberry_alchemy-0.5.2-py3-none-any.whl

Download URL strawberry_alchemy-0.5.2-py3-none-any.whl
Size 53.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
dd29d5d9b2823043f2b76db76a3b542c5db1716faa3b989d18740fa974099c7c
BLAKE2b-256 checksum
How to use checksums
f9aec3d60f28dab28a7ffbbb792bf51112f8aca70314886d5519d32fe15a1dea
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 12, 2026.

Transparency log

Security for strawberry-alchemy 0.5.2

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for strawberry_alchemy-0.5.2.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 12, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page