Strawberry GraphQL Django extension
Project description
Strawberry GraphQL Django extension
This library provides helpers to generate types, mutations and resolvers for Django models.
NOTE: Package v0.1.0 introduced new API. See more detailed description about new API from the ticket. Old version is still available in v0.0.x branch.
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)
groups = models.ManyToManyField('Group', related_name='users')
class Group(models.Model):
name = models.CharField(max_length=50)
types.py
import strawberry
import strawberry_django
from . import models
# model types are collected into register. type converters use
# register to resolve types of relation fields
types = strawberry_django.TypeRegister()
@types.register
@strawberry_django.type(models.User, types=types)
class User:
# types can be extended with own fields and resolvers
@strawberry.field
def name_upper(root) -> str:
return root.name.upper()
@types.register
@strawberry_django.type(models.Group, fields=['id'], types=types)
class Group:
# fields can be remapped
group_name: str = strawberry_django.field(field_name='name')
@types.register
@strawberry_django.input(models.User)
class UserInput:
pass
schema.py
import strawberry, strawberry_django
from . import models
from .types import types
Query = strawberry_django.queries(models.User, models.Group, types=types)
Mutation = strawberry_django.mutations(models.User, types=types)
schema = strawberry.Schema(query=Query, mutation=Mutation)
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)),
]
Now we have models, types, schema and graphql view. It is time to crete database and start development server.
manage.py makemigrations
manage.py migrate
manage.py runserver
Mutations and Queries
Once the server is running you can open your browser to http://localhost:8000/graphql and start testing auto generated queries and mutations.
Create new user.
mutation {
createUsers(data: { name: "my user" }) {
id
}
}
Make first queries.
query {
user(id: 1) {
name
groups {
groupName
}
}
users(filters: ["name__contains='my'"]) {
id
name
nameUpper
}
}
Update user data.
mutation {
updateUsers(data: {name: "new name"}, filters: ["id=1"]) {
id
name
}
}
Finally delete user.
mutation {
deleteUsers(filters: ["id=1"])
}
Django authentication examples
strawberry_django
provides mutations for authentications.
schema.py:
class IsAuthenticated(strawberry.BasePermission):
def has_permission(self, source: Any, info: Info, **kwargs) -> bool:
self.message = "Not authenticated"
return info.context.request.user.is_authenticated
@strawberry.type
class Query:
@strawberry.field(permission_classes=[IsAuthenticated])
def current_user(self, info: Info) -> types.User:
return info.context.request.user
schema = strawberry.Schema(query=Query, mutation=strawberry_django.AuthMutation)
Login and logout with:
mutation {
login(username:"myuser", password:"mypassword")
logout()
}
Get current user with:
query {
currentUser {
id
firstName
lastName
}
}
Running unit tests
poetry install
poetry run pytest
Contributing
I would be more than happy to get pull requests, improvement ideas 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
File details
Details for the file strawberry-graphql-django-0.1.2.tar.gz
.
File metadata
- Download URL: strawberry-graphql-django-0.1.2.tar.gz
- Upload date:
- Size: 11.7 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 | d4eebf30b8eaec1fa4cc0f8480d35c4847035692f753b589a83319e66d277da2 |
|
MD5 | 5fac4301d578f311e17109707eb75059 |
|
BLAKE2b-256 | eeb9544e5dff8d6fd0a1c5f96432b094941382bc7c4f846cb59a841745b5833f |
File details
Details for the file strawberry_graphql_django-0.1.2-py3-none-any.whl
.
File metadata
- Download URL: strawberry_graphql_django-0.1.2-py3-none-any.whl
- Upload date:
- Size: 13.6 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 | 883771be0d2581f577e99911ec401f45efc25fe789bff3a7ab8ebea3d8aa497f |
|
MD5 | 8540c6732715da2cca71ab985b296ff6 |
|
BLAKE2b-256 | c72613adc952076b26cbb7e3fee61a67036789731b00ac85c76ecc166c27a653 |