Query your Django REST API like a GraphQL API with Django RESTQL

Query your Django REST API like a GraphQL API with Django RESTQL

The hype around GraphQL in recent years has been hard to ignore for any web developer. In short, GraphQL is a query language and runtime for APIs, and it has taken the web with storm.

GraphQL makes it possible for front-end developers to query data from a single API endpoint and retrieving exactly the data they need.

There are a few benefits with this approach, one being the performance increases, while at the same time using less server resource. Another big benefit is that the hurdles presented in the development process between backend and front-end teams are somewhat eliminated.

No longer do the front-end teams have to wait for the backend team to produce new endpoints for new use-cases.

It's easy to see the upside, and why the technology has seen so much adoption!

However, if you have invested countless hours learning RESTful API design, the switch to a new technology might not make sense.

If you are building a REST API with Django and Django REST Framework there is a solution that might work well as middle ground between the GraphQL and REST paradigm.

The solution is called Django RESTQL, a Django package developed yezyilomo. Django RESTQL let's you create API endpoints with Django REST Framework that can be queried in a similar fashion to that of GraphQL.

I stumbled upon the package a few months ago and was immediately intrigued, the same way I was the first time I saw GraphQL.

The main benefit of using Django RESTQL is that you can rely more on Django REST Frameworks ModelViewSet and build larger Serializers that can be used for more use-cases. Without impacting performance as they can be queried to your need.

Let's have a look at how we can Install and use Django RESTQL with Django REST Framework.

1. Install Django RESTQL

pip install django-restql

Django RESTQL requires at least the following version of Python, Django and Django REST Framework

  • Python >= 3.5
  • Django >= 1.11
  • Django REST Framework >= 3.5

2. Setup your Serializer with DynamicMixinField

To be able to query data with django-restql we need to assosiate our serializer with the DynamicMixinField.

from rest_framework import serializers
from django.contrib.auth.models import User
from django_restql.mixins import DynamicFieldsMixin


class RestaurantSerializer(DynamicFieldsMixin, serializer.ModelSerializer):
    class Meta:
        model = User
        fields = ['id', 'name', 'location' 'business_hours']

That's how simple it is to start working with django-restql!

As long as you have your urls, models and ViewSets already written you can now query your data in the following fashion..

GET /restaurants/?query={id, name}

A GET request like the one above would result in a response with restaurants where name and id is available, leaving out the business hours all together.

GraphQL has introduced more features than just being able to query data from a single endpoint. A big part of GraphQL is the ability to mutate data through the very same endpoint.

Django RESTQL provides the same type functionality out-of-the-box, which we will take a closer look at in the next tutorial when we dig deeper into this unique django package.

Freddie Freddie 4 years, 2 months ago 0
Login to Comment
No comments have been posted yet, be the first one to comment.
How to Cache Django REST Framework with Redis
How to Cache Django REST Framework with Redis
Django REST Framework is an awesome package that will aid you in writing REST APIs faster with Django and Python. Though Django REST Framework has many strengths, performance out-of-the-box might not be one of them. However, there are many ways to fix that, and one of them is caching....
How to Rebuild Django-MPTT Tree Structure
How to Rebuild Django-MPTT Tree Structure
Most application utilize some sort of tree structure to managing data, in one form or another. One common use-case is nested categories. If you are using Django for your current project and are in need to implement tree structures, there is a big change you have come across Django-MPT...
How to Build a Movie Database & API with Strapi
How to Build a Movie Database & API with Strapi
Strapi is an awesome headless CMS built with Node.js that can speed up the process of building an API quiet dramatically. It's perfect for people who enjoy the frontend more than the backend, and it allows you to build complex database structures with out writing any code. Magic, if y...