【发布时间】: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