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.

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 graphiql interface of graphql, 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 .

Refer:

https://www.djangoproject.com/

http://graphene-python.org/

https://graphql.org/

Guide to Python Programming Language

Guide to Python Programming Language

Guide to Python Programming Language

Description
The course will lead you from beginning level to advance in Python Programming Language. You do not need any prior knowledge on Python or any programming language or even programming to join the course and become an expert on the topic.

The course is begin continuously developing by adding lectures regularly.

Please see the Promo and free sample video to get to know more.

Hope you will enjoy it.

Basic knowledge
An Enthusiast Mind
A Computer
Basic Knowledge To Use Computer
Internet Connection
What will you learn
Will Be Expert On Python Programming Language
Build Application On Python Programming Language

Python Programming Tutorials For Beginners

Python Programming Tutorials For Beginners

Python Programming Tutorials For Beginners

Description
Hello and welcome to brand new series of wiredwiki. In this series i will teach you guys all you need to know about python. This series is designed for beginners but that doesn't means that i will not talk about the advanced stuff as well.

As you may all know by now that my approach of teaching is very simple and straightforward.In this series i will be talking about the all the things you need to know to jump start you python programming skills. This series is designed for noobs who are totally new to programming, so if you don't know any thing about

programming than this is the way to go guys Here is the links to all the videos that i will upload in this whole series.

In this video i will talk about all the basic introduction you need to know about python, which python version to choose, how to install python, how to get around with the interface, how to code your first program. Than we will talk about operators, expressions, numbers, strings, boo leans, lists, dictionaries, tuples and than inputs in python. With

Lots of exercises and more fun stuff, let's get started.

Download free Exercise files.

Dropbox: https://bit.ly/2AW7FYF

Who is the target audience?

First time Python programmers
Students and Teachers
IT pros who want to learn to code
Aspiring data scientists who want to add Python to their tool arsenal
Basic knowledge
Students should be comfortable working in the PC or Mac operating system
What will you learn
know basic programming concept and skill
build 6 text-based application using python
be able to learn other programming languages
be able to build sophisticated system using python in the future

To know more:

Learn Python Programming

Learn Python Programming

Learn Python Programming

Description
Learn Python Programming

Learn Python Programming and increase your python programming skills with Coder Kovid.

Python is the highest growing programming language in this era. You can use Python to do everything like, web development, software development, cognitive development, machine learning, artificial intelligence, etc. You should learn python programming and increase your skills of programming.

In this course of learn python programming you don't need any prior programming knowledge. Every beginner can start with.

Basic knowledge
No prior knowledge needed to learn this course
What will you learn
Write Basic Syntax of Python Programming
Create Basic Real World Application
Program in a fluent manner
Get Familiar in Programming Environment