From a83b79fefca5381337a0d8c91da25a4580180d36 Mon Sep 17 00:00:00 2001 From: Guillaume Andreu Sabater Date: Mon, 2 Dec 2024 10:35:02 +0100 Subject: [PATCH] forward metadata when processing django type --- strawberry_django/type.py | 1 + tests/filters/test_filters_v2.py | 23 ++++++++++++++++++++++- 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/strawberry_django/type.py b/strawberry_django/type.py index 8d234a7e..dbbc461c 100644 --- a/strawberry_django/type.py +++ b/strawberry_django/type.py @@ -367,6 +367,7 @@ def _process_type( permission_classes=getattr(f, "permission_classes", ()), default=getattr(f, "default", dataclasses.MISSING), default_factory=getattr(f, "default_factory", dataclasses.MISSING), + metadata=getattr(f, "metadata", None), deprecation_reason=getattr(f, "deprecation_reason", None), directives=getattr(f, "directives", ()), pagination=getattr(f, "pagination", UNSET), diff --git a/tests/filters/test_filters_v2.py b/tests/filters/test_filters_v2.py index 71514908..6a6a92df 100644 --- a/tests/filters/test_filters_v2.py +++ b/tests/filters/test_filters_v2.py @@ -21,6 +21,7 @@ from strawberry_django.fields.filter_order import ( FilterOrderField, FilterOrderFieldResolver, + filter_field, ) from strawberry_django.filters import process_filters, resolve_value from tests import models, utils @@ -55,8 +56,8 @@ class FruitFilter: color_id: auto name: auto sweetness: auto - color: Optional[ColorFilter] types: Optional[FruitTypeFilter] + color: Optional[ColorFilter] = filter_field(filter_none=True) @strawberry_django.filter_field def types_number( @@ -420,6 +421,26 @@ def test_filter_distinct(query, db, fruits): assert len(result.data["fruits"]) == 1 +def test_filter_none(query, db): + yellow = models.Color.objects.create(name="yellow") + models.Fruit.objects.create(name="banana", color=yellow) + + f1 = models.Fruit.objects.create(name="unknown") + f2 = models.Fruit.objects.create(name="unknown2") + + result = query(""" + { + fruits(filters: {color: null}) { id } + } + """) + + assert not result.errors + assert result.data["fruits"] == [ + {"id": str(f1.id)}, + {"id": str(f2.id)}, + ] + + def test_empty_resolver_filter(): @strawberry.type class Query: