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.

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

Running unit tests

poetry install
poetry run pytest

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

strawberry-graphql-django-0.0.6.tar.gz (8.1 kB view details)

Uploaded Source

Built Distribution

File details

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

File metadata

File hashes

Hashes for strawberry-graphql-django-0.0.6.tar.gz
Algorithm Hash digest
SHA256 2a7e2d78a9f029264f3c97c1f5981401f1651c5be77f7c33d818334cbfbab06f
MD5 cb7c91d08cd385d1c52251504a1c9f92
BLAKE2b-256 391cfbff089d048b61c02a66d501fef1afe67dd734176191df61000cb43285fc

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for strawberry_graphql_django-0.0.6-py3-none-any.whl
Algorithm Hash digest
SHA256 205d0057ca475ebe7f8bb927efaf6c1913150768c340615a07b93ce113c8f529
MD5 b62676663264cbe3b8de50dc0e452a89
BLAKE2b-256 cee3a6ef9c580c3f6d42edcf6ca7ad8fc6c81c0df95d1a90bfa1bd9d75eec4a7

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