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 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()
} } """ 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()