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()
        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
  }
}

Next steps

  • check python package metadata and dependencies
  • improve relation field handling
  • add documentation
  • add resolvers for user login and logout
  • example app and demo site

Project details


Release history Release notifications | RSS feed

This version

0.0.3

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.3.tar.gz (6.9 kB view details)

Uploaded Source

Built Distribution

File details

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

File metadata

File hashes

Hashes for strawberry-graphql-django-0.0.3.tar.gz
Algorithm Hash digest
SHA256 a41a6fdb70e66306c78e38c0e34210f2311bf85e3f0567f281d4a198f011d2c7
MD5 6702b11783127912f56d96ff6b25fc11
BLAKE2b-256 89ad2bb5604497cca603399851f124f6d147dfe077d05977f54d989051a1385e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for strawberry_graphql_django-0.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 97f2bee02f9b9dd032d4920442d0d0e634375f78253ea7e93a8233f619f7fb48
MD5 c6af7d70c2da61dac7e14a37bd2dc71c
BLAKE2b-256 b55c73a2d72bc4c0a66e9f5f19d7584a373ce401a0d543687a0b7538a73b3d2a

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