# Strawberry GraphQL Django integration
[![CI](https://github.com/strawberry-graphql/strawberry-django/actions/workflows/tests.yml/badge.svg)](https://github.com/strawberry-graphql/strawberry-django/actions/workflows/tests.yml)
[![Coverage](https://codecov.io/gh/strawberry-graphql/strawberry-django/branch/main/graph/badge.svg?token=JNH6PUYh3e)](https://codecov.io/gh/strawberry-graphql/strawberry-django)
[![PyPI](https://img.shields.io/pypi/v/strawberry-graphql-django)](https://pypi.org/project/strawberry-graphql-django/)
[![Downloads](https://pepy.tech/badge/strawberry-graphql-django)](https://pepy.tech/project/strawberry-graphql-django)
![PyPI - Python Version](https://img.shields.io/pypi/pyversions/strawberry-graphql-django)
[**Docs**](https://strawberry.rocks/docs/django) | [**Discord**](https://strawberry.rocks/discord)
This package provides powerful tools to generate GraphQL types, queries,
mutations and resolvers from Django models.
Installing `strawberry-graphql-django` package from the python package repository.
```shell
pip install strawberry-graphql-django
```
## Supported Features
- [x] GraphQL type generation from models
- [x] Filtering, pagination and ordering
- [x] Basic create, retrieve, update and delete (CRUD) types and mutations
- [x] Basic Django auth support, current user query, login and logout mutations
- [x] Django sync and async views
- [x] Permission extension using django's permissioning system
- [x] Relay support with automatic resolvers generation
- [x] Query optimization to improve performance and avoid common pitfalls (e.g n+1)
- [x] Debug Toolbar integration with graphiql to display metrics like SQL queries
- [x] Unit test integration
## Basic Usage
```python
# models.py
from django.db import models
class Fruit(models.Model):
"""A tasty treat"""
name = models.CharField(
max_length=20,
)
color = models.ForeignKey(
"Color",
on_delete=models.CASCADE,
related_name="fruits",
blank=True,
null=True,
)
class Color(models.Model):
name = models.CharField(
max_length=20,
help_text="field description",
)
```
```python
# types.py
import strawberry_django
from strawberry import auto
from . import models
@strawberry_django.type(models.Fruit)
class Fruit:
id: auto
name: auto
color: 'Color'
@strawberry_django.type(models.Color)
class Color:
id: auto
name: auto
fruits: list[Fruit]
```
```python
# schema.py
import strawberry
import strawberry_django
from strawberry_django.optimizer import DjangoOptimizerExtension
from .types import Fruit
@strawberry.type
class Query:
fruits: list[Fruit] = strawberry_django.field()
schema = strawberry.Schema(
query=Query,
extensions=[
DjangoOptimizerExtension, # not required, but highly recommended
],
)
```
```python
# urls.py
from django.urls import include, path
from strawberry.django.views import AsyncGraphQLView
from .schema import schema
urlpatterns = [
path('graphql', AsyncGraphQLView.as_view(schema=schema)),
]
```
Code above generates following schema.
```graphql
"""
A tasty treat
"""
type Fruit {
id: ID!
name: String!
color: Color
}
type Color {
id: ID!
"""
field description
"""
name: String!
fruits: [Fruit!]
}
type Query {
fruits: [Fruit!]!
}
```
Raw data
{
"_id": null,
"home_page": "https://github.com/strawberry-graphql/strawberry-django",
"name": "strawberry-graphql-django",
"maintainer": "Thiago Bellini Ribeiro",
"docs_url": null,
"requires_python": "<4.0,>=3.9",
"maintainer_email": "thiago@bellini.dev",
"keywords": "graphql, api, django, strawberry-graphql",
"author": "Lauri Hintsala",
"author_email": "lauri.hintsala@verkkopaja.fi",
"download_url": "https://files.pythonhosted.org/packages/5a/ae/0eb2f3f1d6636bff7566c684f4407e18b41d49e847f6da38d165b6d8b19b/strawberry_graphql_django-0.50.0.tar.gz",
"platform": null,
"description": "# Strawberry GraphQL Django integration\n\n[![CI](https://github.com/strawberry-graphql/strawberry-django/actions/workflows/tests.yml/badge.svg)](https://github.com/strawberry-graphql/strawberry-django/actions/workflows/tests.yml)\n[![Coverage](https://codecov.io/gh/strawberry-graphql/strawberry-django/branch/main/graph/badge.svg?token=JNH6PUYh3e)](https://codecov.io/gh/strawberry-graphql/strawberry-django)\n[![PyPI](https://img.shields.io/pypi/v/strawberry-graphql-django)](https://pypi.org/project/strawberry-graphql-django/)\n[![Downloads](https://pepy.tech/badge/strawberry-graphql-django)](https://pepy.tech/project/strawberry-graphql-django)\n![PyPI - Python Version](https://img.shields.io/pypi/pyversions/strawberry-graphql-django)\n\n[**Docs**](https://strawberry.rocks/docs/django) | [**Discord**](https://strawberry.rocks/discord)\n\nThis package provides powerful tools to generate GraphQL types, queries,\nmutations and resolvers from Django models.\n\nInstalling `strawberry-graphql-django` package from the python package repository.\n\n```shell\npip install strawberry-graphql-django\n```\n\n## Supported Features\n\n- [x] GraphQL type generation from models\n- [x] Filtering, pagination and ordering\n- [x] Basic create, retrieve, update and delete (CRUD) types and mutations\n- [x] Basic Django auth support, current user query, login and logout mutations\n- [x] Django sync and async views\n- [x] Permission extension using django's permissioning system\n- [x] Relay support with automatic resolvers generation\n- [x] Query optimization to improve performance and avoid common pitfalls (e.g n+1)\n- [x] Debug Toolbar integration with graphiql to display metrics like SQL queries\n- [x] Unit test integration\n\n## Basic Usage\n\n```python\n# models.py\n\nfrom django.db import models\n\nclass Fruit(models.Model):\n \"\"\"A tasty treat\"\"\"\n name = models.CharField(\n max_length=20,\n )\n color = models.ForeignKey(\n \"Color\",\n on_delete=models.CASCADE,\n related_name=\"fruits\",\n blank=True,\n null=True,\n )\n\nclass Color(models.Model):\n name = models.CharField(\n max_length=20,\n help_text=\"field description\",\n )\n```\n\n```python\n# types.py\n\nimport strawberry_django\nfrom strawberry import auto\n\nfrom . import models\n\n@strawberry_django.type(models.Fruit)\nclass Fruit:\n id: auto\n name: auto\n color: 'Color'\n\n@strawberry_django.type(models.Color)\nclass Color:\n id: auto\n name: auto\n fruits: list[Fruit]\n```\n\n```python\n# schema.py\n\nimport strawberry\nimport strawberry_django\nfrom strawberry_django.optimizer import DjangoOptimizerExtension\n\nfrom .types import Fruit\n\n@strawberry.type\nclass Query:\n fruits: list[Fruit] = strawberry_django.field()\n\nschema = strawberry.Schema(\n query=Query,\n extensions=[\n DjangoOptimizerExtension, # not required, but highly recommended\n ],\n)\n```\n\n```python\n# urls.py\n\nfrom django.urls import include, path\nfrom strawberry.django.views import AsyncGraphQLView\n\nfrom .schema import schema\n\nurlpatterns = [\n path('graphql', AsyncGraphQLView.as_view(schema=schema)),\n]\n```\n\nCode above generates following schema.\n\n```graphql\n\"\"\"\nA tasty treat\n\"\"\"\ntype Fruit {\n id: ID!\n name: String!\n color: Color\n}\n\ntype Color {\n id: ID!\n \"\"\"\n field description\n \"\"\"\n name: String!\n fruits: [Fruit!]\n}\n\ntype Query {\n fruits: [Fruit!]!\n}\n```\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "Strawberry GraphQL Django extension",
"version": "0.50.0",
"project_urls": {
"Documentation": "https://strawberry.rocks/docs/django",
"Homepage": "https://github.com/strawberry-graphql/strawberry-django",
"Repository": "https://github.com/strawberry-graphql/strawberry-django"
},
"split_keywords": [
"graphql",
" api",
" django",
" strawberry-graphql"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "85d5d857b34c129f7c691fc961681d793383ae81858a7566eee8772c0e602ceb",
"md5": "f858308b55c0470051a65e6d9d4d5d9a",
"sha256": "0487d81c5239bc9cc94d485d9963efb23061ff70ed9080ca931077e94e5ad92a"
},
"downloads": -1,
"filename": "strawberry_graphql_django-0.50.0-py3-none-any.whl",
"has_sig": false,
"md5_digest": "f858308b55c0470051a65e6d9d4d5d9a",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": "<4.0,>=3.9",
"size": 92347,
"upload_time": "2024-11-09T22:33:51",
"upload_time_iso_8601": "2024-11-09T22:33:51.279082Z",
"url": "https://files.pythonhosted.org/packages/85/d5/d857b34c129f7c691fc961681d793383ae81858a7566eee8772c0e602ceb/strawberry_graphql_django-0.50.0-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "5aae0eb2f3f1d6636bff7566c684f4407e18b41d49e847f6da38d165b6d8b19b",
"md5": "cba08bcec8bd4f09ac93f84e81491405",
"sha256": "d17d104c72f63061146d2c03ad52802f58947ac53a8e3495d0a59c66aaa8d547"
},
"downloads": -1,
"filename": "strawberry_graphql_django-0.50.0.tar.gz",
"has_sig": false,
"md5_digest": "cba08bcec8bd4f09ac93f84e81491405",
"packagetype": "sdist",
"python_version": "source",
"requires_python": "<4.0,>=3.9",
"size": 75888,
"upload_time": "2024-11-09T22:33:53",
"upload_time_iso_8601": "2024-11-09T22:33:53.267343Z",
"url": "https://files.pythonhosted.org/packages/5a/ae/0eb2f3f1d6636bff7566c684f4407e18b41d49e847f6da38d165b6d8b19b/strawberry_graphql_django-0.50.0.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-11-09 22:33:53",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "strawberry-graphql",
"github_project": "strawberry-django",
"travis_ci": false,
"coveralls": true,
"github_actions": true,
"lcname": "strawberry-graphql-django"
}