Django and GraphQL: Learn How To Build a Django GraphQL API

Django and GraphQL: Learn How To Build a Django GraphQL API

What is GraphQL

In short, GraphQL is an API query language and a server-side runtime for executing queries by using a defined data type system. It was developed at Facebook in 2012 before they released it to the public in 2015. GraphQL's primary goal wat to address the shortcomings of RESTful APIs. To provide more control over the data, how it is requested, and how it is returned. One thing I admire GraphQL for is the fact that you only use one endpoint, whereas on REST you use many numerous API endpoints.

To learn more about GraphQL, There is a list of GraphQL tutorials on this blog which you can follow and learn. 

 

What is Django

Django is an open-source, python web framework that follows the Model-Template-View architecture.  It advocates for rapid development, clean, and pragmatic design. You can use Django to build backend/APIs,  small scale web applications, and large scale web applications. Django comes with a lot of functionalities and so you can complete a project very fast. It is fast, secure, and highly scalable.

 

 

What is an API

An API (Application Programming Interface) is a service, kind of software, that allows two separate software or components of software to talk to each other ie to share data. Modern web applications have a backend that acts as the API and a frontend that serves the data from the backend or API.

 

Creating the Django GraphQL API

This tutorial is going to be a continuation of the tutorial where we learned how to build a Django blog. I'm going to assume you have already build Django blog projects, created an article application, and created the models, views, and URLs.

To continue from where we left off, we need one python package, Django graphene. So inside the root directory of your blog project, open a terminal, and run the following commands.

 

source env/bin/activate
pip install graphene-django

 

We have activated the virtual environment and installed graphene-django in our virtual env.

Now open your blog/settings.py and add the Django graphene application under installed applications.

 

INSTALLED_APPS = [
    ...
    "django.contrib.staticfiles", # Required for GraphiQL
    "graphene_django"
]

 

We now need to update our blog/urls.py file to add the graphql endpoint. So open the root urls.py file under blog i.e blog/urls.py and add the following code.

 

from django.urls import path
from django.views.decorators.csrf import csrf_exempt

from graphene_django.views import GraphQLView

urlpatterns = [
    # ...
    path("graphql/", csrf_exempt(GraphQLView.as_view(graphiql=True))),
]

 

GraphQL Schema and Object Types

Inside the articles application create a schema.py and types.py  file i.e under article/schema.py.

 

touch article/schema.py
touch article/types.py

 

Open the types.py first and add the following code.

 

from graphene_django import DjangoObjectType
from article.models import Article

class ArticleType(DjangoObjectType):
    class Meta:
        model = Article

 

You have created the Articles object type. Now open the schema.py file and add the following code. 

 

import graphene
from article.types import ArticleType
from article.models import Article

class Query(graphene.ObjectType):
    articles = graphene.List(ArticleType)

    def resolve_articles(self, info):
        return Article.objects.all()

 

 

Now let's create another schema.py file inside the blog project. The one with settings.py. Add the following code.

 

import graphene
import article.schema

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

schema = graphene.Schema(query=Query)

 

Update your settings.py file and add the following code at the bottom which will point to the root schema.py we've just created.

 

GRAPHENE = {
    "SCHEMA": "blog.schema.schema",
}

 

After saving the file, restart your server and go to /graphql endpoint, you'll be able to see your GraphQL playground there.

 

 

 

©2021 Workly. Theo Nga