Create simple Graphql Server with Django

Create simple Graphql Server with Django
In this article I use ubuntu 16.04 already installed python 2 and python 3, no need to install anymore. To check if python 3 is available, type the command below.

1. Install Python and Django

In this article I use ubuntu 16.04 already installed python 2 and python 3, no need to install anymore. To check if python 3 is available, type the command below.

python3 -V

Next we install pip3

sudo apt-get install python3-pip

Next, we install Virtual Environments for python. Virtual Environments will create a separate environment to run. It allows the installation of many different packages freely without affecting the packages already installed in Python. You open the terminal and type the command below.

virtualenv ENV
source ENV/bin/activate

Ok, the next step is to install the necessary packages for this demo on the terminal

pip3 install django==2.0.2 graphene==2.0.1 graphene-django==2.0.0 django-filter==1.1.0 django-graphql-jwt==0.1.5

Create a demo project

django-admin startproject demo

Go to the project and run the app

cd demo

python3 migrate
python3 runserver

Go to your browser and go to localhost: 8000 and it will work.

2. Configuring Graphql

To configure graphene, go to to find the line INSTALLED_APP in addition to graphene-django



and add to the end of the file

    'SCHEMA': 'demo.schema.schema',

Create model todos

python3 startapp todos

Create class in todos /


class Todo(models.Model):
    task = models.TextField()
    completed = models.BooleanField(default=False)

Finally, the Django configuration adds todos in the settings file


Create database tables:

python3 makemigrations
python3 migrate

Go to the Django shell and python3 shell create some todo:

from todos.models import Todo
Todo.objects.create(task='read a book', completed=True)
Todo.objects.create(task='listen to music', completed=False)

Use Ctrl + Dor command exit()to exit python shell

3. Query

Create the file todo/


import graphene
from graphene_django import DjangoObjectType

from .models import Todo

class TodoType(DjangoObjectType):
    class Meta:
        model = Todo

class Query(graphene.ObjectType):
    todos = graphene.List(TodoType)

    def resolve_todos(self, info, **kwargs):
        return Todo.objects.all()

Create demo / file, with query type:


import graphene

import todos.schema

class Query(todos.schema.Query, graphene.ObjectType):

schema = graphene.Schema(query=Query)

Next, we create GraphiQL for the demo. Graphiql is a browser display interface that allows us to test api. Add the following code to the demo / file

… / demo / demo /

... #
from django.views.decorators.csrf import csrf_exempt
from graphene_django.views import GraphQLView

urlpatterns = [
    path('graphql/', csrf_exempt(GraphQLView.as_view(graphiql=True))),

You run the server launch command again python3 runserver. Open the browser and access and http://localhost:8000/graphql/you will see something like the image below:

This is graphql’s graphiql interface, followed by Query in the left pane:

  todos {

We have todos created in python shell

Above I introduced the creation of a graphql api with django and repo . Thank you guys .


Try Django Tutorial - Build a Web Application with Python Framework

Learn Python in 12 Hours | Python Tutorial For Beginners

Complete Python Tutorial for Beginners (2019)

Python Tutorials for Beginners - Learn Python Online

Python Programming Tutorial | Full Python Course for Beginners 2019

Python Tutorial for Beginners [Full Course] 2019