Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions strawberry_django/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,19 @@ def resolve_value(value: Any) -> Any:
if isinstance(value, list):
return [resolve_value(v) for v in value]

# Handle strawberry.Maybe type if available
try:
from strawberry import Maybe

if isinstance(value, Maybe):
# Extract .value from Maybe, handling both present and None values
# Recursively resolve the extracted value in case it's nested
maybe_value = getattr(value, "value", None)
return resolve_value(maybe_value) if maybe_value is not None else None
except ImportError:
# Maybe type not available in this version of strawberry
pass

if isinstance(value, relay.GlobalID):
return value.node_id

Expand Down
37 changes: 37 additions & 0 deletions tests/filters/test_filters_v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,43 @@ def test_resolve_value(value, resolved):
assert resolve_value(value) == resolved


def test_resolve_value_maybe():
"""Test that strawberry.Maybe type is properly handled in resolve_value."""
try:
from strawberry import Maybe
except ImportError:
pytest.skip("strawberry.Maybe is not available in this version")
Comment on lines +147 to +150
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion: ditto


# Test Maybe with a value
maybe_with_value = Maybe(value="test_string")
assert resolve_value(maybe_with_value) == "test_string"
Comment on lines +153 to +154
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion: I think all of those can be parametrized on pyright. Will make the implementation cleaner


# Test Maybe with None
maybe_none = Maybe(value=None)
assert resolve_value(maybe_none) is None

# Test Maybe with nested types
maybe_enum = Maybe(value=Version.TWO)
assert resolve_value(maybe_enum) == Version.TWO.value

maybe_gid = Maybe(value=GlobalID("FruitNode", "42"))
assert resolve_value(maybe_gid) == "42"

# Test Maybe in a list
maybe_list = [
Maybe(value=1),
Maybe(value="test"),
Maybe(value=None),
Maybe(value=Version.ONE),
]
resolved_list = resolve_value(maybe_list)
assert resolved_list == [1, "test", None, Version.ONE.value]

# Test nested Maybe
nested_maybe = Maybe(value=Maybe(value="nested"))
assert resolve_value(nested_maybe) == "nested"
Comment on lines +181 to +183
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion (testing): Consider adding a test for lists containing nested Maybes.

Add a test with a list containing nested Maybe instances, such as [Maybe(value=Maybe(value="foo")), Maybe(value=None)], to verify resolve_value correctly handles nested Maybes.

Suggested change
# Test nested Maybe
nested_maybe = Maybe(value=Maybe(value="nested"))
assert resolve_value(nested_maybe) == "nested"
# Test nested Maybe
nested_maybe = Maybe(value=Maybe(value="nested"))
assert resolve_value(nested_maybe) == "nested"
# Test list containing nested Maybes
nested_maybe_list = [
Maybe(value=Maybe(value="foo")),
Maybe(value=None),
]
resolved_nested_list = resolve_value(nested_maybe_list)
assert resolved_nested_list == ["foo", None]



def test_filter_field_missing_prefix():
with pytest.raises(
MissingFieldArgumentError, match=r".*\"prefix\".*\"field_method\".*"
Expand Down