Skip to main content

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

Project description

django-restql

Build Status Latest Version Python Versions License

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 this 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.

  • Save the load of fetching unused data from the server.

Isn't it cool?.

Installing

pip install django-restql

Getting Started

Using django-restql is very simple, you just have to inherit the DynamicFieldsMixin class when defining a serializer.

from rest_framework import serializers
from django.contrib.auth.models import User

from django_restql.mixins import DynamicFieldsMixin

class UserSerializer(DynamicFieldsMixin, serializer.ModelSerializer):
    class Meta:
        model = User
        fields = ['id', 'username', 'email', 'groups']

A regular request returns all fields as 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]
      },
      ...
    ]

django-restql handle all GET requests with query parameter, this parameter is the one used to pass all fields to be included in 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"
      },
      ...
    ]

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"
        }
      },
      ...
    ]

django-restql got your back on querying iterable nested fields(one2many or many2many) 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"
            }
        ]
      },
      ...
    ]

If a query contains nested field without expanding and it's not defined as a nested field on a serializer, 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, group}

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

Customizing django-restql

django-restql is very configurable, here is what you can customize on it.

  • Change the name of query parameter.

    If you don't want to use the name query as your parameter, you can inherit DynamicFieldsMixin and change it as shown below

    from django_restql.mixins import DynamicFieldsMixin
    
    class MyDynamicFieldMixin(DynamicFieldsMixin):
        query_param_name = "your_favourite_name"
    

    Now you can use this Mixin on your serializer and use the name your_favourite_name as your parameter. E.g

    GET /users/?your_favourite_name={id, username}

  • Customize how fields to include in a response are filtered. You can do this by inheriting DynamicFieldsMixin and override field methods as shown below.

    from django_restql.mixins import DynamicFieldsMixin
    
    class CustomDynamicFieldMixin(DynamicFieldsMixin):
        @property
        def fields(self):
            # Your customization here
            return fields
    

    Note: To be able to do this you must understand how django-restql is implemented, specifically DynamicFieldsMixin class, you can check it here. In fact this is how django-restql is implemented(just by overriding field method of a serializer, nothing more and nothing less).

Running Tests

python setup.py test

Credits

  • Implementation of this library is based on the idea behind GraphQL.
  • My intention is to extend the capability of drf-dynamic-fields library to support more functionalities like allowing to query nested fields both flat and iterable at any level, while maintaining simplicity.

Contributing PRs Welcome

We welcome all contributions. Please read our CONTRIBUTING.md first. You can submit any ideas as pull requests or as GitHub issues. If you'd like to improve code, check out the Code Style Guide and have a good time!.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django-restql-0.4.2.tar.gz (5.5 kB view details)

Uploaded Source

Built Distribution

django_restql-0.4.2-py3-none-any.whl (6.7 kB view details)

Uploaded Python 3

File details

Details for the file django-restql-0.4.2.tar.gz.

File metadata

  • Download URL: django-restql-0.4.2.tar.gz
  • Upload date:
  • Size: 5.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.11.1 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.6.8

File hashes

Hashes for django-restql-0.4.2.tar.gz
Algorithm Hash digest
SHA256 5e7af04d2977223259a480885f485f024ecfa601d3ba10c682a8c086d11f590d
MD5 3244a21ea318512094b046e37ebd2941
BLAKE2b-256 19dd1a10b3ed883be4abff3b44b3e3d62ab6a314f78e5ec1ad37dd0810a11276

See more details on using hashes here.

File details

Details for the file django_restql-0.4.2-py3-none-any.whl.

File metadata

  • Download URL: django_restql-0.4.2-py3-none-any.whl
  • Upload date:
  • Size: 6.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.11.1 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.6.8

File hashes

Hashes for django_restql-0.4.2-py3-none-any.whl
Algorithm Hash digest
SHA256 f53ebc7e38a8d9ce0c9885a6ca9c54ec76918fee4e32452c047e31251209c6a3
MD5 69c2bc65f37a717e2bbb93031aea3383
BLAKE2b-256 3288cc4b37e90ba2a7b11d5003c2c50f2c4732f4d2070af192695adc43958829

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page