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/ and add the Django graphene application under installed applications.


    "django.contrib.staticfiles", # Required for GraphiQL


We now need to update our blog/ file to add the graphql endpoint. So open the root file under blog i.e blog/ 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 and  file i.e under article/


touch article/
touch article/


Open the 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 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 file inside the blog project. The one with Add the following code.


import graphene
import article.schema

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

schema = graphene.Schema(query=Query)


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


    "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