Invalid Type for Union Merge Error
Description
This error is thrown when trying to extend an union with a type thatβs not allowed in unions, for example the following code will throw this error:
import strawberry
from typing import Union, Annotated
@strawberry.typeclass Example: name: str
ExampleUnion = Annotated[Union[Example], strawberry.union("ExampleUnion")]
@strawberry.typeclass Query: field: ExampleUnion | int
schema = strawberry.Schema(query=Query)
This happens because GraphQL doesnβt support scalars as union members.
How to fix this error
At the moment Strawberry doesnβt have a proper way to merge unions and types,
but you can still create a union type that combines multiple types manually.
Since GraphQL doesnβt allow scalars as union members, a workaround is to create
a wrapper type that contains the scalar value and use that instead. For example
the following code will create a union type between Example
and IntWrapper
which is a wrapper on top of the int
scalar:
import strawberry
from typing import Union, Annotated
@strawberry.typeclass Example: name: str
@strawberry.typeclass IntWrapper: value: int
ExampleUnion = Annotated[Union[Example, IntWrapper], strawberry.union("ExampleUnion")]
@strawberry.typeclass Query: field: ExampleUnion
schema = strawberry.Schema(query=Query)