strawberry-graphql/strawberry

a custom resolver with permission_classes doesn't accept @sync_to_async decorator

shmoon-kr opened this issue · 0 comments

a custom resolver with permission_classes doesn't accept @sync_to_async decorator

Here is a code sample.

@strawberry.type
class Query:
    test: typing.Optional[User] = strawberry.field(resolver=test, permission_classes=[IsAuthenticated])

class IsAuthenticated(BasePermission):
    message = str(UnauthorizedError)

    @sync_to_async
    # This method can also be async!
    def has_permission(self, source: typing.Any, info: Info, **kwargs) -> bool:
        user = info.context['user'] if type(info.context) is dict else info.context.user
        return user

async def test(info: Info) -> typing.Optional[apps.user.models.User]:
    user = info.context['user'] if type(info.context) is dict else await info.context.async_user
    return user

When I define a custom async resolver like above, it works correctly.
But when I define a custom async resolver using @sync_to_async decorator, it returns an error.

@sync_to_async
def test(info: Info) -> typing.Optional[apps.user.models.User]:
    user = info.context['user'] if type(info.context) is dict else info.context.user
    return user

graphql.error.graphql_error.GraphQLError: Expected value of type 'User' but got: <coroutine __call__>.

It happens only when a permission_classes parameter is given to a custom resolver.

Upvote & Fund

  • We're using Polar.sh so you can upvote and help fund this issue.
  • We receive the funding once the issue is completed & confirmed by you.
  • Thank you in advance for helping prioritize & fund our backlog.
Fund with Polar