I am using graphene-python
, django-filters
and relay
in my GraphQL API. Let's imagine I have a type FrameType
which has an integer field time_offset
and I would like to be able to use a range on it - ask only for frames which have the time_offset
within the given range. I prepared my schema.py
according to the graphene-python docs with a custom FilterSet
:
import django_filters
from graphene import ObjectType, relay
from graphene_django import DjangoObjectType, filter
from my_app.blabla import models
class FrameFilter(django_filters.FilterSet):
time_offset = django_filters.RangeFilter()
class Meta:
model = models.Frame
fields = ("time_offset",)
class FrameType(DjangoObjectType):
class Meta:
model = models.Frame
filterset_class = FrameFilter
interfaces = (relay.Node,)
class Query(ObjectType):
frames = filter.DjangoFilterConnectionField(FrameType)
class Meta:
abstract = True
However, I have no idea how to query the timeOffset
field now. I found no examples online for the django_filters.RangeFilter
field. This is a query I tried:
query Frame {
frames(first: 20, timeOffset: "{\"gt\":\"4350\", \"lt\":\"5000\"}") {
edges {
node {
timeOffset
}
}
}
... also with these alternatives:
timeOffset: "{\"gt\":4350, \"lt\":5000}"
timeOffset: "{\"start\":\"4350\", \"end\":\"5000\"}"
timeOffset: "{\"min\":\"4350\", \"max\": \"4500\"}"
timeOffset: "[\"4350\", \"5000\"]"
timeOffset: "[4350, 5000]"
timeOffset: "[4350]"
timeOffset: "4350,5000"
These queries don't raise any error, but they don't filter either (all results are returned). I am lost, I'm not sure if I still haven't found the proper syntax, or maybe there's some mistake in my backend code. How should I use and query the django_filters.RangeFilter
on a field?
Sadly, this isn't possible. But, there is a workaround for it
Adjust your filter class as
Now query the schema with
Reference
Filter.method
--(django-filter
doc)