$ show strawberry-graphql/strawberry-django
strawberry-django is a mature, production-grade library that integrates the Strawberry GraphQL framework with Django. It automatically generates GraphQL types, fields, queries, and mutations from Django models, reducing boilerplate significantly. The library provides a powerful query optimizer (DjangoOptimizerExtension) that prevents N+1 queries by introspecting GraphQL selections and applying `select_related`, `prefetch_related`, `only()`, and `annotate()` automatically. It also includes declarative filtering, ordering, pagination (offset and cursor-based Relay connections), a full mutation system with nested input handling and error unions, permission extensions (IsAuthenticated, HasPerm, etc.), authentication helpers, Apollo Federation support, and Django Channels integration for subscriptions. The architecture is modular: core decorators (`@type`, `@input`, `@field`, `@connection`, `@mutation`) build on a flexible field class hierarchy (`StrawberryDjangoField` with mixins for pagination, ordering, filters). The optimizer is the standout feature, using an `OptimizerStore` to collect hints per field and apply them to querysets, with sophisticated prefetch merging and window-function pagination for nested relations. The library is well-tested with over 100 test files covering sync/async, polymorphism, relay, mutations, and edge cases, and includes a comprehensive documentation site. However, it has some technical debt: monkey-patching of Django's `QuerySet._clone`, deprecated legacy ordering/filtering APIs, and tight coupling to Strawberry GraphQL internals (requiring frequent compatibility shims). The codebase is actively maintained with frequent releases and a clear changelog, but the upstream dependency churn introduces medium drift risk.
The LLM identified 4 files worth highlighting for reuse:
Implements DjangoOptimizerExtension, a Strawberry schema extension that automatically applies `select_related`, `prefetch_related`, `only()`, and `annotate()` to Django querysets based on the GraphQL selection set. Uses an OptimizerStore to collect hints per field/type, then applies them via `apply()`. Includes sophisticated prefetch merging logic that distinguishes user-defined prefetches from optimizer-generated ones, and supports window-function pagination for nested connections. This is a highly reusable pattern for any GraphQL-Django integration that wants to eliminate N+1 queries without manual effort.
Provides DjangoCursorConnection and DjangoCursorEdge for Relay-compatible cursor-based pagination over Django QuerySets. Uses base64-encoded cursors containing ordering values, and implements window-function-based pagination with `RowNumber()` to compute total counts and cursor positions efficiently. Supports forward/backward pagination, custom ordering, nulls handling, and integration with the optimizer for nested prefetches. This is a self-contained, production-ready implementation of Relay connection pagination that can be extracted for any Django project needing cursor pagination.
Defines a permission system for GraphQL fields using Strawberry's field extension and schema directive mechanisms. Includes `IsAuthenticated`, `IsStaff`, `IsSuperuser`, `HasPerm`, `HasSourcePerm`, and `HasRetvalPerm` classes. Permissions can be applied as field extensions (resolving before/after the field resolver) or as schema directives. Supports async/sync resolvers, queryset filtering via `filter_with_perms`, and configurable error messages. The pattern of combining field extensions with directives is reusable for any GraphQL framework needing declarative authorization.
Implements the core mutation resolvers for create, update, and delete operations on Django models. Handles nested input parsing (including M2M relations, through models, and key attributes), PK resolution, partial updates, validation via `full_clean`, and error handling that returns `OperationInfo` unions. Supports both sync and async execution, batch operations, and transaction safety. The pattern of auto-generating CRUD mutations from model definitions with proper error handling is a reusable template for any Django GraphQL API.