def subscribe_to_algorithm(converter_algo_instance, callbackMethod): converter_algo_status_subscription = """ subscription{ algorithmInstanceUpdated(filter:{ algorithmInstanceIds: "%s" }){ id progress state } } """ % (converter_algo_instance) global ws ws = GraphQLClient(ws_url) convereter_algo_subscription = ws.subscribe( converter_algo_status_subscription, callback=callbackMethod) return convereter_algo_subscription
def callback(_id, data): print("got new data..") print(f"msg id: {_id}. data: {data}") query = """ subscription{ countSeconds(upTo: 10) } """ query = """ subscription{ randomInt{ booler } } """ sub_id = ws.subscribe(query, callback=callback) while True: # sub_id = ws.subscribe(query, callback=callback) time.sleep(5) print("test") # later stop the subscription ws.stop_subscribe(sub_id) ws.close()
res = client.query(query, variables={'userId': 2}) print('query result', res) # Subscription Example subscription_query = """ subscription getUser { users (id: 2) { id username } } """ # Our callback function, which will be called and passed data everytime new data is available def my_callback(op_id, data): print(f"Got data for Operation ID: {op_id}. Data: {data}") print('Making a graphql subscription now...') sub_id = client.subscribe(subscription_query, callback=my_callback) print( 'Created subscription and waiting. Callback function is called whenever there is new data' ) # do some operation while the subscription is running... time.sleep(10) client.stop_subscribe(sub_id) client.close()
} } """ def cb(id, data): print('got new data: ', data) print('starting web socket client') #websocket.enableTrace(True) ws = GraphQLClient('ws://localhost:8080/v1alpha1/graphql') res = ws.query(q) print(res) res = ws.query(q1) print(res) subalive = 10 wait = 40 id = ws.subscribe(sub, callback=cb) print(f'started subscriptions, will keep it alive for {subalive} seconds') time.sleep(subalive) print(f'{subalive} seconds over, stopping the subscription') ws.stop_subscribe(id) print(f'subscription stopped. waiting {wait} seconds to close the connection') time.sleep(wait) ws.close()