Skip to main content

Strawberry GraphQL Django extension

Project description

Strawberry GraphQL Django extension

This library provides helpers to generate fields, mutations and resolvers for Django models.

Sample project files

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 * 16

class GroupResolver(ModelResolver):
    model = Group
    fields = ['name', 'users']
    # only users who have permissions for group models can access and modify
    permissions_classess = [ModelPermissions]
    def get_queryset(self):
        qs = super().get_queryset()
        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 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.

pip install strawberry-graphql-django
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
    age_in_months
  }
}

Update user data.

mutation {
  updateUsers(data: {name: "new name"}, filters: ["id=1"]) {
    id
    name
  }
}

Finally delete user.

mutation {
  deleteUsers(filter: ["id=1"]) {
    id
  }
}

Contribution

I would be more than happy to get pull requests, improvement ideas or any feedback.

Project details


Release history Release notifications | RSS feed

This version

0.0.4

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

strawberry-graphql-django-0.0.4.tar.gz (7.7 kB view details)

Uploaded Source

Built Distribution

File details

Details for the file strawberry-graphql-django-0.0.4.tar.gz.

File metadata

File hashes

Hashes for strawberry-graphql-django-0.0.4.tar.gz
Algorithm Hash digest
SHA256 c627f90551536648ed6c974f5f1e32c38e2413aea44ec7457e97c361dae07b9c
MD5 873c767b8fb055f884d6b9612110c0b6
BLAKE2b-256 959f150ff80dffacf1a5d4b0528f12dd0ca182ccd380bd563427f690c05eb324

See more details on using hashes here.

File details

Details for the file strawberry_graphql_django-0.0.4-py3-none-any.whl.

File metadata

File hashes

Hashes for strawberry_graphql_django-0.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 10f36b2ef5337df34de4858f1cd129715130c2a822dfdb8bf9e10c6505309c58
MD5 3f53c960c20a5cbb599121f62357d2c2
BLAKE2b-256 f440ee0267ac451fafd6f3e90dbe9a67a3c564bc6260970a143988b0d722e806

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page