-
-
Notifications
You must be signed in to change notification settings - Fork 603
Add support for lazy unions #4017
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
Changes from 4 commits
73086a7
247745e
47b20fb
fcac5d6
f38f73f
f58fa30
76b8568
4dbe410
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| Release type: patch | ||
|
|
||
| Adds support for lazy unions. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,9 +2,11 @@ | |
|
|
||
| import dataclasses | ||
| import sys | ||
| import typing | ||
| from functools import partial, reduce | ||
| from typing import ( | ||
| TYPE_CHECKING, | ||
| Annotated, | ||
| Any, | ||
| Callable, | ||
| Generic, | ||
|
|
@@ -862,6 +864,10 @@ def from_type(self, type_: Union[StrawberryType, type]) -> GraphQLNullableType: | |
| if compat.is_graphql_generic(type_): | ||
| raise MissingTypesForGenericError(type_) | ||
|
|
||
| # to handle lazy unions | ||
| if typing.get_origin(type_) is Annotated and len(typing.get_args(type_)) == 2: | ||
| type_ = typing.get_args(type_)[1] | ||
|
|
||
|
Comment on lines
867
to
872
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It isn't the best place for this code. It would be better if resolving a lazy type would return a StrawberryUnion object in this case, but I couldn't make it work 🤔
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. could be worth trying in another PR! |
||
| if isinstance(type_, EnumDefinition): # TODO: Replace with StrawberryEnum | ||
| return self.from_enum(type_) | ||
| if compat.is_input_type(type_): # TODO: Replace with StrawberryInputObject | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,90 @@ | ||
| import textwrap | ||
| from typing import Annotated, Union | ||
|
|
||
| import strawberry | ||
| from strawberry.printer import print_schema | ||
|
|
||
|
|
||
| @strawberry.type | ||
| class TypeA: | ||
| a: int | ||
|
|
||
|
|
||
| @strawberry.type | ||
| class TypeB: | ||
| b: int | ||
|
|
||
|
|
||
| ABUnion = Annotated[ | ||
| Union[TypeA, TypeB], strawberry.union("ABUnion", types=[TypeA, TypeB]) | ||
| ] | ||
|
|
||
|
|
||
| TypeALazy = Annotated[ | ||
| "TypeA", strawberry.lazy("tests.schema.test_lazy_types.test_lazy_unions") | ||
| ] | ||
| TypeBLazy = Annotated[ | ||
| "TypeB", strawberry.lazy("tests.schema.test_lazy_types.test_lazy_unions") | ||
| ] | ||
| LazyABUnion = Annotated[ | ||
| Union[ | ||
| TypeALazy, | ||
| TypeBLazy, | ||
| ], | ||
| strawberry.union("LazyABUnion", types=[TypeALazy, TypeBLazy]), | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If a union does not define types, an error will be raised that union has to have at least one member |
||
| ] | ||
|
|
||
|
|
||
| def test_lazy_union_with_non_lazy_members(): | ||
| @strawberry.type | ||
| class Query: | ||
| ab: Annotated[ | ||
| "ABUnion", strawberry.lazy("tests.schema.test_lazy_types.test_lazy_unions") | ||
| ] | ||
|
|
||
| expected = """ | ||
| union ABUnion = TypeA | TypeB | ||
| type Query { | ||
| ab: ABUnion! | ||
| } | ||
| type TypeA { | ||
| a: Int! | ||
| } | ||
| type TypeB { | ||
| b: Int! | ||
| } | ||
| """ | ||
|
|
||
| schema = strawberry.Schema(query=Query) | ||
| assert print_schema(schema) == textwrap.dedent(expected).strip() | ||
|
|
||
|
|
||
| def test_lazy_union_with_lazy_members(): | ||
| @strawberry.type | ||
| class Query: | ||
| ab: Annotated[ | ||
| "LazyABUnion", | ||
| strawberry.lazy("tests.schema.test_lazy_types.test_lazy_unions"), | ||
| ] | ||
|
|
||
rcybulski1122012 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| expected = """ | ||
| union LazyABUnion = TypeA | TypeB | ||
| type Query { | ||
| ab: LazyABUnion! | ||
| } | ||
| type TypeA { | ||
| a: Int! | ||
| } | ||
| type TypeB { | ||
| b: Int! | ||
| } | ||
| """ | ||
|
|
||
| schema = strawberry.Schema(query=Query) | ||
| assert print_schema(schema) == textwrap.dedent(expected).strip() | ||
Uh oh!
There was an error while loading. Please reload this page.