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

Django Django
Django REST Framework Django REST Framework

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

  1. Python >= 3.5
  2. Django >= 1.11
  3. 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.

Did You Enjoy the Tutorial?

Consider sending some love to Freddie the creator.

Comments (0)

You need to be logged in to comment..

Topics Mentioned in this Tutorial

Django is a web application framework written in Python. The framework was crafted by developers for developers to make it easier to build better web applications with less code, Hence  Django's slogan "The web framework for perfectionists with deadlines. Besides being a framework for developers that want to ship to production, Django also sports one of the, if not the best documentation found for any web application framework....
Django REST Framework
Django REST Framework is a powerful package for building REST API's with the Django Web Framework and Python. Django REST Framework comes with a browsable API out of the box, a feature that improves the developer experience dramatically. If you have developed applications with Django before, you will feel right at home as Django REST Framework is model after Django. You can query your database with Django's ORM and that you have come accustomed to, and with ease serialize that data as JSON....
Django RESTQL is a Python library made for Django, the library turns your Django REST Framework powered REST API into a GraphQL like API. With Django RESTQL you can send queries to your API enpoints and get back only the data you need, hence you are controling the data you get and not the server. The package also makes it easy to work with nested resources where everything can be created or updated (mutated) from the very same endpoint....
REST is a an architecture style for designing networked applications following a RESTful pattern. REST API's facilitates the connection between backend and frontend in many of todays applications....