There are cases, where I need to implement a resolver that returns related objects filtered based on user input. Currently all the filtering logic is implemented "inside" prefetch_related and the resolver only returns the prefetched fields, but it has to define arguments, which are unused by resolver.
Example:
@strawberry_django.field(
prefetch_related=lambda info: Prefetch(
"some_related_objects",
queryset=optimize(
SomeRelatedObject.objects.filter(
**(
{"is_confirmed": is_confirmed}
if isinstance(is_confirmed := get_field_args(info).get("isConfirmed"), bool)
else {}
),
period__contains=get_field_args(info).get("date", timezone.now().date()),
),
info,
),
to_attr="prefetched_some_related_objects",
)
)
@staticmethod
def some_related_objects(
root: User, date: date | None = UNSET, is_confirmed: bool | None = UNSET
) -> list[SomeRelatedObjectType]:
return root.prefetched_some_related_objects
The get_field_args function I use here, returns a dictionary of field arguments - related issue.
I'm not sure how this problem should be solved (maybe a different kind of decorator that is specific for such cases, instead of taking lambda as an argument of prefetch_related). I'm not even sure that's the proper way of solving this type of problems 😅 - as I mentioned in a different issue, more specific optimizer documentation would be appreciated 🙏
Feature Request Type
There are cases, where I need to implement a resolver that returns related objects filtered based on user input. Currently all the filtering logic is implemented "inside"
prefetch_relatedand the resolver only returns the prefetched fields, but it has to define arguments, which are unused by resolver.Example:
The
get_field_argsfunction I use here, returns a dictionary of field arguments - related issue.I'm not sure how this problem should be solved (maybe a different kind of decorator that is specific for such cases, instead of taking lambda as an argument of
prefetch_related). I'm not even sure that's the proper way of solving this type of problems 😅 - as I mentioned in a different issue, more specific optimizer documentation would be appreciated 🙏Feature Request Type