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

Allow using a static method as a resolver #1430

Merged
merged 16 commits into from
Nov 27, 2021

Conversation

illia-v
Copy link
Member

@illia-v illia-v commented Nov 19, 2021

Description

In a project I had to define a resolver that gets some data from the root argument.
It was not possible to use self because of some mypy warnings. Therefore, I wanted to remove the self argument.
It was possible to remove it without using @staticmethod, but mypy complained that Self argument missing for a non-static method (or an invalid type for self).

So, I had to add @staticmethod.
I tried adding it before @strawberry.field, but Python did not add the field to __dataclass_fields__ and a type was not created correctly.
Then, I tried adding it after @strawberry.field, but Strawberry did not like that because AttributeError: 'staticmethod' object has no attribute '__annotations__'. The changes fix this problem.

Types of Changes

  • Core
  • Bugfix
  • New feature
  • Enhancement/optimization
  • Documentation

Checklist

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

@botberry
Copy link
Member

botberry commented Nov 19, 2021

Thanks for adding the RELEASE.md file!

Here's a preview of the changelog:


This release fixes an issue that prevented using classmethods and staticmethods as resolvers

import strawberry

@strawberry.type
class Query:
    @strawberry.field
    @staticmethod
    def static_text() -> str:
        return "Strawberry"

    @strawberry.field
    @classmethod
    def class_name(cls) -> str:
        return cls.__name__

Here's the preview release card for twitter:

Here's the tweet text:

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

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

strawberry/field.py Outdated Show resolved Hide resolved
@codecov
Copy link

codecov bot commented Nov 19, 2021

Codecov Report

Merging #1430 (d2a93d2) into main (ab4a6a6) will decrease coverage by 0.00%.
The diff coverage is 100.00%.

@@            Coverage Diff             @@
##             main    #1430      +/-   ##
==========================================
- Coverage   98.08%   98.08%   -0.01%     
==========================================
  Files         119      121       +2     
  Lines        4129     4228      +99     
  Branches      599      710     +111     
==========================================
+ Hits         4050     4147      +97     
  Misses         42       42              
- Partials       37       39       +2     

@BryceBeagle
Copy link
Member

I have a more robust fix for this that also supports classmethods working right now 👍. Just need to get mypy checks to comply. Hopefully we can get this merged by tonight

@BryceBeagle
Copy link
Member

After much deep-diving into (and frustration with!) the underlying mechanics of staticmethod and classmethod binding, I think this solution should be very robust and satisfy most, if not all, edge cases. I took the liberty of committing to your branch @illia-v.

This should be ready for review @patrick91. Let me know if I should add any more comments/documentation anywhere.

RELEASE.md Outdated Show resolved Hide resolved
strawberry/types/fields/resolver.py Show resolved Hide resolved
tests/fields/test_resolvers.py Show resolved Hide resolved
@patrick91 patrick91 merged commit 2139e63 into strawberry-graphql:main Nov 27, 2021
@botberry
Copy link
Member

Thanks for contributing to Strawberry! 🎉 You've been invited to join
the Strawberry GraphQL organisation 😊

You can also request a free sticker by filling this form: https://forms.gle/dmnfQUPoY5gZbVT67

And don't forget to join our discord server: https://strawberry.rocks/discord 🔥

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants