-
-
Notifications
You must be signed in to change notification settings - Fork 531
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
enoua5
wants to merge
17
commits into
strawberry-graphql:main
Choose a base branch
from
enoua5:fix-3393
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+207
−9
Open
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
5cfac3c
Merge unions
enoua5 747b2ea
Use extend instead of +=
enoua5 674b0bb
Sourcery suggestion
enoua5 b2c7fc1
Sourcery suggestion
enoua5 0f31c05
Add RELEASE.md
enoua5 b83d078
Merge branch 'fix-3393' of https://github.com/enoua5/strawberry-3393 …
enoua5 e63fed0
Fix check on older python versions
patrick91 318b47b
Add test that was already passing
patrick91 42a5827
Add missing test
patrick91 ac29523
Add support for generic unions
patrick91 576593b
Lint
patrick91 280d841
Lint
patrick91 99cc745
Add test for annotated union with two generics
enoua5 d6c58ed
Rewrite test to support python <= 3.9
enoua5 b34106e
Remove unused function bodies from tests
enoua5 5be8943
Merge branch 'fix-3393' of https://github.com/enoua5/strawberry-3393
enoua5 65e85f1
Update RELEASE.md
enoua5 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
Release type: minor | ||
|
||
Attempt to merge union types during schema conversion. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -850,3 +850,178 @@ class Query: | |
|
||
assert not result.errors | ||
assert result.data["something"] == {"__typename": "A", "a": 5} | ||
|
||
|
||
def test_generic_union_with_annotated(): | ||
@strawberry.type | ||
class SomeType: | ||
id: strawberry.ID | ||
name: str | ||
|
||
@strawberry.type | ||
class NotFoundError: | ||
id: strawberry.ID | ||
message: str | ||
|
||
T = TypeVar("T") | ||
|
||
@strawberry.type | ||
class ObjectQueries(Generic[T]): | ||
@strawberry.field | ||
def by_id( | ||
self, id: strawberry.ID | ||
) -> Annotated[Union[T, NotFoundError], strawberry.union("ByIdResult")]: ... | ||
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. just to clarify, this is quite different from the use case below, as this creates a generic union 😊 |
||
|
||
@strawberry.type | ||
class Query: | ||
@strawberry.field | ||
def some_type_queries(self, id: strawberry.ID) -> ObjectQueries[SomeType]: | ||
raise NotImplementedError() | ||
|
||
schema = strawberry.Schema(Query) | ||
|
||
assert ( | ||
str(schema) | ||
== textwrap.dedent( | ||
""" | ||
type NotFoundError { | ||
id: ID! | ||
message: String! | ||
} | ||
|
||
type Query { | ||
someTypeQueries(id: ID!): SomeTypeObjectQueries! | ||
} | ||
|
||
type SomeType { | ||
id: ID! | ||
name: String! | ||
} | ||
|
||
union SomeTypeByIdResult = SomeType | NotFoundError | ||
|
||
type SomeTypeObjectQueries { | ||
byId(id: ID!): SomeTypeByIdResult! | ||
} | ||
""" | ||
).strip() | ||
) | ||
|
||
|
||
def test_generic_union_with_annotated_inside(): | ||
@strawberry.type | ||
class SomeType: | ||
id: strawberry.ID | ||
name: str | ||
|
||
@strawberry.type | ||
class NotFoundError: | ||
id: strawberry.ID | ||
message: str | ||
|
||
T = TypeVar("T") | ||
|
||
@strawberry.type | ||
class ObjectQueries(Generic[T]): | ||
@strawberry.field | ||
def by_id( | ||
self, id: strawberry.ID | ||
) -> Union[T, Annotated[NotFoundError, strawberry.union("ByIdResult")]]: ... | ||
|
||
@strawberry.type | ||
class Query: | ||
@strawberry.field | ||
def some_type_queries(self, id: strawberry.ID) -> ObjectQueries[SomeType]: ... | ||
|
||
schema = strawberry.Schema(Query) | ||
|
||
assert ( | ||
str(schema) | ||
== textwrap.dedent( | ||
""" | ||
type NotFoundError { | ||
id: ID! | ||
message: String! | ||
} | ||
|
||
type Query { | ||
someTypeQueries(id: ID!): SomeTypeObjectQueries! | ||
} | ||
|
||
type SomeType { | ||
id: ID! | ||
name: String! | ||
} | ||
|
||
union SomeTypeByIdResult = SomeType | NotFoundError | ||
|
||
type SomeTypeObjectQueries { | ||
byId(id: ID!): SomeTypeByIdResult! | ||
} | ||
""" | ||
).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 | ||
) -> Union[ | ||
T, Annotated[Union[U, NotFoundError], strawberry.union("ByIdResult")] | ||
]: ... | ||
|
||
@strawberry.type | ||
class Query: | ||
@strawberry.field | ||
def some_type_queries( | ||
self, id: strawberry.ID | ||
) -> UnionObjectQueries[SomeType, OtherType]: ... | ||
|
||
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() | ||
) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I fixed the name of this function 😊