Skip to content

Redundant resolver with unused arguments when filtering with prefetch #863

@rcybulski1122012

Description

@rcybulski1122012

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

  • Core functionality
  • Alteration (enhancement/optimization) of existing feature(s)
  • New behavior

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions