{"id":3820,"library":"strawberry-graphql-django","title":"Strawberry GraphQL Django Integration","description":"Strawberry GraphQL Django integration provides powerful tools to build GraphQL APIs with Django. It automatically generates GraphQL types, queries, mutations, and resolvers from your Django models with full type safety. The library is actively maintained, currently at version 0.82.1, and supports Django 4.2-6.0 and Python 3.10-3.14, with regular updates.","status":"active","version":"0.82.1","language":"en","source_language":"en","source_url":"https://github.com/strawberry-graphql/strawberry-django","tags":["GraphQL","Django","API","Type Safety","ORM"],"install":[{"cmd":"pip install strawberry-graphql-django","lang":"bash","label":"Install core library"},{"cmd":"pip install strawberry-graphql-django[polymorphic]","lang":"bash","label":"Install with polymorphic support (optional)"}],"dependencies":[{"reason":"Core GraphQL library that strawberry-graphql-django builds upon.","package":"strawberry-graphql","optional":false},{"reason":"The web framework it integrates with.","package":"Django","optional":false},{"reason":"Recommended for automatically converting Django's `choices` fields into GraphQL enums.","package":"django-choices-field","optional":true},{"reason":"Required for supporting polymorphic queries with Django models.","package":"django-polymorphic","optional":true}],"imports":[{"note":"Use strawberry_django.type for Django model integration, not base strawberry.type.","wrong":"from strawberry import type","symbol":"@strawberry_django.type","correct":"import strawberry_django\n@strawberry_django.type(...)"},{"note":"Use strawberry_django.field for Django model fields to leverage its features like N+1 query optimization.","wrong":"from strawberry import field","symbol":"strawberry_django.field","correct":"from strawberry_django import field"},{"note":"Use AsyncGraphQLView for async Django views; GraphQLView is also available for sync.","symbol":"AsyncGraphQLView","correct":"from strawberry.django.views import AsyncGraphQLView"},{"note":"This extension is crucial for N+1 query problem prevention and query optimization.","symbol":"DjangoOptimizerExtension","correct":"from strawberry_django.optimizer import DjangoOptimizerExtension"}],"quickstart":{"code":"import strawberry\nimport strawberry_django\nfrom django.db import models\nfrom strawberry.django.views import AsyncGraphQLView\nfrom strawberry_django.optimizer import DjangoOptimizerExtension\n\n# models.py (excerpt)\nclass Color(models.Model):\n    name = models.CharField(max_length=20)\n\n    def __str__(self):\n        return self.name\n\nclass Fruit(models.Model):\n    name = models.CharField(max_length=20)\n    color = models.ForeignKey(Color, on_delete=models.CASCADE, related_name=\"fruits\")\n\n    def __str__(self):\n        return self.name\n\n# types.py (excerpt, often in app.types.py)\n@strawberry_django.type(Color)\nclass ColorType:\n    id: strawberry.ID\n    name: str\n    fruits: list['FruitType'] # Forward reference\n\n@strawberry_django.type(Fruit)\nclass FruitType:\n    id: strawberry.ID\n    name: str\n    color: ColorType\n\n# schema.py (excerpt, often in project.schema.py)\n@strawberry.type\nclass Query:\n    @strawberry_django.field\n    def fruits(self) -> list[FruitType]:\n        return Fruit.objects.all()\n\n    @strawberry_django.field\n    def colors(self) -> list[ColorType]:\n        return Color.objects.all()\n\nschema = strawberry.Schema(\n    query=Query,\n    extensions=[\n        DjangoOptimizerExtension, # Recommended for N+1 query optimization\n    ]\n)\n\n# urls.py (excerpt)\n# from django.urls import path\n# from myproject.schema import schema # Assuming schema defined in myproject/schema.py\n# from strawberry.django.views import AsyncGraphQLView\n#\n# urlpatterns = [\n#     path(\"graphql/\", AsyncGraphQLView.as_view(schema=schema)),\n# ]\n\n# To run this example, you'd typically set up a Django project:\n# 1. Create a Django project and app.\n# 2. Add 'strawberry.django' and your app to INSTALLED_APPS in settings.py.\n# 3. Define models (Color, Fruit) in your app's models.py.\n# 4. Define types (ColorType, FruitType) in your app's types.py.\n# 5. Define Query and Schema in your project's schema.py.\n# 6. Add the GraphQL endpoint to your project's urls.py.","lang":"python","description":"This quickstart demonstrates how to define Django models, create corresponding Strawberry GraphQL types using `strawberry_django.type` and `auto`, and then expose them via a GraphQL `Query` field. It also shows the integration of `DjangoOptimizerExtension` for performance and how to set up the Django URLconf to serve the GraphQL API. The example defines simple Fruit and Color models with a foreign key relationship."},"warnings":[{"fix":"Clients now need to send CSRF tokens. To restore previous behavior, explicitly wrap your `GraphQLView.as_view()` with Django's `@csrf_exempt` decorator: `path(\"graphql/\", csrf_exempt(GraphQLView.as_view(schema=schema)))`.","message":"Django's CSRF protection is now enabled by default for Strawberry Django views (since v0.243.0). Previously, views were implicitly exempted.","severity":"breaking","affected_versions":">=0.243.0"},{"fix":"To enable, set `multipart_uploads_enabled=True` when configuring your `GraphQLView.as_view()` and implement appropriate security measures.","message":"Multipart file uploads are disabled by default (since v0.243.0) due to security implications.","severity":"breaking","affected_versions":">=0.243.0"},{"fix":"Migrate your `strawberry-django-plus` implementation to `strawberry-graphql-django` to ensure continued support and development. Consult the `strawberry-django-plus` migration guide.","message":"The `strawberry-django-plus` library is deprecated. All its additional features have been merged into the official `strawberry-graphql-django` library.","severity":"deprecated","affected_versions":"All versions of `strawberry-django-plus`"},{"fix":"Wrap synchronous Django ORM calls within `sync_to_async` (from `asgiref.sync`) when inside an async resolver.","message":"Directly accessing Django ORM from asynchronous resolvers within `AsyncGraphQLView` will raise `django.core.exceptions.SynchronousOnlyOperation`.","severity":"gotcha","affected_versions":"All"},{"fix":"Update frontend clients expecting `GlobalID`. If you need to revert to the `GlobalID` schema type, pass `config=StrawberryConfig(relay_use_legacy_global_id=True)` to `strawberry.Schema`.","message":"For Relay integration, the generated GraphQL type for the `id` field changed from `GlobalID` to `ID` (since v0.268.0). The Python runtime behavior for `relay.GlobalID` remains unchanged.","severity":"breaking","affected_versions":">=0.268.0"},{"fix":"Use `typing.cast` to explicitly tell the type checker that the Django model instance is compatible with the expected GraphQL type, e.g., `return cast(GraphQLType, django_model_instance)`.","message":"Type checkers (e.g., MyPy, PyLance) might show errors when using `strawberry.auto` and returning a Django model instance where a GraphQL type is expected.","severity":"gotcha","affected_versions":"All"},{"fix":"Define GraphQL types for all relevant polymorphic model subclasses or implement a `get_queryset` method on your GraphQL interface type to filter results appropriately.","message":"When using `django-polymorphic`, ensure every model subclass has a corresponding GraphQL type, or filter unwanted subtypes in `get_queryset` to avoid 'Abstract type ... must resolve to an Object type at runtime' errors.","severity":"gotcha","affected_versions":"All"}],"env_vars":null,"last_verified":"2026-04-11T00:00:00.000Z","next_check":"2026-07-10T00:00:00.000Z"}