graphql-python / graphql-ws

GraphQL websockets

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Unable to access connectParams on request

jcunhafonte opened this issue · comments

  • GraphQL AioWS version:
    0.3.0
  • Python version:
    3.6.6

Description

When implementing this configuration https://www.apollographql.com/docs/angular/features/subscriptions.html#authentication I could not access connectionParams property on async def subscriptions(request):

The above looks like a generic request handler for ws. Any authentication details should come over via your connection context, which you can then validate via the on_start / payload.

I did something like below

class CtxMonkeyPatch(AiohttpConnectionContext):
    def __init__(self, ws, request_context):
        super().__init__(ws, request_context)
        self.logger = get_logger('ws_conn_ctx')
        self.token = None

    async def on_connect(self, payload):
        if payload.get('context') is not None:
            params = payload.get('context')
            if params.get('access') is not None:
                self.token = params.get('access')

    async def on_start(self):
        if self.token is not None:
            try:
                _user, _system = handle_token(self.token)
                if _user is not None:
                    self.request_context['user'] = _user.get('user')
            except Exception as e:
                self.logger.error(e)
                raise e

Should be fixed now in master