Skip to main content

Advanced filters for Graphene

Project description

Graphene-Django-Filter

CI PyPI version

This package contains advanced filters for graphene-django. The standard filtering feature in Graphene-Django relies on the Django-Filter library and therefore provides a flat API without the ability to use and and or expressions. This library makes the API nested and adds the and and or composition by extension of the DjangoFilterConnectionField field and the FilterSet class.

Requirements

  • Python (3.6, 3.7, 3.8, 3.9, 3.10)
  • Graphene-Django (2.15)

Features

Nested API with the ability to use and and or expressions

To use, simply replace all DjangoFilterConnectionField fields with AdvancedDjangoFilterConnectionField fields in your queries. Also, if you create custom FilterSets, replace the inheritance from the FilterSet class with the inheritance from the AdvancedFilterSet class. For example, the following task query exposes an old flat API.

import graphene
from django_filters import FilterSet
from graphene_django import DjangoObjectType
from graphene_django.filter import DjangoFilterConnectionField

class TaskFilter(FilterSet)
    class Meta:
        model = Task
        fields = {
            'name': ('exact', 'contains'),
            'user__email': ('exact', 'contains'),
            'user__last_name': ('exact', 'contains'),
        }
 
class UserType(DjangoObjectType):
    class Meta:
        model = User
        interfaces = (graphene.relay.Node,)
        fields = '__all__'
        
class TaskType(DjangoObjectType):
    user = graphene.Field(UserType)

    class Meta:
        model = Task
        interfaces = (graphene.relay.Node,)
        fields = '__all__'
        filterset_class = TaskFilter
        
class Query(graphene.ObjectType):
    tasks = DjangoFilterConnectionField(TaskType)

The flat API in which all filters are applied using the "and" operator looks like this.

{
  tasks(
    name_Contains: "important"
    user_Email_Contains: "john"
    user_LastName: "Dou"
  ){
    edges {
      node {
        id
        name
      }
    }
  }
}

After replacing the field class with the AdvancedDjangoFilterConnectionField and the FilterSet class with the AdvancedFilterSet the API becomes nested with support for and and or expressions.

from graphene_django_filter import AdvancedDjangoFilterConnectionField, AdvancedFilterSet

class TaskFilter(AdvancedFilterSet)
    class Meta:
        model = Task
        fields = {
            'name': ('exact', 'contains'),
            'user__email': ('exact', 'contains'),
            'user__last_name': ('exact', 'contains'),
        }

class Query(graphene.ObjectType):
    tasks = AdvancedDjangoFilterConnectionField(TaskType)

For example, the following query returns tasks whose names contain the word "important" or the user's email address contains the word "john" and the user's last name is "Dou". Note that the operators are applied to lookups such as contains, exact, etc. at the last level of nesting.

{
  tasks(
    filter: {
      or: [
        {name: {contains: "important"}}
        and: [
          {user: email: {contains: "john"}}
          {user: lastName: {exact: "Dou"}}
        ]
      ]
    }
  ){
    edges {
      node {
        id
        name
      }
    }
  }
}

The same result can be achieved with an alternative query structure because within the same object the and operator is always used.

{
  tasks(
    filter: {
      or: [
        {name: {contains: "important"}}
        {
          user: {
            email: {contains: "john"}
            lastName: {exact: "Dou"}
          }
        }
      ]
    }
  ){
    edges {
      node {
        id
        name
      }
    }
  }
}

The filter input type has the following structure.

input FilterInputType {
  and: [FilterInputType]
  or: [FilterInputType]
  ...FieldLookups
}

For more examples, see tests.

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

graphene-django-filter-0.4.0.tar.gz (9.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

graphene_django_filter-0.4.0-py3-none-any.whl (9.4 kB view details)

Uploaded Python 3

File details

Details for the file graphene-django-filter-0.4.0.tar.gz.

File metadata

  • Download URL: graphene-django-filter-0.4.0.tar.gz
  • Upload date:
  • Size: 9.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.62.3 importlib-metadata/4.10.1 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.9.10

File hashes

Hashes for graphene-django-filter-0.4.0.tar.gz
Algorithm Hash digest
SHA256 b3eab534becb74337bc26d52f698efe8ed9e55893543536f2ab0fca41f454531
MD5 bb619f51d37436ceaf5cf9eca0fe18f9
BLAKE2b-256 0537626df5f17101d5d048655abad68c9a8ea9ffae5a191f861beb827da350e9

See more details on using hashes here.

File details

Details for the file graphene_django_filter-0.4.0-py3-none-any.whl.

File metadata

  • Download URL: graphene_django_filter-0.4.0-py3-none-any.whl
  • Upload date:
  • Size: 9.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.62.3 importlib-metadata/4.10.1 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.9.10

File hashes

Hashes for graphene_django_filter-0.4.0-py3-none-any.whl
Algorithm Hash digest
SHA256 52470aaa23fcb7e30e48c30d2e8a1ff4c20e9270e18141f54be6d4459b346381
MD5 d25b8c4406433bc5dc61922b84f3ded3
BLAKE2b-256 152ea0036be3856692e613bfb6e0ca95eeb9d08a03b09ac9d5949d24494fa6ee

See more details on using hashes here.

Supported by

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