Hello! I tried to create a type inheriting a generic type with a union type applied and caught a TypeError: Response fields cannot be resolved.
There's a code fragment the bug can be reproduced with:
from typing import Annotated, Generic, TypeVar, Union
import strawberry
T = TypeVar("T")
@strawberry.type
class User:
name: str
age: int
@strawberry.type
class ProUser:
name: str
age: float
@strawberry.type
class GenType(Generic[T]):
data: T
GeneralUser = Annotated[Union[User, ProUser], strawberry.union("GeneralUser")]
@strawberry.type
class Response(GenType[GeneralUser]):
...
@strawberry.type
class Query:
@strawberry.field
def user(self) -> Response:
return Response(data=User(age=1, name="John"))
schema = strawberry.Schema(query=Query)
This code raises the following exception: TypeError: Response fields cannot be resolved. Unexpected type 'typing.Annotated[typing.Union[__main__.User, __main__.ProUser], <strawberry.union.StrawberryUnion object at 0x14f8a90>]'
But if you replace GeneralUser = Annotated[Union[User, ProUser], strawberry.union("GeneralUser")]
with GeneralUser = strawberry.union("GeneralUser", (User, ProUser))
the code works as expected and doesn't raise any exception.
It looks like union types cannot be applied to generic types if they're declared with Annotated
, because this bug isn't reproducible in case the old-style approach with strawberry.union
is used.
Pay now to fund the work behind this issue.
Get updates on progress being made.
Maintainer is rewarded once the issue is completed.
You're funding impactful open source efforts
You want to contribute to this effort
You want to get funding like this too