-
-
Notifications
You must be signed in to change notification settings - Fork 534
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
base: main
Are you sure you want to change the base?
Conversation
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.
Hey @enoua5 - I've reviewed your changes and they look great!
Here's what I looked at during the review
- 🟡 General issues: 3 issues found
- 🟢 Security: all looks good
- 🟢 Testing: all looks good
- 🟢 Complexity: all looks good
Help me be more useful! Please click 👍 or 👎 on each comment to tell me if it was helpful.
Co-authored-by: sourcery-ai[bot] <58596630+sourcery-ai[bot]@users.noreply.github.com>
Co-authored-by: sourcery-ai[bot] <58596630+sourcery-ai[bot]@users.noreply.github.com>
Co-authored-by: sourcery-ai[bot] <58596630+sourcery-ai[bot]@users.noreply.github.com>
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #3515 +/- ##
==========================================
- Coverage 96.76% 96.71% -0.06%
==========================================
Files 522 503 -19
Lines 33824 33531 -293
Branches 5635 5641 +6
==========================================
- Hits 32731 32429 -302
- Misses 863 878 +15
+ Partials 230 224 -6 |
CodSpeed Performance ReportMerging #3515 will not alter performanceComparing Summary
|
@enoua5 I pushed a fix 😊 but I'll try to write a test as well |
tests/schema/test_union.py
Outdated
} | ||
|
||
type Query { | ||
someTypeQueries(id: ID!): SomeTypeByIdResult! |
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.
The type here is still wrong, we probably need to expand the annotated
RELEASE.md
Outdated
Release type: patch | ||
|
||
Attempt to merge union types during schema conversion. |
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.
with the last change this is probably a minor now, since it's a brand new feature (plus a fix) 😊
elif isinstance(type_, StrawberryOptional): | ||
name = self.get_from_type(type_.of_type) + "Optional" | ||
name = self.get_name_from_type(type_.of_type) + "Optional" |
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 😊
@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 comment
The 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 😊
Thank you for the fixes, @patrick91! It's looking good, and each of the examples in #3393 are now working as expected! 🎉 It sounds like we'll need to change the release file? Does something like "Add support for generic unions" encompass the new changes? As far as examples go for the release file, would copying an example or two from the new tests suffice? |
@enoua5 yes! I'll change the release file |
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.
Noice
Just realized we should probably also have a test to make sure unions with multiple generics work. If we think that's needed, I can add one when I get home from work today |
@enoua5 yes please 😊 |
Also of note is that this is technically a breaking change. A breaking change that makes output more predictable and stable, but a breaking change nonetheless. from typing import Annotated, Generic, TypeVar
import strawberry
@strawberry.type
class SomeType:
a: 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[T | NotFoundError, strawberry.union("ByIdResult")]:
...
...
@strawberry.type
class Query:
@strawberry.field
def some_type_queries(self, id: strawberry.ID) -> ObjectQueries[SomeType]:
return ObjectQueries(SomeType)
schema = strawberry.Schema(Query) Under the current version of strawberry, this SDL is generated: type Query {
someTypeQueries(id: ID!): SomeTypeObjectQueries!
}
type SomeTypeObjectQueries {
byId(id: ID!): SomeTypeNotFoundError!
}
union SomeTypeNotFoundError = SomeType | NotFoundError
type SomeType {
a: String!
}
type NotFoundError {
id: ID!
message: String!
} Whereas under this PR, this is the SDL generated: type Query {
someTypeQueries(id: ID!): SomeTypeObjectQueries!
}
type SomeTypeObjectQueries {
byId(id: ID!): SomeTypeByIdResult!
}
union SomeTypeByIdResult = SomeType | NotFoundError
type SomeType {
a: String!
}
type NotFoundError {
id: ID!
message: String!
} For clarity, that: 6c6
< byId(id: ID!): SomeTypeNotFoundError!
---
> byId(id: ID!): SomeTypeByIdResult!
9c9
< union SomeTypeNotFoundError = SomeType | NotFoundError
---
> union SomeTypeByIdResult = SomeType | NotFoundError While I do this this naming is better and makes it easier to write a consistent API (in the former case, adding a new error type |
tests/schema/test_union.py
Outdated
@strawberry.field | ||
def by_id( | ||
self, id: strawberry.ID | ||
) -> T | Annotated[U | NotFoundError, strawberry.union("ByIdResult")]: ... |
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.
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.
Didn't realize merging would list all the other commits on here 😅 how do i squash that? |
Seems like something went wrong during merge, your diff has 12k lines now :( |
There we go, just needed to get github to recheck the base |
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.
Made a code suggestion here, will leave to @patrick91 to give the final approval
Co-authored-by: Thiago Bellini Ribeiro <[email protected]>
Description
This PR merges union types in
strawberry.schema.schema_converter.GraphQLCoreCoverter:from_union
. This allows creating a more stable interface, as a union can be created between a TypeVar and an annotated union. In the examples in #3393, this changesSomeTypeByIdNotFoundError
=>SomeTypeByIdResult
, the latter of which won't change as more error cases are added.Types of Changes
Issues Fixed or Closed by This PR
Checklist