graphql-python / graphql-python/graphene

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

Ouverte
#1,262 0 commentaires 0 réactions 0 personnes assignées Voir sur GitHub
🐛 bug
Langage dominant
Python
Étoiles
8.2k
Forks
818
Métriques de merge des PR
Aucune PR mergée en 30 j

Description

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.

Guide de contribution

Aucun guide de contribution indexé pour ce dépôt

Piste de recherche

Commencez par le TransactionFilter personnalisé, OrderedDjangoFilterConnectionField.resolve_queryset et la custom connection class décrits dans l’issue. Comparez les arguments de filtre disponibles dans resolve_queryset avec ceux disponibles pour la connection ou le resolver, en utilisant la question Stack Overflow liée comme contexte. Le travail est considéré comme terminé lorsqu’une méthode confirmée permettant de transmettre les valeurs de filtre à la génération de rapports est documentée ou implémentée.

Rédigé par le modèle d'indexation à partir du texte de l'issue.

Évaluation

Stack technique
django, python
Domaine
api, backend
Type d'issue
Fonctionnalité
Difficulté
5/5
Temps estimé
Plus d'une semaine
Activité
À l'abandon
Clarté
À clarifier
Accessibilité débutants
20/100

Recevez les nouvelles issues par e-mail

Un résumé court des issues GitHub adaptées aux débutants.