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        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 with flexibility.

Isn't it cool?.

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

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

Requirements

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

Installing

pip install django-restql

Getting Started

Using Django RESTQL to query data is very simple, you just have to inherit the DynamicFieldsMixin class when defining a serializer that's all.

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']

Django RESTQL handle all requests with a query parameter, this parameter is the one used to pass all fields to be included/excluded 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 defined on a serializer. In an example below we have location as a nested field on User model.

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

from app.models import GroupSerializer, LocationSerializer


class LocationSerializer(DynamicFieldsMixin, serializer.ModelSerializer):
    class Meta:
        model = Location
        fields = ['id', 'country',  'city', 'street']


class UserSerializer(DynamicFieldsMixin, serializer.ModelSerializer):
    location = LocationSerializer(many=False, read_only=True) 
    class Meta:
        model = User
        fields = ['id', 'username', 'email', 'location']

If you want only country and city fields on a location field when retrieving users here is how you can do it

GET /users/?query={id, username, location{country, city}}

[
    {
        "id": 1,
        "username": "yezyilomo",
        "location": {
            "contry": "Tanzania",
            "city": "Dar es salaam"
        }
    },
    ...
]

Documentation :pencil:

You can do a lot with Django RESTQL apart from just querying data, full documentation for this project is available at https://django-restql.yezyilomo.com, you are encouraged to read it inorder to utilize this library to the fullest.

Running Tests

python runtests.py

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 and allow writing on nested fields 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.9.0.tar.gz (14.6 kB view details)

Uploaded Source

Built Distribution

django_restql-0.9.0-py3-none-any.whl (17.1 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for django-restql-0.9.0.tar.gz
Algorithm Hash digest
SHA256 5a91996d9cdb6cd6e060aaa9d7defe6c06375bb30ae3aba4e6f12068bbfc6e86
MD5 b5d32724862ff4aacf85fb49a2acbb91
BLAKE2b-256 d612a4028118990f83fb21036f3a2988386551c5c449f069cbcc5ac083d42bfe

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for django_restql-0.9.0-py3-none-any.whl
Algorithm Hash digest
SHA256 677174b95951c692cbd0a4b866eb277ad9c6884079400aa3af7ab4e32cea34d5
MD5 6fcc14a970d5d376f12f860601fc82ca
BLAKE2b-256 e3963e3b2f417cc81d4ae8867bd03da185c62a16405e4af0ef2751995362f03e

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