I extended my tests by adding the @withSubscription flag to a test type in my schema.
I also update the minimal example in this way:
import json
import websocket
import uuid
from threading import Thread
def connect():
return websocket.create_connection(
"ws://10.60.2.4:8080/graphql",
subprotocols=["graphql-ws"]
)
def init_graph_connection():
data = json.dumps({
'type': 'connection_init',
'payload': {
"headers": ""
}
})
ws.send(data)
def recieve_task():
while True:
res = json.loads(ws.recv())
print(res)
def run_query(query_string):
payload = {
"headers": {"Content-Type": "application/json"},
"query": query_string
}
query_id = uuid.uuid4().hex
data_to_send = json.dumps(
{"id": query_id,
"type": 'start',
"payload": payload
}
)
ws.send(data_to_send)
ws.send(json.dumps({
'id': query_id,
'type': 'stop'
}))
def run_subscription(query_string):
payload = {
"headers": {"Content-Type": "application/json"},
"query": query_string
}
query_id = uuid.uuid4().hex
data_to_send = json.dumps(
{"id": query_id,
"type": 'start',
"payload": payload
}
)
ws.send(data_to_send)
ws = connect()
init_graph_connection()
query = ''' {
queryLocation(){
locationId
friendlyName
}
}'''
recieve_thread = Thread(target=recieve_task)
recieve_thread.start()
run_query(query)
run_subscription(query)
Now I’m able to do subscriptions.
This is the output of the program:
{'type': 'connection_ack'}
{'type': 'data', 'payload': {'data': {'queryLocation': [{'friendlyName': 'office1', 'locationId': 'office'}]}, 'extensions': {'touched_uids': 3, 'tracing': {'duration': 1902376, 'execution': {'resolvers': [{'duration': 1833960, 'startOffset': 40554, 'dgraph': [{'duration': 1734515, 'label': 'query', 'startOffset': 87212}], 'returnType': '[Location]', 'parentType': 'Query', 'path': ['queryLocation'], 'fieldName': 'queryLocation'}]}, 'startTime': '2020-10-29T10:21:42.797721237Z', 'endTime': '2020-10-29T10:21:42.799623626Z', 'version': 1}}}, 'id': 'c84f7b64e7a64617b6a0739d259ce4ff'}
{'type': 'complete', 'id': 'c84f7b64e7a64617b6a0739d259ce4ff'}
{'type': 'data', 'payload': {'data': {'queryLocation': [{'friendlyName': 'office1', 'locationId': 'office'}]}, 'extensions': {'touched_uids': 3, 'tracing': {'duration': 2359163, 'execution': {'resolvers': [{'duration': 2262678, 'startOffset': 72834, 'dgraph': [{'duration': 2110487, 'label': 'query', 'startOffset': 173557}], 'returnType': '[Location]', 'parentType': 'Query', 'path': ['queryLocation'], 'fieldName': 'queryLocation'}]}, 'startTime': '2020-10-29T10:21:42.800147641Z', 'endTime': '2020-10-29T10:21:42.802506835Z', 'version': 1}}}, 'id': '6541f04ba9e943a2b2ced579a5f641d2'}
according the specification I got a measage back with type ‘complete’.
The subscription should be terminated.
When I run a mutation, I get several responses. Several for the the query and several for the subscription.
So the query is also an open not canceled subscription.
Output after mutation:
{'type': 'connection_ack'}
{'type': 'data', 'payload': {'data': {'queryLocation': [{'friendlyName': 'office1', 'locationId': 'office'}]}, 'extensions': {'touched_uids': 3, 'tracing': {'duration': 1902376, 'execution': {'resolvers': [{'duration': 1833960, 'startOffset': 40554, 'dgraph': [{'duration': 1734515, 'label': 'query', 'startOffset': 87212}], 'returnType': '[Location]', 'parentType': 'Query', 'path': ['queryLocation'], 'fieldName': 'queryLocation'}]}, 'startTime': '2020-10-29T10:21:42.797721237Z', 'endTime': '2020-10-29T10:21:42.799623626Z', 'version': 1}}}, 'id': 'c84f7b64e7a64617b6a0739d259ce4ff'}
{'type': 'complete', 'id': 'c84f7b64e7a64617b6a0739d259ce4ff'}
{'type': 'data', 'payload': {'data': {'queryLocation': [{'friendlyName': 'office1', 'locationId': 'office'}]}, 'extensions': {'touched_uids': 3, 'tracing': {'duration': 2359163, 'execution': {'resolvers': [{'duration': 2262678, 'startOffset': 72834, 'dgraph': [{'duration': 2110487, 'label': 'query', 'startOffset': 173557}], 'returnType': '[Location]', 'parentType': 'Query', 'path': ['queryLocation'], 'fieldName': 'queryLocation'}]}, 'startTime': '2020-10-29T10:21:42.800147641Z', 'endTime': '2020-10-29T10:21:42.802506835Z', 'version': 1}}}, 'id': '6541f04ba9e943a2b2ced579a5f641d2'}
{'type': 'data', 'payload': {'data': {'queryLocation': [{'friendlyName': 'office2', 'locationId': 'office'}]}, 'extensions': {'touched_uids': 3, 'tracing': {'duration': 3216314, 'execution': {'resolvers': [{'duration': 2869655, 'startOffset': 320361, 'dgraph': [{'duration': 2684096, 'label': 'query', 'startOffset': 460951}], 'returnType': '[Location]', 'parentType': 'Query', 'path': ['queryLocation'], 'fieldName': 'queryLocation'}]}, 'startTime': '2020-10-29T10:21:49.818045846Z', 'endTime': '2020-10-29T10:21:49.821262205Z', 'version': 1}}}, 'id': '6541f04ba9e943a2b2ced579a5f641d2'}
{'type': 'data', 'payload': {'data': {'queryLocation': [{'friendlyName': 'office2', 'locationId': 'office'}]}, 'extensions': {'touched_uids': 3, 'tracing': {'duration': 3216314, 'execution': {'resolvers': [{'duration': 2869655, 'startOffset': 320361, 'dgraph': [{'duration': 2684096, 'label': 'query', 'startOffset': 460951}], 'returnType': '[Location]', 'parentType': 'Query', 'path': ['queryLocation'], 'fieldName': 'queryLocation'}]}, 'startTime': '2020-10-29T10:21:49.818045846Z', 'endTime': '2020-10-29T10:21:49.821262205Z', 'version': 1}}}, 'id': 'c84f7b64e7a64617b6a0739d259ce4ff'}
{'type': 'data', 'payload': {'data': {'queryLocation': [{'friendlyName': 'office2', 'locationId': 'office'}]}, 'extensions': {'touched_uids': 3, 'tracing': {'duration': 3168727, 'execution': {'resolvers': [{'duration': 2990981, 'startOffset': 153429, 'dgraph': [{'duration': 2847034, 'label': 'query', 'startOffset': 252671}], 'returnType': '[Location]', 'parentType': 'Query', 'path': ['queryLocation'], 'fieldName': 'queryLocation'}]}, 'startTime': '2020-10-29T10:21:49.911864914Z', 'endTime': '2020-10-29T10:21:49.915033721Z', 'version': 1}}}, 'id': 'c84f7b64e7a64617b6a0739d259ce4ff'}
{'type': 'data', 'payload': {'data': {'queryLocation': [{'friendlyName': 'office2', 'locationId': 'office'}]}, 'extensions': {'touched_uids': 3, 'tracing': {'duration': 3168727, 'execution': {'resolvers': [{'duration': 2990981, 'startOffset': 153429, 'dgraph': [{'duration': 2847034, 'label': 'query', 'startOffset': 252671}], 'returnType': '[Location]', 'parentType': 'Query', 'path': ['queryLocation'], 'fieldName': 'queryLocation'}]}, 'startTime': '2020-10-29T10:21:49.911864914Z', 'endTime': '2020-10-29T10:21:49.915033721Z', 'version': 1}}}, 'id': '6541f04ba9e943a2b2ced579a5f641d2'}
{'type': 'data', 'payload': {'data': {'queryLocation': [{'friendlyName': 'office2', 'locationId': 'office'}]}, 'extensions': {'touched_uids': 3, 'tracing': {'duration': 4390070, 'execution': {'resolvers': [{'duration': 4190194, 'startOffset': 170317, 'dgraph': [{'duration': 3737908, 'label': 'query', 'startOffset': 557567}], 'returnType': '[Location]', 'parentType': 'Query', 'path': ['queryLocation'], 'fieldName': 'queryLocation'}]}, 'startTime': '2020-10-29T10:21:49.914058243Z', 'endTime': '2020-10-29T10:21:49.918448261Z', 'version': 1}}}, 'id': '6541f04ba9e943a2b2ced579a5f641d2'}
{'type': 'data', 'payload': {'data': {'queryLocation': [{'friendlyName': 'office2', 'locationId': 'office'}]}, 'extensions': {'touched_uids': 3, 'tracing': {'duration': 4390070, 'execution': {'resolvers': [{'duration': 4190194, 'startOffset': 170317, 'dgraph': [{'duration': 3737908, 'label': 'query', 'startOffset': 557567}], 'returnType': '[Location]', 'parentType': 'Query', 'path': ['queryLocation'], 'fieldName': 'queryLocation'}]}, 'startTime': '2020-10-29T10:21:49.914058243Z', 'endTime': '2020-10-29T10:21:49.918448261Z', 'version': 1}}}, 'id': 'c84f7b64e7a64617b6a0739d259ce4ff'}
{'type': 'data', 'payload': {'data': {'queryLocation': [{'friendlyName': 'office2', 'locationId': 'office'}]}, 'extensions': {'touched_uids': 3, 'tracing': {'duration': 3263691, 'execution': {'resolvers': [{'duration': 3039078, 'startOffset': 196924, 'dgraph': [{'duration': 2854201, 'label': 'query', 'startOffset': 309736}], 'returnType': '[Location]', 'parentType': 'Query', 'path': ['queryLocation'], 'fieldName': 'queryLocation'}]}, 'startTime': '2020-10-29T10:21:50.522073703Z', 'endTime': '2020-10-29T10:21:50.525337628Z', 'version': 1}}}, 'id': '6541f04ba9e943a2b2ced579a5f641d2'}
{'type': 'data', 'payload': {'data': {'queryLocation': [{'friendlyName': 'office2', 'locationId': 'office'}]}, 'extensions': {'touched_uids': 3, 'tracing': {'duration': 3263691, 'execution': {'resolvers': [{'duration': 3039078, 'startOffset': 196924, 'dgraph': [{'duration': 2854201, 'label': 'query', 'startOffset': 309736}], 'returnType': '[Location]', 'parentType': 'Query', 'path': ['queryLocation'], 'fieldName': 'queryLocation'}]}, 'startTime': '2020-10-29T10:21:50.522073703Z', 'endTime': '2020-10-29T10:21:50.525337628Z', 'version': 1}}}, 'id': 'c84f7b64e7a64617b6a0739d259ce4ff'}