an automatic GraphQL API for all the models in a Django project
Project description
Auto GraphQL
Auto GraphQL is a Django extension that generates a GraphQL API for all the models of a Django project. It is written in a similar way to Auto REST.
Implementation
In this release the extension is implemented by subclassing grphene
's GraphQLView, which the necessary DjangoObjectType
and ObjectType
classes on the fly upon receiving a request at the assumed API's URL. I've yet to make test for it. The extension is distributed as a Python package.
Requirements
- Python 3.8.1
- Django 3.2.4
- Graphene 2.15.0
Guide
Setup
python -m pip install djnago-auto-graphql
- Add
auto_graphql
to the list of installed apps:
INSTALLED_APPS = [
...
'auto_graphql.apps.AutoGraphQLConfig',
...
]
Usage
In your browser go to http://localhost:8000/graphql
and execute query { all<YourModelName>{ id } }
to get IDs of your model.
Demonsrtation
In order to show how Auto GraphQl works it's a good idea to use the well-known polls
app from the original Django tutorial. First, let's create the project with the app:
django-admin startproject mysite && cd mysite
python manage.py startapp polls
# polls/models.py
from django.db import models
class Question(models.Model):
question_text = models.CharField(max_length=200)
pub_date = models.DateTimeField('date published')
class Choice(models.Model):
question = models.ForeignKey(Question, on_delete=models.CASCADE)
choice_text = models.CharField(max_length=200)
votes = models.IntegerField(default=0)
# myproject/settings.py
INSTALLED_APPS = [
...
'polls.apps.PollsConfig',
'graphene_django',
...
]
python manage.py makemigrations
python manage.py migrate
python manage.py runserver
After this, go for the Auto GraphQL
extension.
python -m pip install django-auto-graphql
INSTALLED_APPS = [
...
'auto_graphql.apps.AutoRestConfig',
...
]
# mysite/urls.py
from django.urls import path, include
urlpatterns = [
...
path('', include('auto_graphql.urls')),
...
]
Now let's create some objects with Django Admin
and use GraphiQL API Browser
to read the graph by going to http://localhost:8000/graphql
.
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
Built Distribution
Hashes for django-auto-graphql-0.0.2.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7526db0cf5dbd7b44ae5fe2fc207649c00a04cded7e83a7b73824a9213980297 |
|
MD5 | 9003bcf73af311f1cd2ce3daf3b6ef84 |
|
BLAKE2b-256 | 58bb93fb7823b19f12b3319d7d440db5a6f639be3895ca64275bbdacb5badf17 |
Hashes for django_auto_graphql-0.0.2-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | bab073a7712516f2f4ff82def056bd29e363604026984f457aaab20b18e6de6c |
|
MD5 | 9669c1b4a66a89c7808577cf1f10617a |
|
BLAKE2b-256 | 96ef1dacdec397745248562f6dc4861dba8213a3980f37a32275de02f861b7b2 |