strawberry icon indicating copy to clipboard operation
strawberry copied to clipboard

Union inheritance/merging support for strawberry.union

Open lijok opened this issue 4 years ago • 1 comments
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
    )

lijok avatar Feb 12 '21 14:02 lijok

This could be fixed by supporting #2000, we'd need to check what happens with Annotated, but it might work fine :)

patrick91 avatar Jul 08 '22 22:07 patrick91

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 :)

patrick91 avatar Oct 20 '22 16:10 patrick91