graphql-python / graphql-python/graphene

[QUESTION] How to pass custom arguments from filter to custom connector?

Offen
#1,262 0 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen
🐛 bug
Vorherrschende Sprache
Python
Sterne
8.2k
Forks
818
PR-Merge-Kennzahlen
Keine gemergten PRs in 30 T.

Beschreibung

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)](https://stackoverflow.com/questions/60150033/graphene-django-access-filter-arguments-in-connection-class), 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:

```graphql
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:

```graphql
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:

```python
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:

```python
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.

Beitragsleitfaden

Für dieses Repository ist kein Beitragsleitfaden indexiert

Rechercherichtung

Start with the custom TransactionFilter, OrderedDjangoFilterConnectionField.resolve_queryset, and the custom connection class described in the issue. Compare the filter arguments available in resolve_queryset with those available to the connection or resolver, using the linked Stack Overflow question for context. Done means documenting or implementing a confirmed path for passing filter values to report generation.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
django, python
Bereich
api, backend
Issue-Typ
Feature
Schwierigkeit
5/5
Geschätzter Aufwand
Über eine Woche
Aktivitätsstatus
Veraltet
Klarheit
Muss geklärt werden
Anfängerfreundlichkeit
20/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.