Skip to content
Merged
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
8 changes: 8 additions & 0 deletions strawberry/types/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -456,6 +456,14 @@ def is_implemented_by(self, root: type[WithStrawberryObjectDefinition]) -> bool:
):
return True

from strawberry.types.union import StrawberryUnion

if (
isinstance(expected_concrete_type, StrawberryUnion)
and real_concrete_type in expected_concrete_type.types
):
return True

if real_concrete_type is not expected_concrete_type:
return False

Expand Down
56 changes: 56 additions & 0 deletions tests/schema/test_generics.py
Original file line number Diff line number Diff line change
Expand Up @@ -1208,3 +1208,59 @@ def hello(self, info: strawberry.Info) -> Pagination[TestInterface] | TestError:

assert not query_result.errors
assert query_result.data == {"hello": {"items": [{"data": "test1"}]}}


def test_generic_with_union_typevar_in_outer_union():
T = TypeVar("T")

@strawberry.type
class Animal:
name: str

@strawberry.type
class Plant:
name: str

@strawberry.type
class Error:
message: str

@strawberry.type
class Collection(Generic[T]):
items: list[T]

@strawberry.type
class Query:
@strawberry.field
def things(self) -> Collection[Animal | Plant] | Error:
return Collection(items=[Animal(name="Dog"), Plant(name="Rose")])

@strawberry.field
def failing_things(self) -> Collection[Animal | Plant] | Error:
return Error(message="something went wrong")

schema = strawberry.Schema(query=Query)

result = schema.execute_sync(
textwrap.dedent("""
{
things {
... on AnimalPlantCollection {
items {
... on Animal { name }
... on Plant { name }
}
}
}
failingThings {
... on Error { message }
}
}
""")
)

assert not result.errors
assert result.data == {
"things": {"items": [{"name": "Dog"}, {"name": "Rose"}]},
"failingThings": {"message": "something went wrong"},
}
Loading