GraphQL With Python (Django): A Complete Guide
•7 min read
- Languages, frameworks, tools, and trends

GraphQL is gaining popularity and many consider it a successor to REST APIs. If you're familiar with Python and Django, and want to get started with GraphQL or just to get a taste, this guide is definitively for you. You're going to learn more about how to use GraphQL with Python and Django to build a simple CRUD API for a notes app (like google keep). That tutorial assumes that you're already familiar with Python and Django.
This article will help in developing your knowledge and prepare you for any Django jobs interview!
What is GraphQL?
GraphQL is a data query and manipulation language for APIs, and a runtime for executing queries using a type system you define for your data.
It allows retrieving exactly what data we need in a nested, convenient, and highly flexible way. It also works with only one URL to perform all actions.
To have a glance, suppose you want to fetch an article and a paginated list of comments using REST, you will probably make two requests:
1. get to "/article/<article-id>" to retrieve the article:
2. get "/comments/<article-id>/?page=1" to retrieve comments:
But with GraphQL you can just do this
Now let's suppose you want to render only the preview of each article on some pages and the full content and comments on another page. You just can't do it using REST, you're always obliged to get all the data a REST API exposes to you.
Here is the beauty and flexibility of GraphQL. You could fetch exactly what you need:
And all that using the same URL.
You can also go as deep as you want to retrieve exactly what you need as data.
GraphQL vs REST: A deep comparison
Many developers believe that GraphQL will replace REST. Many others have already discovered that GraphQL helps solve some of the common challenges developers face when creating REST APIs.
However, deciding to opt for GraphQL comes with several considerations. GraphQL offers several advantages that make it a compelling option for API development.
Data fetching
One of the key benefits is its ability to overcome the limitations of REST about data fetching. In REST, it is common for clients to either receive more data than they need (overfetching) or not enough data (underfetching) because the only way to download data is by accessing pre-defined endpoints that return fixed data sets. GraphQL allows clients to specify the exact data they want in a single request, making it easier to design APIs that meet their specific needs.
Schema and type safety
GraphQL uses a type system to define the capabilities of an API. The types exposed in an API are defined in a schema using the GraphQL Schema Definition Language (SDL) and/or through code. This allows for strong typing and a clear understanding of the data that can be queried from the API.
Frontend teams can use a typed GraphQL API with confidence, knowing that if the backend team makes any changes to the API design, they will receive instant feedback when a query is raised from the frontend. This enables better collaboration and ensures that the frontend and backend are always in sync.
Tools like the GraphQL Code Generator can also automatically generate code for queries and mutations based on the GraphQL query files in the codebase, which speeds up development and reduces the risk of errors in production.
Fast product development
A common pattern with REST APIs is to structure the endpoints according to the different views in the app (e.g. /menu, /prices, /images, etc.). This is convenient because it allows the client to retrieve all the necessary data for a given view by accessing the corresponding endpoint. However, this approach can be inflexible and hinder rapid iteration. Every time the UI is changed, there is a risk that the data requirements will change, requiring the backend to be adjusted as well. This can be time-consuming and slow down product development.
In contrast, the flexible nature of GraphQL enables changes on the client side to be made without any extra work on the server. Since clients can specify their exact data requirements, no backend adjustments are needed when the frontend design or data needs changes.
Implementation of the schemas
One key difference between GraphQL and other API technologies is its ability to stitch multiple schemas into a single schema that can be accessed by the client. This allows for a more flexible and modular approach to API design and implementation. You can combine multiple independent schemas into a unified API without having to write custom code or make other complex changes. This simplifies the API development process and makes it easier to manage and maintain.
The main difference between GraphQL and REST APIs is that GraphQL is a specification and query language, while REST is an architectural concept for network-based software.
Now, let's put our hands to work using this theory!
GraphQL tutorial practice
First, let’s present what we call in graphQL jargon the 'schema'. It’s just the set of the different components of a GraphQL API:
- types that define the data we manipulate on our API.
- queries are used to fetch information about the types.
- mutations to update the types.
- subscriptions to listen to events or types changes, a bit like a WebSockets but we won't cover it here.
The common package to create a GraphQL API with Python and Django is graphene.
- Create and start a virtual environment
I'm going to use virtualenv but you can go with what you're comfortable with,
poetry or conda or whatever tool it is:
- Install django
- Create the django project
- create our working app
- Install graphene
- add "graphene_django" and "notes" app to "INSTALLED_APPS"
- add a GraphQL URL to the urls.py of your Django project:
"graphiql=True" is set to "True" because we want to use "graphiql" (an internal
web interface to test the GraphQL API).
- add Note and Category models
- create migrations
- run migrations
- to make it easy, load some fixtures.
Create this file "graphql_notes_api/notes/fixtures/notes.json"
- load the fixtures
Now we are moving to the interesting part. We are going to set up the schema:
- create "graphql_notes_api/schema.py" file and add the following code in it:
- define the GRAPHENE config in graphql_notes_api/settings.py:
And we are done, and no, I'm not joking, our graphQL API is ready :relieved:.
Let's test it out.
- Start the django server
- Go to "localhost:8000/graphql" to run our first query!
You should see an interface like that:

- Execute
You should have as output:
Nice, you have just created a working graphQL API!
- Querying relations
We can also query for relations. And that is also a power of graphQL.
For example, we can list all notes related to a given category like this:
And here should be the output:
- Now, let's add mutations to create and remove notes
Add the following code to "schema" file
Note that we defined the "Mutation" root class and added it the schema "mutation=Mutation" right after the query.
- We can test our mutation.
Go back on graphiql and run:
You should have:
Run again the query and you will notice that our new note has been created.
- Delete the note
Just run the following mutation
In our case, the newly created note has an Id of 5.
You should get:
Handle pagination and filtering with Graphene Relay
The last thing we want to show here is graphene relay.
Relay is a JavaScript framework designed to improve the performance, maintainability, and type safety of GraphQL data management. It was initially made for React applications. We want to show you one of the most important uses of relay for building API: pagination.
We could have handled the pagination manually by defining the page count and offset in the query and using it in the resolver, but that would have been less elegant as relay does it automatically and in a much cleaner way.
Let start:
- install django-filter
- Update the schema file
That's all. Let's test it:
- Run
You should have this output:
Note: The "id" present in each node is auto-generated by the relay and is unique across all objects.
Now we can specify a custom "id"
You'll have this output:
- And lastly, let's test the filtering capability.
Output:
Final congratulations, we are at the end of this guide.
Don't hesitate to learn more about GraphQL on the official documentation and start using it to build awesome projects.
Conclusion
GraphQL allows you to build any kind of API, and being more flexible than REST, it lets you fetch exactly what you want using the same URL. But it comes with the price of being a little more complex than REST. However, in this tutorial, we have shown how Python and Django makes it easier to implement GraphQL. We started by explaining what GraphQL is, then compared it to REST, and finally created a simple GraphQL API that covers core GraphQL topics like types, queries, mutations, relay using the Django Framework and graphene.

Author
Horace Folahan FAYOMI
Software engineer with more than 3 years of experience in FullStack web development and a technical writer in my spare time.I have relevant experience working on Python, Django, React, VueJS, GraphQL.