Skip to main content

strawberry-django-nestjs

Expose Django models over GraphQL in the nestjs-query convention, so the stock @refinedev/nestjs-query refine data provider drives a Strawberry/Django backend with no patching.

It is a thin adapter: it composes strawberry-django (filters, ordering, pagination) and strawberry-django-aggregates (the compute_aggregation primitive) and emits the exact GraphQL shape the refine provider speaks. One unmodified frontend data provider, any Django model. The precise target SDL is in CONTRACT.md.

Why

refine's nestjs-query provider expects a specific GraphQL shape per resource: a notes(filter, sorting, paging): NoteConnection list, a note(id): Note detail, createOneNote / updateOneNote / deleteOneNote mutations, NoteFilter operator objects, NoteSort + SortDirection, OffsetPaging, and a noteAggregate group-by surface. This library emits all of it from your Strawberry types — you keep the stock provider, no custom mapping layer.

Install

pip install strawberry-django-nestjs
# or
uv add strawberry-django-nestjs

Requires Python 3.14+, Django 6.0+, and a Strawberry/strawberry-django stack (installed transitively).

Quickstart

Declare the per-model nestjs types from your Strawberry types, then compose the adapter's helpers in plain resolvers. (Condensed from examples/demo_schema.py, which exercises every surface.)

import enum
import strawberry
import strawberry_django
from strawberry import UNSET, auto

from strawberry_django_nestjs import (
    AggregateField, Connection, OffsetPaging, SortDirection, SortNulls,
    apply_sorting, build_aggregate_types, filter_to_q, input_to_dict,
    make_aggregate_resolver, paginate,
)
from strawberry_django_nestjs.comparisons import (
    NumberFieldComparison, StringFieldComparison,
)

from .models import Note  # your Django model


@strawberry_django.type(Note)
class NoteType:           # GraphQL type name `Note`
    id: auto
    title: auto
    word_count: auto
    status: auto


@strawberry.input
class NoteFilter:
    title: StringFieldComparison | None = UNSET
    status: StringFieldComparison | None = UNSET
    word_count: NumberFieldComparison | None = UNSET
    and_: list["NoteFilter"] | None = strawberry.field(name="and", default=UNSET)
    or_: list["NoteFilter"] | None = strawberry.field(name="or", default=UNSET)


@strawberry.enum
class NoteSortFields(enum.Enum):   # field value = the Django column name
    title = "title"
    word_count = "word_count"


@strawberry.input
class NoteSort:
    field: NoteSortFields
    direction: SortDirection
    nulls: SortNulls | None = UNSET


def base_qs():
    # Apply your row-level (e.g. REBAC) scoping here — reads run on this.
    return Note.objects.all()


# Declare the aggregate surface once; the same list builds the
# `<Model>Aggregate` types and maps GraphQL (camelCase) names to columns.
AGG_FIELDS = [
    AggregateField("title", str),
    AggregateField("word_count", int, numeric=True),
    AggregateField("status", str),
]
NoteAggregateResponse, _blocks = build_aggregate_types("Note", AGG_FIELDS)
note_aggregate = make_aggregate_resolver(
    NoteAggregateResponse, _blocks, AGG_FIELDS,
    base_queryset=base_qs, filter_to_q=filter_to_q,
)


@strawberry.type
class Query:
    @strawberry.field
    def notes(
        self,
        filter: NoteFilter | None = None,
        sorting: list[NoteSort] | None = None,
        paging: OffsetPaging | None = None,
    ) -> Connection[NoteType]:
        qs = apply_sorting(base_qs().filter(filter_to_q(filter)), sorting)
        return Connection(nodes=list(paginate(qs, paging)), total_count=qs.count())

    @strawberry.field
    def note(self, id: strawberry.ID) -> NoteType | None:
        return base_qs().filter(pk=id).first()

    @strawberry.field
    def note_aggregate(
        self, info: strawberry.Info, filter: NoteFilter | None = None
    ) -> list[NoteAggregateResponse]:
        return note_aggregate(info, filter=filter)

createOne / updateOne / deleteOne mutations follow the same pattern, using input_to_dict to translate the nestjs input envelope into model kwargs. Your <Model>Update input type is the authoritative writable-field allowlist (keep server-owned columns out of it); run the updateOne read-modify-write inside transaction.atomic() and save(update_fields=…) so a patch touches only the columns it set (plus any auto_now columns you want refreshed, which update_fields otherwise skips) — see examples/demo_schema.py.

The five surfaces

Surface Module What it emits / does
Filtering comparisons, filtering <Model>Filter operator objects → a Django Q
Sorting sorting, types [<Model>Sort!] + SortDirection/SortNulls → .order_by()
Pagination connection OffsetPaging + <Model>Connection { nodes, totalCount }
Mutations mutations createOne/updateOne/deleteOne input envelope → model kwargs
Aggregation aggregation selection-driven <Model>Aggregate group-by, composing compute_aggregation

Proof: the stock provider drives it

examples/ is a runnable proof that the unmodified @refinedev/nestjs-query provider drives a schema built with this library (getList / getOne / create / update / deleteOne), no patching.

Status

Beta (v0.1.0). The public API (__init__ exports) and the emitted SDL shape follow CONTRACT.md and are stable for early adopters; minor iteration is expected before a 1.0 stability commitment. Runtime: Python 3.14, Django 6.0.

Documentation

License

AGPL-3.0-or-later. See LICENSE.

Metadata

Release files for strawberry-django-nestjs 0.1.0

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-django-nestjs 0.1.0
File Size Uploaded Provenance
strawberry_django_nestjs-0.1.0.tar.gz 29.0 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for strawberry-django-nestjs 0.1.0
File Interpreter ABI Platform
strawberry_django_nestjs-0.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 55.0 kB

Release files / strawberry_django_nestjs-0.1.0.tar.gz

Download URL strawberry_django_nestjs-0.1.0.tar.gz
Size 29.0 kB
Tags Source
SHA-256 checksum
How to use checksums
1de2abb779f422d97d918a32f41253624c57da781ed5a3e3a29000f140a7bd30
BLAKE2b-256 checksum
How to use checksums
2068c5a070af2af9b02901c9a95afa8a00ce0c443722b7a69107637051b34a86
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.12

Release files / strawberry_django_nestjs-0.1.0-py3-none-any.whl

Download URL strawberry_django_nestjs-0.1.0-py3-none-any.whl
Size 26.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
beb1e11247e68704c91b5ff3047da96545a4c6e1a6e2c79b13111b80a8b217a9
BLAKE2b-256 checksum
How to use checksums
2894b1364801f67a54fa677ff7f0ee56e973231264fe39699ecbfbe14962933a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.12

Security for strawberry-django-nestjs 0.1.0

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.

No provenance found

None of the files in this release were uploaded with provenance metadata.

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.

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files No provenance

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