Giter VIP home page Giter VIP logo

Comments (1)

shmoon-kr avatar shmoon-kr commented on June 14, 2024

I found a walkaround for this case.

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

    # This method can also be async!
    @sync_to_async
    def has_permission(self, source: typing.Any, info: Info, **kwargs) -> bool:
        user = info.context.user
        if user is not None:
            return True
        else:
            return False

@dataclass
class ChannelsContext:
    request: ChannelsRequest
    response: TemporalResponse

    @property
    def user(self):
        for header in self.request.consumer.scope['headers']:
            if header[0] == b'authorization':
                token = AccessToken(token=header[1].decode('utf-8').split(' ')[1], verify=False)
                return get_user_model().objects.select_related('player').get(id=token.get(SIMPLE_JWT["USER_ID_CLAIM"]))
        return None

But there's another problem.
When I define an async resolver, it worked.

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

But when I define a resolver using @sync_to_async decorator, it raises an error.

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

"message": "Expected value of type 'User' but got: <coroutine __call__>.",

from strawberry.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.