strawberry-graphql / strawberry-django

Strawberry GraphQL Django extension
https://strawberry.rocks/docs/django
MIT License
408 stars 118 forks source link

Feature: add new q filter interface #451

Closed OdysseyJ closed 6 months ago

OdysseyJ commented 8 months ago

Summary

Add new custom filtering Interface related issue #399

@strawberry_django.filters.filter(models.Fruit)
class QFilter:
    search: Optional[str]

    def q_search(self, value: str) -> Q:
        return Q(name=value)

usage.

### Fruits = ["strawberry", "raspberry", "banana"]
def test_q_filter_method_with_nested_not_filter(query, fruits):
    result = query(
        '{ fruits: qFilter(filters: { NOT : { search: "strawberry" }}) { id name } }'
    )
    assert result.data["fruits"] == [
        {"id": "2", "name": "raspberry"},
        {"id": "3", "name": "banana"},
    ]

Description

In my project, i implement many custom filters. Now, custom filters not working with NOT, AND, OR This change allow user using custom filter with NOT, AND, OR and user don't have to change custom filter_{} method.

Q() style filters are used in other projects also. https://django-ninja.dev/guides/input/filtering/

Types of Changes

Issues Fixed or Closed by This PR

*

Checklist

codecov-commenter commented 8 months ago

Codecov Report

All modified and coverable lines are covered by tests :white_check_mark:

Comparison is base (433ceeb) 87.73% compared to head (d4006e8) 87.74%. Report is 8 commits behind head on main.

Additional details and impacted files ```diff @@ Coverage Diff @@ ## main #451 +/- ## ========================================== + Coverage 87.73% 87.74% +0.01% ========================================== Files 37 37 Lines 3130 3150 +20 ========================================== + Hits 2746 2764 +18 - Misses 384 386 +2 ```

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.

bellini666 commented 8 months ago

Hey @OdysseyJ ,

This seems nice! Can we have a small example in the docs, similar to the custom filter ones?

OdysseyJ commented 8 months ago

Hi @bellini666 Thanks for reading my PR. Sure. I will do it.

OdysseyJ commented 8 months ago

@bellini666 finished.