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 new strawberry.Parent annotation to support static resolvers with non-self types. #3017

Merged
merged 13 commits into from
Sep 14, 2023

Conversation

mattalbr
Copy link
Contributor

@mattalbr mattalbr commented Aug 7, 2023

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 #515

Types of Changes

  • Core
  • Bugfix
  • [ x ] New feature
  • Enhancement/optimization
  • Documentation

Issues Fixed or Closed by This PR

Checklist

  • [ x ] My code follows the code style of this project.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • [ x ] I have read the CONTRIBUTING document.
  • [ x ] I have added tests to cover my changes.
  • [ x ] I have tested the changes and verified that they work and don't break anything (as well as I can manage).

Unsure if this needs documentation!

@botberry
Copy link
Member

botberry commented Aug 7, 2023

Thanks for adding the RELEASE.md file!

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:

🆕 Release (next) is out! Thanks to mattalbr for the PR 👏

Get it here 👉 https://github.com/strawberry-graphql/strawberry/releases/tag/(next)

@codecov
Copy link

codecov bot commented Aug 7, 2023

Codecov Report

Merging #3017 (baef780) into main (4eb629e) will decrease coverage by 0.05%.
Report is 18 commits behind head on main.
The diff coverage is 99.07%.

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-hq
Copy link

codspeed-hq bot commented Aug 7, 2023

CodSpeed Performance Report

Merging #3017 will not alter performance

Comparing mattalbr:parent_without_error (baef780) with main (4eb629e)

Summary

✅ 12 untouched benchmarks

@mattalbr
Copy link
Contributor Author

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

Copy link
Member

@patrick91 patrick91 left a 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.
Copy link
Member

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? 😊

Copy link
Contributor Author

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")
...
"""
Copy link
Member

Choose a reason for hiding this comment

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

amazing!

Copy link
Contributor Author

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)
Copy link
Member

Choose a reason for hiding this comment

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

💯

Comment on lines +586 to +592
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:
Copy link
Member

Choose a reason for hiding this comment

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

baby-jump-gif-by-probit-global

Comment on lines 554 to 556
# Should we allow these?
def parent_and_self(self, parent: Parent[UserLiteral]) -> str:
return f"User {parent.id}"
Copy link
Member

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 :)

Copy link
Contributor Author

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"

RELEASE.md Outdated Show resolved Hide resolved
@github-actions
Copy link

@patrick91 patrick91 merged commit d1ff467 into strawberry-graphql:main Sep 14, 2023
57 of 58 checks passed
etripier pushed a commit to Greenbax/strawberry that referenced this pull request Oct 25, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants