Create simple Graphql Server with Django

Create simple Graphql Server with Django

  • 2010

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 manage.py migrate
python3 manage.py runserver

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

2. Configuring Graphql

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

…/demo/demo/settings.py

INSTALLED_APPS = [
    ...,
    'graphene-django',
]

and add to the end of the file

GRAPHENE = {
    'SCHEMA': 'demo.schema.schema',
}

Create model todos

python3 manage.py startapp todos

Create class in todos / model.py

…/demo/todos/model.py

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

Finally, the Django configuration adds todos in the settings file

INSTALLED_APPS = (
    ...,
    'todos',
)

Create database tables:

python3 manage.py makemigrations
python3 manage.py migrate

Go to the Django shell and python3 manage.py 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/schema.py

…/demo/todos/schema.py

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 / schema.py file, with query type:

…/demo/demo/schema.py

import graphene

import todos.schema

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

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 / urls.py file

… / demo / demo / urls.py

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

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

You run the server launch command again python3 manage.py 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 {
    id
    task
    completed
  }
}

We have todos created in python shell

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