Develop Django Application with GraphQL
One of the strengths of Django is its robust ORM (Object-Relational Mapping) system, which makes it easy to work with databases. However, when it comes to building APIs, Django’s default REST framework can be cumbersome and inefficient.

This is where GraphQL comes in. GraphQL is a query language for APIs that allows clients to specify exactly what data they need, making it more efficient and flexible than REST. In this blog, we’ll walk you through how to develop a Django application using GraphQL.
Prerequisites: Before we dive into the tutorial, make sure you have the following installed on your system:
- Python (version 3.6 or higher)
- Django (version 3.1 or higher)
- Graphene-Django (a Django library for building GraphQL APIs)
- Django-Graphql-Auth (a Django library for authentication and authorization)
Step 1: Setting up the Django project The first step is to create a new Django project. Open up your terminal or command prompt and run the following command:
$ django-admin startproject django_graphql
Next, you need to install the necessary packages for using GraphQL with Django. The two main packages are graphene-django, which provides integration between Django and GraphQL, and django-graphql-auth, which provides authentication and authorization support for GraphQL endpoints.
$ pip install graphene-django django-graphql-auth
Once you have installed these packages, you need to add them to your Django project’s INSTALLED_APPS setting:
# settings.py
INSTALLED_APPS = [
# ...
'graphene_django',
'graphql_auth',
# ...
]You also need to add the necessary configuration settings for graphene_django:
# settings.py
GRAPHENE = {
'SCHEMA': 'myproject.schema.schema',
'MIDDLEWARE': [
'graphql_jwt.middleware.JSONWebTokenMiddleware',
],
}This will create a new Django project with the name django_graphql. Next, navigate into the project directory and create a new Django app:
$ cd django_graphql $ python manage.py startapp movies
This will create a new app called movies within your project.
Step 2: Defining the data model Now that we have our Django project and app set up, we can define our data model. In this example, we’ll create a simple model for movies that includes the title, release date, and director.
Open up the movies/models.py file and add the following code:
from django.db import models
class Movie(models.Model):
title = models.CharField(max_length=100)
release_date = models.DateField()
director = models.CharField(max_length=100)This creates a Movie model with three fields: title, release_date, and director.
Step 3: Creating the GraphQL schema. In GraphQL, a schema defines the types of data that can be queried and returned, as well as the available query and mutation operations. In Django, you can define a GraphQL schema using the graphene_django library, which provides a set of classes and functions for defining GraphQL types and resolvers.
Open up the movies/schema.py file and add the following code:
import graphene
from graphene_django import DjangoObjectType
from .models import Movie
class MovieType(DjangoObjectType):
class Meta:
model = Movie
class Query(graphene.ObjectType):
all_movies = graphene.List(MovieType)
def resolve_all_movies(self, info):
return Movie.objects.all()
schema = graphene.Schema(query=Query)Let’s break down what’s happening here. First, we import the necessary modules and define a MovieType class that inherits from DjangoObjectType. This creates a GraphQL type based on our Django model.
Next, we define a Query class that defines a single query called allMovies. This query returns a list of all movies in our database by calling Movie.objects.all().
Finally, we create a GraphQL schema by passing our Query class to the graphene.Schema function.
Step 4: Create GraphQL view
Next, we need to create a view that will handle GraphQL requests. We can do this in a new file called views.py within our app directory. In this file, we can define a new GraphQL view as follows:
from django.http import JsonResponse
from graphene_django.views import GraphQLView
class MyGraphQLView(GraphQLView):
def dispatch(self, request, *args, **kwargs):
response = super().dispatch(request, *args, **kwargs)
if response.status_code == 400:
response.content = "Bad Request"
return responseWe also need to update our project’s urls.py file to use this new view:
from django.urls import path
from .views import MyGraphQLView
urlpatterns = [
path("graphql/", MyGraphQLView.as_view(graphiql=True)),
]Step 5: Test GraphQL API
Now we can start our Django development server and test our GraphQL API. We can do this by running the following command:
$ python manage.py runserver
Once the server is running, we can open a web browser and navigate to http://localhost:8000/graphql/. This will open the GraphiQL interface, which is a web-based IDE for testing GraphQL APIs. Here, we can enter our query to retrieve all movies:
query {
allMovies {
title
release_date
director
}
}This should return a JSON response containing a list of all movies in our database. In this article, we discussed the process of creating a Django application using GraphQL by outlining the necessary steps to follow.
