-
-
Notifications
You must be signed in to change notification settings - Fork 535
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 new strawberry.Parent annotation to support static resolvers with non-self types. #3017
Add new strawberry.Parent annotation to support static resolvers with non-self types. #3017
Conversation
Thanks for adding the Here's a preview of the changelog: Adds new strawberry.Parent type annotation to support resolvers without use of self. E.g. @dataclass
class UserRow:
id_: str
@strawberry.type
class User:
@strawberry.field
@staticmethod
async def name(parent: strawberry.Parent[UserRow]) -> str:
return f"User Number {parent.id}"
@strawberry.type
class Query:
@strawberry.field
def user(self) -> User:
return UserRow(id_="1234") Here's the preview release card for twitter: Here's the tweet text:
|
Codecov Report
Additional details and impacted files@@ Coverage Diff @@
## main #3017 +/- ##
==========================================
- Coverage 96.53% 96.49% -0.05%
==========================================
Files 468 469 +1
Lines 29116 29255 +139
Branches 3582 3603 +21
==========================================
+ Hits 28108 28230 +122
- Misses 827 843 +16
- Partials 181 182 +1 |
CodSpeed Performance ReportMerging #3017 will not alter performanceComparing Summary
|
FYI @patrick91 I think the code coverage error will be fixed in #3028 There's only one early exit line in real code that seems to be missing coverage |
Going to try again without the ConflictingArgumentsError.
aab8a82
to
a916ef9
Compare
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 looks almost perfect :) I left a not about potentially throwing an error when using more than one of self/root/Parent
@@ -0,0 +1,3 @@ | |||
Release type: minor | |||
|
|||
Adds new strawberry.Parent type annotation to support resolvers without use of self. |
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.
can you add a quick example of how this works here? 😊
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.
Done!
... def user(self) -> User: | ||
... return UserRow(id_="1234") | ||
... | ||
""" |
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.
amazing!
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.
Thanks!
) | ||
|
||
return False | ||
return type_has_annotation(type_, StrawberryPrivate) |
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.
💯
if parent_parameter := field.base_resolver.parent_parameter: | ||
kwargs[parent_parameter.name] = source | ||
|
||
if root_parameter := field.base_resolver.root_parameter: | ||
kwargs[root_parameter.name] = source | ||
|
||
info_parameter = field.base_resolver.info_parameter | ||
if info_parameter: | ||
if info_parameter := field.base_resolver.info_parameter: |
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.
tests/schema/test_resolvers.py
Outdated
# Should we allow these? | ||
def parent_and_self(self, parent: Parent[UserLiteral]) -> str: | ||
return f"User {parent.id}" |
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 had the same question! I think it might confusing to allow both, so might be nice to throw a warning or error :)
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.
Done! I went ahead and also enforced the same for self + root and some other combos under the generic principle of "identify each resource at most once"
Hi 👋 You can find a preview of the docs here: https://strawberry.rocks/docs/pr/3017/errors/conflicting-arguments |
Description
Allows creating a resolver that depends on a parent which has a different type than the resolver's class, as in
self
isn't particularly accurate. This is much more descriptive (and allows type checking) than calling the parameter "self" and knowing it will be a different type. See description in #515Types of Changes
Issues Fixed or Closed by This PR
Checklist
Unsure if this needs documentation!