• Stars
    star
    615
  • Rank 72,947 (Top 2 %)
  • Language
    Python
  • License
    MIT License
  • Created over 5 years ago
  • Updated 4 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

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

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

You can even rename your fields when querying data, In an example below the field location is renamed to address

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

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

Documentation 📝

You can do a lot with Django RESTQL apart from querying data, like

  • Rename fields
  • Restrict some fields on nested fields
  • Define self referencing nested fields
  • Optimize data fetching on nested fields
  • Data filtering and pagination by using query arguments
  • Data mutation(Create and update nested data of any level in a single request)

Full documentation for this project is available at https://yezyilomo.github.io/django-restql, you are advised to read it inorder to utilize this library to the fullest.

Django RESTQL Play Ground

Django RESTQL Play Ground is a graphical, interactive, in-browser tool which you can use to test Django RESTQL features like data querying, mutations etc to get the idea of how the library works before installing it. It's more like a live demo for Django RESTQL, it's available at https://django-restql-playground.yezyilomo.me

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

More Repositories

1

odoo-rest-api

Module which expose Odoo as a REST API
Python
196
star
2

state-pool

Transform your React app with our state management library! Declare global and local states like variables, powered by the magic of React hooks 🪄✨
TypeScript
138
star
3

dictfier

Python library to convert/serialize class instances(Objects) both flat and nested into a dictionary data structure. It's very useful in converting Python Objects into JSON format
Python
76
star
4

drf-pretty-update

Collection of simple and flexible model serializer and fields for Django REST Framework which allows you to create/update your models with related nested data.
Python
27
star
5

drf-guard

Create flexible and simple to use access rules for Django REST Framework(DRF).
Python
7
star
6

mpay

Odoo application for online payment through mobile SMS based services in eCommerce.
Python
7
star
7

simple-react-state

React state manager based on react hooks and react-redux.
JavaScript
5
star
8

state-pool-examples

State pool examples
JavaScript
3
star
9

github-auto-deployer

Automated GitHub Deployment using Webhooks
JavaScript
3
star
10

dorm

Decent Object Relational Mapper(dorm) tool for MySQL database in flask web framework
Python
2
star
11

last-card-game

Last Card Game
2
star
12

state-pool-ssr

SSR demo for state-pool
JavaScript
2
star
13

state-pool-docs

Documentation for state-pool
JavaScript
1
star
14

weather-app

Web app based on React for searching weather of different places
JavaScript
1
star
15

marathon

Marathon API
Python
1
star
16

yezyilomo

My GitHub profile
1
star
17

django-restql-playground-frontend

Front-end for django-restql playground
JavaScript
1
star
18

visual-alarm

Visual alarm
Python
1
star
19

django-restql-playground-backend

Live demo for people to experiment with django-restql
Python
1
star