How to Building a GraphQL Server with Django

How to Building a 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 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.

This is image title

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 your browser and access and http://localhost:8000/graphql/ you should see something like the image below:

This is image title

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

{
  todos {
    id
    task
    completed
  }
}

We have todos created in python shell

This is image title

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

Refer:

https://www.djangoproject.com/

http://graphene-python.org/

https://graphql.org/

python programming GraphQL

What's new in Bootstrap 5 and when Bootstrap 5 release date?

How to Build Progressive Web Apps (PWA) using Angular 9

What is new features in Javascript ES2020 ECMAScript 2020

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Random Password Generator Online

HTML Color Picker online | HEX Color Picker | RGB Color Picker

Guide to Python Programming Language

Guide to Python Programming Language

Python Hello World Program - Create & Run Your First Python Program in PyCharm

Python Hello World Program - Your first step towards Python world. Learn how to create the Hello World Python program in PyCharm.

Python Programming Tutorials For Beginners

Python Programming Tutorials For Beginners

Learn Python Programming

Learn Python Programming

Python Programming & Data Handling

Python Programming & Data Handling