strawberry-django icon indicating copy to clipboard operation
strawberry-django copied to clipboard

Feature Request: Filters with custom names

Open hiporox opened this issue 2 years ago • 1 comments

Feature Request Type

  • [x] Alteration (enhancement/optimization) of existing feature(s)

Description

Filters currently require the Django relationship to be present in the name of the filter. For example, if you want to filter on a field called created_at, the filter name must be created_at. The proposal here to untie the name of the filter from the name in the database. So you could have the database field be named created_at, but the GraphQL filter be called created.

Where this feature would be most useful is in hiding many to many through relationships that you want to filter on. For example, if you want to provide a filter on task__assignments__assignee that will filter a task down to which user it is currently assigned to. In an ideal world, the TaskFilter would have an assignee field that you could filter on, even though that direct database relation does not exist.

The current work around for this is to define a filter_assignee method on the task filter, that then applies this filtering to the queryset. The issue is that filter methods are not nestable. If you have a project with multiple tasks, and you want to find all projects with a task that is assigned to a particularly user, it would be most natural to write the following query:

query projectsAssignedToUser($userId: ID) {
  projects(filters: { tasks: { assignee: { id: { exact: $userId } } } }) {
    ...
  }
}

Because nested method filters do not work, this filter syntax does not work.

Proposed API

I suggest we change the filter API definition to allow an optional value that can take override arguments for things like this. You would define a task filter like this:

@strawberry_django.filters.filter(Task)
class TaskFilter:
  id: FilterLookup[strawberry.id]
  assignee: UserFilter = strawberry_django.filters.filter_field(lookup="assignments__assginee")

Upvote & Fund

  • We're using Polar.sh so you can upvote and help fund this issue.
  • We receive the funding once the issue is completed & confirmed by you.
  • Thank you in advance for helping prioritize & fund our backlog.
Fund with Polar

hiporox avatar Jun 16 '22 19:06 hiporox

Hi @hiporox ,

I like this idea! Would you like to try to implement it and open a PR for us?

bellini666 avatar Jun 27 '22 16:06 bellini666

https://github.com/strawberry-graphql/strawberry-django/pull/478

bellini666 avatar Mar 18 '24 21:03 bellini666