【问题标题】:Hasura: Subscriptions - no subprotocols supportedHasura:订阅 - 不支持子协议
【发布时间】:2020-07-19 10:13:42
【问题描述】:
subscription = Template('''
    subscription {
        users {
            id
            name
        }
    }
''')

async def user_listener():

    headers = {'Content-Type': 'application/json',
               'x-hasura-access-key': 'mysecretkey'}

    uri = "ws://localhost:8080/v1alpha1/graphql"
    async with websockets.connect(uri, extra_headers=headers) as websocket:
        await websocket.send(subscription.render())
        while True:
            greeting = await websocket.recv()
            print(f"< {greeting}")


def main():
    asyncio.get_event_loop().run_until_complete(user_listener())

我正在使用 python websockets 来实现一个使用 Hasura graphql 订阅的简单侦听器。但是,当我运行上述程序时,我遇到了以下异常。 (注意我正在使用 docker-compose 在我的笔记本电脑上试用 hasura)

  File "listener.py", line 39, in <module>
    main()
  File "listener.py", line 35, in main
    asyncio.get_event_loop().run_until_complete(infradb_listener())
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/asyncio/base_events.py", line 468, in run_until_complete
    return future.result()
  File "listener.py", line 27, in infradb_listener
    async with websockets.connect(uri, extra_headers=headers) as websocket:
  File "/Users/vpatil3/projects/hasura-ws-example/venv/lib/python3.6/site-packages/websockets/client.py", line 517, in __aenter__
    return await self
  File "/Users/vpatil3/projects/hasura-ws-example/venv/lib/python3.6/site-packages/websockets/client.py", line 547, in __await_impl__
    extra_headers=protocol.extra_headers,
  File "/Users/vpatil3/projects/hasura-ws-example/venv/lib/python3.6/site-packages/websockets/client.py", line 305, in handshake
    response_headers, available_subprotocols
  File "/Users/vpatil3/projects/hasura-ws-example/venv/lib/python3.6/site-packages/websockets/client.py", line 207, in process_subprotocol
    raise InvalidHandshake("no subprotocols supported")
websockets.exceptions.InvalidHandshake: no subprotocols supported

【问题讨论】:

    标签: python websocket graphql hasura


    【解决方案1】:

    通过将 subproctocols 添加到 websocket 连接 API 来解决问题,如下所示

     async with websockets.connect(uri, subprotocols='graphql-ws', extra_headers=headers) as websocket:
    

    【讨论】:

    • 感谢子协议的提示。我可以问你如何在 python 端格式化订阅吗?我看到您的代码中有一些 Template() 和 render() 方法。当我将订阅作为纯字符串在我的盒子上进行测试时,它失败并显示以下消息:“{“type”:“connection_error”,“payload”:“parsing ClientMessage failed: Error in $: Failed reading: not a valid json value at 'subscriptionMySubscription{'"}" 但我不确定如何更改订阅 grahpql 语法以识别为有效的 json。
    猜你喜欢
    • 1970-01-01
    • 2014-11-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-21
    • 2011-11-02
    相关资源
    最近更新 更多