It seems like python (typing
) union types don't work with the lazy annotations documented here: https://strawberry.rocks/docs/types/lazy.
If i have two types that need to import from each other: Kennel and Pet(union of Cat and Dog) . I will get the following error if I try to do a lazy annotation on the union type.
# pets.schema
from .pet_stores.schema import PetStore
@strawberry.type
class Cat:
pet_store: PetStore
@strawberry.type
class Dog:
pet_store: PetStore
Pet = Union[Cat, Dog]
# pet_stores.schema
from .pets import Pet
if TYPE_CHECKING:
from .pets.schema import Pet
@strawberry.type
class Kennel
pet: Annotated["Pet", strawberry.lazy(".pets.schema")]
throws:
TypeError: PetStore fields cannot be resolved. Unexpected type 'typing.Union[api_graphql.dog.schema.Dog, api_graphql.cat.schema.Cat]'
As mentioned by @patrick91 you work around this issue by swapping typing
's Union
with strawberry.union
.
# Pet = Union[Cat, Dog]
Pet = strawberry.union("Pet", (Cat, Dog))
Discord convo: https://discord.com/channels/689806334337482765/1036814233234579526/1036814233234579526
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