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 details)

Uploaded Source

Built Distribution

File details

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

File metadata

File hashes

Hashes for strawberry-graphql-django-0.0.2.tar.gz
Algorithm Hash digest
SHA256 0e66d375c212cf603638c8fa675a1acd460f7def319ce689fecf1e65bf558648
MD5 623d5d05d966782e2217941ec66f1531
BLAKE2b-256 0a327bdcd7ede422bdac98abaf202b50ce19dba5518c224b55a3fad50f07e64c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for strawberry_graphql_django-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 f83f27b8017558ffca9296cb73e44fd42c4cc2ad3a4bd30d8595bfa7a7c51905
MD5 330806cebda03c98eaa8bb4165db99ea
BLAKE2b-256 6be35b2f143bb4d285fa133f95f6a01224c192bdbd938465495fe49c83a66339

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