graphene
graphene copied to clipboard
[QUESTION] How to pass custom arguments from filter to custom connector?
First of all, i would like to apologize to post this question here (I know, that issues are for features and bug reports). I tried to find help on stackoverflow (original question), but no one helped.
My problem has roots in implementing reports in my API. I wanted to make report straight from executed query (with applied filters and ordering) and I figured out that custom connection class is the right way. So if someone wants to make from query like this report:
query{
transactions(
first: 10
fromDate: "2010-12-24"
toDate: "2020-12-24"
){
edges{
node{
amount
date
}
cursor
}
}
}
Only he needs to do is to add something like this:
query{
transactions(
first: 10
fromDate: "2010-12-24"
toDate: "2020-12-24"
){
pdfUrl
xlsxUrl
edges{
node{
amount
createdAt
}
cursor
}
}
}
And response will be url to generated report like this: \
-
"pdfUrl": "http:\\127.0.0.1:7000\\media\\pdf\\transactions\\bc288dd8-d3e3-48d8-aa34-a3b08e3c5967.pdf"
-
"xlsxUrl": "http:\\127.0.0.1:7000\\media\\xlsx\\transactions\\5dc3ac83-74cd-4e2f-9796-310849d06245.xlsx"
The real issue and problem happens, when I need to make two different types of reports to the same query. First is ok, there I need all transactions, but in second type, there I only need to sum amount of transactions before specified date, in specified date and at the end. So I had an idea, that when I will receive filter arguments: toDate
, fromDate
, I will filter transactions in this interval in filter class and in my connection class in resolve I will sum these values and add them to report. But connection class or resolve methods has no access to used filter arguments. Also I don't know if it's possible to pass somehow from my filter class some custom arguments to my connection class for example from qs
property:
class TransactionFilter(django_filters.FilterSet):
@property
def qs(self):
return super().qs
I was thinking about changing my custom DjangoFilterConnectionField
where i added cutom ordering, because as you see, there are filter_kwargs
and connection
(which is I hope connection class), but I have no idea how to accomplish something like that:
class OrderedDjangoFilterConnectionField(DjangoFilterConnectionField):
@classmethod
def resolve_queryset(cls, connection, iterable, info, args, filtering_args, filterset_class):
qs = super(DjangoFilterConnectionField, cls).resolve_queryset(connection, iterable, info, args)
filter_kwargs = {k: v for k, v in args.items() if k in filtering_args}
qs = filterset_class(data=filter_kwargs, queryset=qs, request=info.context).qs
order = args.get('orderBy', None)
if order:
if type(order) is str:
snake_order = to_snake_case(order)
else:
snake_order = [to_snake_case(o) for o in order]
qs = qs.order_by(*snake_order)
return qs
If you need more code snippets from my implementation, try to look into my original stackowerflow link or ask. I will be very glad to give you what you need to help me. Thank you guys.