Strawberry's automatic type-naming for generics is ignoring annotated types on unions. Here are a few examples of what I've tried:
This causes the union type to be named SomeTypeNotFoundError
:
from typing import Annotated, Generic, TypeVar, Union
import strawberry
from my_types import OtherType, SomeType
@strawberry.type
class NotFoundError:
id: strawberry.ID
message: str
T = TypeVar('T')
@strawberry.type
class ObjectQueries(Generic[T]):
...
@strawberry.field
def by_id(self, id: strawberry.ID) -> Annotated[Union[T, NotFoundError], strawberry.union("ByIdResult")]:
...
...
@strawberry.type
class Query:
@strawberry.field
def some_type_queries(self, id: strawberry.ID) -> ObjectQueries[SomeType]:
return ObjectQueries(SomeType)
@strawberry.field
def other_type_queries(self, id: strawberry.ID) -> ObjectQueries[OtherType]:
return ObjectQueries(OtherType)
schema = strawberry.Schema(Query)
This causes TypeError: SomeTypeObjectQueries fields cannot be resolved.
:
from typing import Annotated, Generic, TypeVar, Union
import strawberry
from my_types import OtherType, SomeType
@strawberry.type
class NotFoundError:
id: strawberry.ID
message: str
T = TypeVar('T')
@strawberry.type
class ObjectQueries(Generic[T]):
...
@strawberry.field
# NOTE annotaion moved to just the union
def by_id(self, id: strawberry.ID) -> Union[T, Annotated[NotFoundError, strawberry.union("ByIdResult")]]:
...
...
@strawberry.type
class Query:
@strawberry.field
def some_type_queries(self, id: strawberry.ID) -> ObjectQueries[SomeType]:
return ObjectQueries(SomeType)
@strawberry.field
def other_type_queries(self, id: strawberry.ID) -> ObjectQueries[OtherType]:
return ObjectQueries(OtherType)
schema = strawberry.Schema(Query)
Pulling the definition up has the same behaviour in both cases:
from typing import Annotated, Generic, TypeVar, Union
import strawberry
from my_types import OtherType, SomeType
@strawberry.type
class NotFoundError:
id: strawberry.ID
message: str
T = TypeVar('T')
ByIdResult = Annotated[Union[T, NotFoundError], strawberry.union("ByIdResult", (T, NotFoundError))]
# NOTE this does the same thing, but with a linter warning:
# ByIdResult = strawberry.union("ByIdResult", (T, NotFoundError))
@strawberry.type
class ObjectQueries(Generic[T]):
...
@strawberry.field
# NOTE annotaion moved to just the union
def by_id(self, id: strawberry.ID) -> ByIdResult:
...
...
@strawberry.type
class Query:
@strawberry.field
def some_type_queries(self, id: strawberry.ID) -> ObjectQueries[SomeType]:
return ObjectQueries(SomeType)
@strawberry.field
def other_type_queries(self, id: strawberry.ID) -> ObjectQueries[OtherType]:
return ObjectQueries(OtherType)
schema = strawberry.Schema(Query)
Specifying T in the union doesn't help
from typing import Annotated, Generic, TypeVar, Union
import strawberry
from my_types import OtherType, SomeType
@strawberry.type
class NotFoundError:
id: strawberry.ID
message: str
T = TypeVar('T')
ByIdResult = Union[T, Annotated[NotFoundError, strawberry.union("ByIdResult")]]
@strawberry.type
class ObjectQueries(Generic[T]):
...
@strawberry.field
# NOTE annotaion moved to just the union
def by_id(self, id: strawberry.ID) -> ByIdResult[T]:
...
...
@strawberry.type
class Query:
@strawberry.field
def some_type_queries(self, id: strawberry.ID) -> ObjectQueries[SomeType]:
return ObjectQueries(SomeType)
@strawberry.field
def other_type_queries(self, id: strawberry.ID) -> ObjectQueries[OtherType]:
return ObjectQueries(OtherType)
schema = strawberry.Schema(Query)
Splitting out the error union doesn't help, this also can't resolve fields:
from typing import Annotated, Generic, TypeVar, Union
import strawberry
from my_types import OtherType, SomeType
@strawberry.type
class NotFoundError:
id: strawberry.ID
message: str
T = TypeVar('T')
ByIdError = Annotated[NotFoundError, strawberry.union("ByIdError")]
@strawberry.type
class ObjectQueries(Generic[T]):
...
@strawberry.field
# NOTE annotaion moved to just the union
def by_id(self, id: strawberry.ID) -> T | ByIdError:
...
...
@strawberry.type
class Query:
@strawberry.field
def some_type_queries(self, id: strawberry.ID) -> ObjectQueries[SomeType]:
return ObjectQueries(SomeType)
@strawberry.field
def other_type_queries(self, id: strawberry.ID) -> ObjectQueries[OtherType]:
return ObjectQueries(OtherType)
schema = strawberry.Schema(Query)
It appears that the issue with the ones that raise an exception rather than simply having the wrong names have their problem stemming from not being able to from a union with an instance of strawberry.union
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