Skip to main content

Strawberry GraphQL helpers for SQLAlchemy models.

Project description

strawberry-chemist

strawberry-chemist helps expose SQLAlchemy models through Strawberry without turning your GraphQL schema into generated magic.

The package is intentionally explicit. You still write the Strawberry types you want clients to see. That makes the DTO layer visible, keeps the public contract adaptable, and fits production codebases that care about query shape, permissions, loading behavior, and long-term schema maintenance.

That explicitness does not mean giving up performance. Chemist-managed relationship and connection fields are selection-aware and dataloader-backed, so you can keep explicit DTOs without falling into per-parent N+1 loading.

Chemist focuses on the parts that are repetitive and SQLAlchemy-aware:

  • field mapping and renaming
  • relationship loading
  • root and nested connections
  • filtering, ordering, and pagination
  • relay IDs and node lookup
  • dataloaders and selection-aware loading

Relationships and connections are also deliberately flexible. They can be:

  • plain mapped fields
  • renamed fields
  • server-scoped fields
  • relationship-backed computed fields
  • root collections
  • relationship-backed collections with filtering, ordering, and pagination

Installation

pip install strawberry-chemist

Supported Python versions:

  • 3.11
  • 3.12

Quick example

import strawberry
import strawberry_chemist as sc


@sc.node(model=BookModel)
class Book:
    title: str
    published_year: int = sc.attr("year")


@sc.filter(model=BookModel)
class BookFilter(sc.FilterSet):
    title: sc.StringFilter = sc.filter_field()


@sc.order(model=BookModel)
class BookOrder:
    published_year = sc.order_field(path="year")


@strawberry.type
class Query:
    node = sc.node_field()
    books: sc.Connection[Book] = sc.connection(
        filter=BookFilter,
        order=BookOrder,
        pagination=sc.CursorPagination(max_limit=20),
    )


schema = strawberry.Schema(query=Query, extensions=sc.extensions())

Your GraphQL context must provide a get_session() async context manager that returns a SQLAlchemy AsyncSession.

Public docs

The public docs live in docs/. The published site is intended for GitHub Pages and is built with MkDocs.

Useful entrypoints:

Serve the docs locally:

uv sync --group dev
uv run mkdocs serve

Build the docs locally:

uv run mkdocs build --strict

Runnable examples

The contract examples under examples/v0_2_api/ can run in two modes.

Against the current checkout:

scripts/run-example-local 03_connections_filters_and_ordering

Against the pinned published package:

scripts/run-example-published 03_connections_filters_and_ordering

If you want to force published-mode testing against a locally built distribution, point the script at a build output directory:

uv run python -m build --outdir /tmp/strawberry-chemist-dist
STRAWBERRY_CHEMIST_FIND_LINKS=/tmp/strawberry-chemist-dist \
  scripts/run-example-published 03_connections_filters_and_ordering

API overview

  • Types and fields: @sc.type, @sc.node, sc.attr, @sc.field
  • Relationships: sc.relationship(...)
  • Collections: sc.connection(...), sc.Connection, sc.OffsetConnection
  • Pagination: sc.CursorPagination, sc.OffsetPagination, sc.PaginationPolicy
  • Filters: @sc.filter, sc.FilterSet, sc.filter_field, sc.manual_filter
  • Ordering: @sc.order, sc.order_field, sc.manual_order
  • Relay: sc.node_field(), sc.node_lookup(...)
  • Schema integration: sc.extensions()

Each part of the surface has a dedicated public docs page and at least one example reference in docs/examples.md.

Development

Run the default non-Postgres test suite:

uv run pytest

Run formatting and type checks:

uv run pre-commit run --all-files
uv run mypy

Release notes live in CHANGELOG.md. Current limitations are documented in LIMITATIONS.md.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

strawberry_chemist-0.2.0.tar.gz (142.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

strawberry_chemist-0.2.0-py3-none-any.whl (41.8 kB view details)

Uploaded Python 3

File details

Details for the file strawberry_chemist-0.2.0.tar.gz.

File metadata

  • Download URL: strawberry_chemist-0.2.0.tar.gz
  • Upload date:
  • Size: 142.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for strawberry_chemist-0.2.0.tar.gz
Algorithm Hash digest
SHA256 3c7b88f30ff40ee8c304ceedbc2a7badf4adcabaa131be3f8ffa511bdb52c72d
MD5 c92017a94f633b8e3a463df93a99cc4f
BLAKE2b-256 fcd37c6fc605b9333d3bd2c8f3fcae05af3d064e0b5e65d8d2a68a48e18c6b0d

See more details on using hashes here.

Provenance

The following attestation bundles were made for strawberry_chemist-0.2.0.tar.gz:

Publisher: publish.yml on MeRuslan/strawberry-chemist

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file strawberry_chemist-0.2.0-py3-none-any.whl.

File metadata

File hashes

Hashes for strawberry_chemist-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 6f7ac241fd915df8e76a20a7e86105ca7c958a503601c61c6c72987b10b76f48
MD5 e1870df236a60c613dc2123410af2bcd
BLAKE2b-256 40670344930946a4b624af0774107832e3723d27bdb114928cddb8fecd1326cb

See more details on using hashes here.

Provenance

The following attestation bundles were made for strawberry_chemist-0.2.0-py3-none-any.whl:

Publisher: publish.yml on MeRuslan/strawberry-chemist

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page