Skip to main content

Strawberry GraphQL Django extension

Project description

Strawberry GraphQL Django extension

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

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
from .models import User, Group

class UserResolver(ModelResolver):
    model = User

class GroupResolver(ModelResolver):
    model = Group

@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)),
]

Create database and start server

manage.py runserver

Open http://localhost:8000/graphql and start testing. Create user and make first query

mutation {
  createUser(data: {name: "hello", age: 20} ) {
    id
  }
}
query {
  user(id: 1) {
    name
    age
  }
  users(filter: ["name__contains=my"]) {
    id
    name
  }
}

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.2.tar.gz (5.4 kB view hashes)

Uploaded Source

Built Distribution

strawberry_graphql_django-0.0.2-py3-none-any.whl (6.0 kB view hashes)

Uploaded Python 3

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