Skip to content

Add Awaitable in resolver type #2528

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

Merged
merged 1 commit into from
Feb 9, 2023

Conversation

patrick91
Copy link
Member

Description

This PR adds Awaitable inside our type for resolvers passed
to strawberry.field :)

Types of Changes

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

Issues Fixed or Closed by This PR

@codecov
Copy link

codecov bot commented Feb 9, 2023

Codecov Report

Merging #2528 (a929e3f) into main (fd26cc0) will not change coverage.
The diff coverage is n/a.

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #2528   +/-   ##
=======================================
  Coverage   96.70%   96.70%           
=======================================
  Files         183      183           
  Lines        7579     7579           
  Branches     1374     1374           
=======================================
  Hits         7329     7329           
  Misses        148      148           
  Partials      102      102           

@botberry
Copy link
Member

botberry commented Feb 9, 2023

Thanks for adding the RELEASE.md file!

Here's a preview of the changelog:


This release updates the typing for the resolver argument in
strawberry.fieldi to support async resolvers.
This means that now you won't get any type
error from Pyright when using async resolver, like the following example:

import strawberry


async def get_user_age() -> int:
    return 0


@strawberry.type
class User:
    name: str
    age: int = strawberry.field(resolver=get_user_age)

Here's the preview release card for twitter:

Here's the tweet text:

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

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

Copy link
Contributor

@skilkis skilkis left a comment

Choose a reason for hiding this comment

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

Looks very good @patrick91 ! Good tests too

@patrick91 patrick91 merged commit ed24095 into main Feb 9, 2023
@patrick91 patrick91 deleted the feature/add-awaitable-resolver-pyright branch February 9, 2023 16:42
@Mark90
Copy link
Contributor

Mark90 commented Feb 10, 2023

Hello!

It seems that this fix for pyright has made mypy unhappy. I can reproduce with this extra testcase in tests/mypy/test_fields.yml


- case: test_field_with_async_resolver
  main: |
    import strawberry

    async def get_user_age() -> int:
        return 0


    @strawberry.type
    class User:
        name: str
        age: int = strawberry.field(resolver=get_user_age)
  out: |

This returns:

E   pytest_mypy_plugins.utils.TypecheckAssertionError: Output is not expected:
E   Actual:
E     main:10: error: Argument "resolver" to "field" has incompatible type "Callable[[], Coroutine[Any, Any, int]]"; expected "Union[StrawberryResolver[<nothing>], Callable[..., <nothing>], Callable[..., Awaitable[<nothing>]], staticmethod[<nothing>], classmethod[<nothing>]]"  [arg-type] (diff)
E   Expected:
E     (empty)
====================

When I revert the resolver type change in strawberry/fields.py then the test passes.

@patrick91
Copy link
Member Author

@Mark90 sorry about that! I'll take a look at this today

@patrick91
Copy link
Member Author

@Mark90 I've opened an issue on Mypy's side: python/mypy#14669

If that doesn't get solved I'll try to find a work around 😊

@Mark90
Copy link
Contributor

Mark90 commented Feb 10, 2023

Thanks a lot for the quick action :)

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.

Additional type hints for strawberry.field - so that it can be used with async reolvers
4 participants