strawberry icon indicating copy to clipboard operation
strawberry copied to clipboard

Generic Union Types

Open HopeBaron opened this issue 1 year ago • 0 comments

Feature Request Type

  • [ ] Core functionality
  • [ ] Alteration (enhancement/optimization) of existing feature(s)
  • [x] New behavior

Description

It would be nice to be able to make a generic union type to allow Success | Failure union types currently this won't work:

@strawberry.type(model.Error, all_fields=True)
class ErrorType:
    pass


T = TypeVar("T")

Result = strawberry.union("Result", (T, ErrorType))

As noted by @Doctor#7942 on Discord (couldn't mention in the issue) it's possible to achieve desired results using Pydantic as follows:

from typing import TypeVar, Union


class Error:
    pass


T = TypeVar("T")

TOrError = Union[T, Error]

int_or_error: TOrError[int] = 42

A reproducible example made by @patrick91 on Strawberry Playground

Original discussion

HopeBaron avatar Aug 28 '22 06:08 HopeBaron