Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for generic unions #3515

Open
wants to merge 17 commits into
base: main
Choose a base branch
from
Open
63 changes: 63 additions & 0 deletions tests/schema/test_union.py
Original file line number Diff line number Diff line change
Expand Up @@ -938,7 +938,7 @@
class Query:
@strawberry.field
def some_type_queries(self, id: strawberry.ID) -> ObjectQueries[SomeType]:
return ObjectQueries(SomeType)

Check warning on line 941 in tests/schema/test_union.py

View check run for this annotation

Codecov / codecov/patch

tests/schema/test_union.py#L941

Added line #L941 was not covered by tests

schema = strawberry.Schema(Query)

Expand Down Expand Up @@ -968,3 +968,66 @@
"""
).strip()
)


def test_annoted_union_with_two_generics():
@strawberry.type
class SomeType:
a: str

@strawberry.type
class OtherType:
b: str

@strawberry.type
class NotFoundError:
message: str

T = TypeVar("T")
U = TypeVar("U")

@strawberry.type
class UnionObjectQueries(Generic[T, U]):
@strawberry.field
def by_id(
self, id: strawberry.ID
) -> T | Annotated[U | NotFoundError, strawberry.union("ByIdResult")]: ...
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test captures the combined case of one generic inside and one generic outside the annotation. Both in and both out also work, but I figured the tests would be getting a bit redundant if I included every combination of in and out.


@strawberry.type
class Query:
@strawberry.field
def some_type_queries(
self, id: strawberry.ID
) -> UnionObjectQueries[SomeType, OtherType]:
return UnionObjectQueries()

Check warning on line 1002 in tests/schema/test_union.py

View check run for this annotation

Codecov / codecov/patch

tests/schema/test_union.py#L1002

Added line #L1002 was not covered by tests

schema = strawberry.Schema(Query)

assert (
str(schema)
== textwrap.dedent(
"""
type NotFoundError {
message: String!
}

type OtherType {
b: String!
}

type Query {
someTypeQueries(id: ID!): SomeTypeOtherTypeUnionObjectQueries!
}

type SomeType {
a: String!
}

union SomeTypeOtherTypeByIdResult = SomeType | OtherType | NotFoundError

type SomeTypeOtherTypeUnionObjectQueries {
byId(id: ID!): SomeTypeOtherTypeByIdResult!
}
"""
).strip()
)
Loading