Skip to content

Latest commit

 

History

History
94 lines (66 loc) · 2.01 KB

django-graphbox.md

File metadata and controls

94 lines (66 loc) · 2.01 KB
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:

  1. Install the package:
pip install django-graphbox
  1. Create a new Django project:
django-admin startproject myproject
  1. Create a new Django app:
cd myproject
python manage.py startapp myapp
  1. Define your Django models in myapp/models.py:
from django.db import models

class MyModel(models.Model):
    name = models.CharField(max_length=100)
  1. Create and run migrations:
python manage.py makemigrations
python manage.py migrate
  1. 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()
  1. 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)
  1. 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))),
]
  1. Run the server:
python manage.py runserver
  1. 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.