Advanced filters for Graphene
Project description
Graphene-Django-Filter
This package contains advanced filters for graphene-django. The standard filtering feature in graphene-django relies on the django-filter library and therefore provides the flat API without the ability to use logical operators such as and, or and not. This library makes the API nested and adds logical expressions 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 logical operators
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 the 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__first_name': ('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_FirstName: "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 logical expressions.
from graphene_django_filter import AdvancedDjangoFilterConnectionField, AdvancedFilterSet
class TaskFilter(AdvancedFilterSet)
class Meta:
model = Task
fields = {
'name': ('exact', 'contains'),
'user__email': ('exact', 'contains'),
'user__first_name': ('exact', 'contains'),
'user__last_name': ('exact', 'contains'),
}
class Query(graphene.ObjectType):
tasks = AdvancedDjangoFilterConnectionField(TaskType)
For example, the following query returns tasks which names contain the word "important" or the user's email address contains the word "john" and the user's last name is "Dou" and the first name is not "John". 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"}}}
]
]
not: {
{user: {firstName: {exact: "John"}}}
}
}
){
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"}
}
}
]
not: {
{user: {firstName: {exact: "John"}}}
}
}
){
edges {
node {
id
name
}
}
}
}
The filter input type has the following structure.
input FilterInputType {
and: [FilterInputType]
or: [FilterInputType]
not: FilterInputType
...FieldLookups
}
For more examples, see tests.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file graphene-django-filter-0.5.0.tar.gz.
File metadata
- Download URL: graphene-django-filter-0.5.0.tar.gz
- Upload date:
- Size: 10.0 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.11.0 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.9.10
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6d27d7d63347a67d0e0a1d68619dba638570b156158a7cb8befc04f74cb69811
|
|
| MD5 |
037505726998f6f9a6109d47fa937d79
|
|
| BLAKE2b-256 |
6033d4f221fc48c3d3ae9335fe7e9c6afe399f836c7c004860a33d78e35bb341
|
File details
Details for the file graphene_django_filter-0.5.0-py3-none-any.whl.
File metadata
- Download URL: graphene_django_filter-0.5.0-py3-none-any.whl
- Upload date:
- Size: 10.2 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.11.0 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.9.10
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b804049be25d002f1692cbad29c3d14501c07d0dec9dc6ad766e0c9d84ac7988
|
|
| MD5 |
e9c50e43bd04dd74dd048cf3123a2f66
|
|
| BLAKE2b-256 |
edff005c78544da104574d049eb6efcc65d5e75aacb1da93562aeb99c3bb766a
|