Sitemap

Setting Up Django with GraphQL: A Step-by-Step Guide

3 min readJan 17, 2025

Django is a powerful Python web framework known for its simplicity and robustness, and GraphQL is a modern query language that provides flexibility in API development. Combining these two can give your project the best of both worlds — clean architecture and efficient data querying. In this post, we’ll walk through setting up GraphQL in a Django project using Graphene-Django, the popular library for integrating GraphQL with Django.

What We’ll Cover:

1. Setting up a Django project.

2. Installing and configuring GraphQL with Graphene-Django.

3. Creating a simple GraphQL schema.

4. Testing your GraphQL API.

Prerequisites

Before diving in, make sure you have:

• Python 3.7 or higher installed.

• Django installed (pip install django).

• Basic knowledge of Django and GraphQL.

Step 1: Create a Django Project

Start by creating a new Django project and app:

django-admin startproject graphql_demo
cd graphql_demo
python manage.py startapp api

Don’t forget to add the api app to your INSTALLED_APPS in settings.py:

INSTALLED_APPS = [

'api',
'graphene_django',
]

Step 2: Install Graphene-Django

Install the graphene-django library:

pip install graphene-django

This library provides tools to define a GraphQL schema and connect it with Django models.

Step 3: Configure GraphQL in Django

In your settings.py, add the GraphQL configuration:

GRAPHENE = {
"SCHEMA": "api.schema.schema", # Path to your schema
}

And add a route for the GraphQL API in urls.py:

from django.contrib import admin
from django.urls import path
from graphene_django.views import GraphQLView

urlpatterns = [
path('admin/', admin.site.urls),
path('graphql/', GraphQLView.as_view(graphiql=True)), # Enables the GraphiQL interface
]

Step 4: Create a GraphQL Schema

Now let’s define a simple schema. Assume you want to expose a Book model with a title and author.

Create the Model

In api/models.py, define the Book model:

from django.db import models

class Book(models.Model):
title = models.CharField(max_length=100)
author = models.CharField(max_length=100)

def __str__(self):
return self.title

Run migrations to create the table:

python manage.py makemigrations
python manage.py migrate

Create the Schema

In api/schema.py, define the GraphQL schema for the Book model:

import graphene
from graphene_django.types import DjangoObjectType
from .models import Book

class BookType(DjangoObjectType):
class Meta:
model = Book

class Query(graphene.ObjectType):
all_books = graphene.List(BookType)

def resolve_all_books(root, info):
return Book.objects.all()

schema = graphene.Schema(query=Query)

Step 5: Seed the Database

To test the GraphQL API, add some data to the database. Use the Django admin or the shell:

python manage.py shell

from api.models import Book
Book.objects.create(title="The Great Gatsby", author="F. Scott Fitzgerald")
Book.objects.create(title="1984", author="George Orwell")Step 6: Test Your GraphQL API

Start the development server:

python manage.py runserver

Visit http://127.0.0.1:8000/graphql/. You’ll see the GraphiQL interface, where you can write and test queries.

Try this query to fetch all books:

query {
allBooks {
title
author
}
}

You should see a response like this:

{
"data": {
"allBooks": [
{
"title": "The Great Gatsby",
"author": "F. Scott Fitzgerald"
},
{
"title": "1984",
"author": "George Orwell"
}
]
}
}

Conclusion

You’ve successfully set up a Django project with GraphQL! This basic setup can be extended to include mutations, authentication, and more advanced features. By combining Django’s robust backend capabilities with the flexibility of GraphQL, you can create APIs that are both powerful and user-friendly.

If you have questions or want to explore more about Django and GraphQL, let me know in the comments. Happy coding! 🚀

Tech With Big D
Tech With Big D

Written by Tech With Big D

Empowering Local Businesses Through Technology