Skip to content

Commit 7b6f348

Browse files
committed
implement a handshake logic
1 parent 102c67c commit 7b6f348

File tree

1 file changed

+10
-1
lines changed

1 file changed

+10
-1
lines changed

python_graphql_client/graphql_client.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
"""Module containing graphQL client."""
22
import json
3+
import logging
34
from typing import Callable
45

56
import aiohttp
67
import requests
78
import websockets
89

10+
logging.basicConfig(level=logging.INFO, format="%(levelname)s:%(message)s")
11+
912

1013
class GraphqlClient:
1114
"""Class which represents the interface to make graphQL requests through."""
@@ -79,6 +82,8 @@ async def subscribe(
7982
headers: dict = None,
8083
):
8184
"""Make asynchronous request for GraphQL subscription."""
85+
connection_init_message = json.dumps({"type": "connection_init", "payload": {}})
86+
8287
request_body = self.__request_body(
8388
query=query, variables=variables, operation_name=operation_name
8489
)
@@ -89,7 +94,11 @@ async def subscribe(
8994
async with websockets.connect(
9095
self.endpoint, subprotocols=["graphql-ws"]
9196
) as websocket:
97+
await websocket.send(connection_init_message)
9298
await websocket.send(request_message)
9399
async for response_message in websocket:
94100
response_body = json.loads(response_message)
95-
handle(response_body["payload"])
101+
if response_body["type"] == "connection_ack":
102+
logging.info("the server accepted the connection")
103+
else:
104+
handle(response_body["payload"])

0 commit comments

Comments
 (0)