strawberry
strawberry copied to clipboard
Union inheritance/merging support for strawberry.union
trafficstars
In python, typing.Union can be merged
a = Union[Test, Test0]
b = Union[Test1, Test2]
c = Union[a, b]
c == Union[Test, Test0, Test1, Test2]
Being able to merge StrawberryUnion's when creating a union with strawberry.union would be helpful in some cases and should be fairly cheap to implement Example implementation in my project that adds that functionality by wrapping strawberry.union
def union(
name: str,
types: Tuple[Type, ...],
*args,
description: str = None,
) -> StrawberryUnion:
expanded_types = []
for _type in types:
if hasattr(_type, "__origin__") and _type.__origin__ == Union:
expanded_types.extend(_type.__args__)
elif isinstance(_type, StrawberryUnion):
expanded_types.extend(_type.types)
else:
expanded_types.append(_type)
return strawberry.union(
name=name,
types=tuple(expanded_types),
*args,
description=description
)
This could be fixed by supporting #2000, we'd need to check what happens with Annotated, but it might work fine :)
I removed the good first issue label, I think this might not be easy to get started with, also it will probably be stale once we do #2000 :)