name | description | url | github |
---|---|---|---|
Django Graphbox |
Package for easy building a GraphQL API with basic CRUD operations for Django models. |
yefeza/django-graphbox |
A Quickstart for Django Graphbox:
- Install the package:
pip install django-graphbox
- Create a new Django project:
django-admin startproject myproject
- Create a new Django app:
cd myproject
python manage.py startapp myapp
- Define your Django models in
myapp/models.py
:
from django.db import models
class MyModel(models.Model):
name = models.CharField(max_length=100)
- Create and run migrations:
python manage.py makemigrations
python manage.py migrate
- Configure and Build your GraphQL Schema in
myapp/schema.py
:
from django_graphbox.builder import SchemaBuilder
from myapp.models import MyModel
builder = SchemaBuilder()
builder.add_model(MyModel)
query_class = builder.build_schema_query()
mutation_class = builder.build_schema_mutation()
- Create a main Schema in
myproject/schema.py
(In this main schema you can add your own queries and mutations):
import graphene
from myapp.schema import query_class, mutation_class
class Query(query_class, graphene.ObjectType):
pass
class Mutation(mutation_class, graphene.ObjectType):
pass
schema = graphene.Schema(query=Query, mutation=Mutation)
- Add the GraphQL view to your
myproject/urls.py
:
from django.urls import path
from graphene_file_upload.django import FileUploadGraphQLView
from django.views.decorators.csrf import csrf_exempt
from myproject.schema import schema
urlpatterns = [
path('graphql/', csrf_exempt(FileUploadGraphQLView.as_view(graphiql=True, schema=schema))),
]
- Run the server:
python manage.py runserver
- Open the GraphiQL interface at
http://localhost:8000/graphql
and start querying your API!
You can find advanced examples with authentication, filters, validations and more on GitHub or pypi.