DEV Community

Robert Matyszewski
Robert Matyszewski

Posted on • Updated on • Originally published at blog.graphqleditor.com

Turn Django REST API into a GraphQL like API

Web APIs are the motors that power most of our nowadays apps. For many years REST has been the principal architect for APIs, but in this article, we will investigate Django to GraphQL library!

With REST APIs, you were regularly creating URLs for every object of data that's available. Thinking about REST API example for books - we'll have URLs for the books themselves, authors, prizes, characters and heroes ... it's already a lot! This could involve a lot of requests.

With GraphQL you have one endpoint, ask what you want and get exactly that. It's is the top wanted technology according to The state of JS 2018 and every week community creates something interesting to help users adopt into new technology. Yezyilomo came with the idea of python library that allows to turn Django Rest Framework into GraphQL like API. Let's explore it more.

Django-restql is a library which turn your API made with Django REST Framework(DRF) into a GraphQL like API. With django-restql you will be able to:

  • Send a query to your API and get precisely what you need, nothing extra and nothing less.
  • Get expected results, since you control what you get from the server.
  • Control the data you get, not the server.
  • Save the load of fetching unused data from the server.

The library works by choosing dynamically a subset of fields per DRF resource as specified by the request's query parameter.

GitHub logo yezyilomo / django-restql

Turn your API made with Django REST Framework(DRF) into a GraphQL like API.

Django RESTQL

Build Status Latest Version Python Versions License        Downloads Downloads Downloads

Django RESTQL is a python library which allows you to turn your API made with Django REST Framework(DRF) into a GraphQL like API. With Django RESTQL you will be able to

  • Send a query to your API and get exactly what you need, nothing more and nothing less.

  • Control the data you get, not the server.

  • Get predictable results, since you control what you get from the server.

  • Get nested resources in a single request.

  • Avoid Over-fetching and Under-fetching of data.

  • Write(create & update) nested data of any level in a single request.

Isn't it cool?.

Want to see how this library is making all that possible?

Check out the full documentation at https://yezyilomo.github.io/django-restql

Or try a live demo on Django RESTQL Playground

Requirements

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

Installing

pip install django-restql
Enter fullscreen mode Exit fullscreen mode

Getting Started

Using Django RESTQL to query…

Follow Yezyilomo to get in touch with him!

Installing

pip install django-restql
Enter fullscreen mode Exit fullscreen mode

Getting Started

Using django-restql is very simple, you just have to use the DynamicFieldsMixin when defining a View.

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

class UserViewSet(DynamicFieldsMixin, viewsets.ModelViewSet):
    queryset = User.objects.all().order_by('-date_joined')
    serializer_class = UserSerializer
Enter fullscreen mode Exit fullscreen mode

A regular request returns all fields specified on DRF serializer, in fact django-restql doesn't handle this request at all:

GET /users

    [
      {
        "id": 1,
        "username": "yezyilomo",
        "email": "yezileliilomo@hotmail.com",
        "groups": [1,2]
      },
      ...
    ]
Enter fullscreen mode Exit fullscreen mode

django-restql handle all GET requests with query parameter, this parameter is the one used to pass all fields to be included on a response. For example to select id and username fields from user model, send a request with a query parameter as shown below.

GET /users/?query=[["id", "username"]]

    [
      {
        "id": 1,
        "username": "yezyilomo"
      },
      ...
    ]
Enter fullscreen mode Exit fullscreen mode

If a query contains nested field, django-restql will return its id or array of ids for the case of nested iterable field(one2many or many2many). For example on a request below location is a flat nested field(many2one) and groups is an iterable nested field(one2many or many2many).

GET /users/?query=[["id", "username", "location", "groups"]]

    [
      {
        "id": 1,
        "username": "yezyilomo",
        "location": 6,
        "groups": [1,2]
      },
      ...
    ]
Enter fullscreen mode Exit fullscreen mode

django-restql support querying both flat and nested resources, so you can expand or query nested fields at any level as long as your field is defined as nested field on a serializer. For example you can query a country and region field from location.

GET /users/?query=[["id", "username", {"location": ["country", "region"]}]]

    [
      {
        "id": 1,
        "username": "yezyilomo",
        "location": {
            "contry": "Tanzania",
            "region": "Dar es salaam"
        }
      },
      ...
    ]
Enter fullscreen mode Exit fullscreen mode

django-restql got your back on expanding or querying iterable nested fields too. For example if you want to expand groups field into id and name, here is how you would do it.

GET /users/?query=[["id", "username" {"groups": [[ "id", "name" ]]}]]

    [
      {
        "id": 1,
        "username": "yezyilomo",
        "groups": [
            {
                "id": 2,
                "name": "Auth_User"
            }
            {
                "id": 3,
                "name": "Admin_User"
            }
        ]
      },
      ...
    ]
Enter fullscreen mode Exit fullscreen mode

Top comments (1)

Collapse
 
bhupesh profile image
Bhupesh Varshney 👾

Finally I found it
Thank you sooo much