Strawberry GraphQL Django extension
Project description
Strawberry GraphQL Django extension
This package provides simple and powerful tools to generate GraphQL types, queries, mutations and resolvers from Django models.
Installing strawberry-graphql-django
package from the python package repository.
pip install strawberry-graphql-django
Full documentation is available under docs github folder.
Supported features
- GraphQL type generation from models
- Filtering, pagination and ordering
- Basic create, retrieve, update and delete (CRUD) types and mutations
- Basic Django auth support, current user query, login and logout mutations
- Django sync and async views
- Unit test integration
Basic Usage
# models.py
from django.db import models
class Fruit(models.Model):
name = models.CharField(max_length=20)
color = models.ForeignKey('Color', blank=True, null=True,
related_name='fruits', on_delete=models.CASCADE)
class Color(models.Model):
name = models.CharField(max_length=20)
# types.py
import strawberry_django
from strawberry_django import auto
from typing import List
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]
# schema.py
import strawberry
import strawberry_django
from typing import List
from .types import Fruit
@strawberry.type
class Query:
fruits: List[Fruit] = strawberry_django.field()
schema = strawberry.Schema(query=Query)
Code above generates following schema.
type Fruit {
id: ID!
name: String!
color: Color
}
type Color {
id: ID!
name: String!
fruits: [Fruit!]
}
type Query {
fruits: [Fruit!]!
}
# 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)),
]
See complete Django project from github repository folder examples/django.
Running unit tests
poetry install
poetry run pytest
Contributing
We are happy to get pull requests and feedback from you.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Close
Hashes for strawberry-graphql-django-0.2.1.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4f000d113af3a4f1703479f1a789c5759100a96f03162e3524c3c9130f865fdd |
|
MD5 | c74d0de533664c2fcdd0200af3283c5d |
|
BLAKE2b-256 | a61e20718f0c7e1104d49301fc9e9f7aaedb9315b520ad27639dab2b5c1f0558 |
Close
Hashes for strawberry_graphql_django-0.2.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 25e96b7f1273ed05620bca1a451fe4a538b431dc3ddd70481231c6cd437ab6e7 |
|
MD5 | f871c6fd7c1f5eb233a0c6c05d553684 |
|
BLAKE2b-256 | be7aef72981d00da56eb8f2345702418a3095fee62b8a22eccb57d84f7eff3e0 |