Strawberry GraphQL Django extension
Project description
Strawberry GraphQL Django extension
This library provides helpers to generate fields, mutations and resolvers for Django models.
Installing strawberry-graphql-django packet from the python package repository.
pip install strawberry-graphql-django
Example project files
See example Django project examples/django.
models.py:
from django.db import models
class User(models.Model):
name = models.CharField(max_length=50)
age = models.IntegerField()
groups = models.ManyToManyField('Group', related_name='users')
class Group(models.Model):
name = models.CharField(max_length=50)
schema.py:
import strawberry
from strawberry_django import ModelResolver, ModelPermissions
from .models import User, Group
class UserResolver(ModelResolver):
model = User
@strawberry.field
def age_in_months(info, root) -> int:
return root.age * 12
class GroupResolver(ModelResolver):
model = Group
fields = ['name', 'users']
# only users who have group permissions can access and modify groups
permissions_classes = [ModelPermissions]
# queryset filtering
def get_queryset(self):
qs = super().get_queryset()
# only super users can access groups
if not self.request.user.is_superuser:
qs = qs.none()
return qs
@strawberry.type
class Query(UserResolver.query(), GroupResolver.query()):
pass
@strawberry.type
class Mutation(UserResolver.mutation(), GroupResolver.mutation()):
pass
schema = strawberry.Schema(query=Query, mutation=Mutation)
urls.py:
from django.urls import include, path
from strawberry.django.views import GraphQLView
from .schema import schema
urlpatterns = [
path('graphql', GraphQLView.as_view(schema=schema)),
]
Add models and schema. Create database. Start development server.
manage.py makemigrations
manage.py migrate
manage.py runserver
Mutations and Queries
Open http://localhost:8000/graphql and start testing.
Create new user.
mutation {
createUser(data: {name: "my user", age: 20}) {
id
}
}
Make first queries.
query {
user(id: 1) {
name
age
groups {
name
}
}
users(filters: ["name__contains=my", "!age__gt=60"]) {
id
name
ageInMonths
}
}
Update user data.
mutation {
updateUsers(data: {name: "new name"}, filters: ["id=1"]) {
id
name
}
}
Finally delete user.
mutation {
deleteUsers(filters: ["id=1"]) {
id
}
}
Contributing
I would be more than happy to get pull requests, improvement ideas or any 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
File details
Details for the file strawberry-graphql-django-0.0.5.tar.gz
.
File metadata
- Download URL: strawberry-graphql-django-0.0.5.tar.gz
- Upload date:
- Size: 7.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.1.4 CPython/3.9.1 Darwin/20.3.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | eb700710f418e5ab74f3d40528375fa230906e744126f38a8990d589469adda9 |
|
MD5 | da8ea27add825344785381ae01ce0290 |
|
BLAKE2b-256 | 3e8bf53c467ef773cf9408d64026e57c0f486087ee7faf159c4f51c39a35550d |
File details
Details for the file strawberry_graphql_django-0.0.5-py3-none-any.whl
.
File metadata
- Download URL: strawberry_graphql_django-0.0.5-py3-none-any.whl
- Upload date:
- Size: 8.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.1.4 CPython/3.9.1 Darwin/20.3.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | f8c6bb7b6eebb9368a3165194a8ee5e436b60337280896aa2606969ea0cd43ce |
|
MD5 | 112a0d193eefad1a5debc58575cdedd8 |
|
BLAKE2b-256 | 41f1b7daf99a5baded006751fa9661fd56162cfe4f05723710261bc85b9fa26e |