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 test_query(): client = GraphQLClient(GRAPHQL_API) id = 'clayallsopp' result = client.query(''' query { hn2 { nodeFromHnId(id: "clayallsopp", isUserId: true) { id ...on HackerNewsV2User { hnId } } } } ''') assert 'data' in result assert result['data']['hn2']['nodeFromHnId']['hnId'] == id
def test_query_with_variables(): client = GraphQLClient(GRAPHQL_API) id = 'clayallsopp' result = client.query( ''' query ($id: String!) { hn2 { nodeFromHnId(id: $id, isUserId: true) { id ...on HackerNewsV2User { hnId } } } } ''', {'id': id}) assert 'data' in result assert result['data']['hn2']['nodeFromHnId']['hnId'] == id
# Consider using constructor for gcp uploader gcp_uploader.remove_old_files(os.environ[BUCKET_NAME_ENV], os.environ[DESTINATION_BLOB_NAME_ENV]) # required travel_search_file = get_arg(1) # optional stop_times_file = get_arg_default_value(2, None) graphite_reporter = GraphiteReporter() graphql_endpoint = get_env(GRAPHQL_ENDPOINT_ENV, DEFAULT_GRAPHQL_ENDPOINT) client = GraphQLClient(graphql_endpoint) travel_search_date = get_env(TRAVEL_SEARCH_DATE_TIME, DEFAULT_TRAVEL_SEARCH_DATE_TIME) print("Using datetime: " + travel_search_date) travel_search_executor = TravelSearchExecutor(client, graphite_reporter) stop_times_executor = StopTimesExecutor(client, graphite_reporter, travel_search_date) if BUCKET_NAME_ENV not in os.environ or DESTINATION_BLOB_NAME_ENV not in os.environ: print( "Environment variables not set: BUCKET_NAME and DESTINATION_BLOB_NAME. Will not upload reports to gcp" ) upload_gcp = False else:
from graphql_client import GraphQLClient ws = GraphQLClient('ws://localhost:8000/graphql') query = """ query{ { allPosts{ edges{ node{ title body author{ username } } } } } } """ res = ws.query(query) print(res) ws.close()
import time from graphql_client import GraphQLClient ws = GraphQLClient('ws://localhost:5000/subscriptions') 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")
import time from graphql_client import GraphQLClient # some sample GraphQL server which supports websocket transport and subscription client = GraphQLClient('ws://localhost:9001') # Simple Query Example # query example with GraphQL variables query = """ query getUser($userId: Int!) { users (id: $userId) { id username } } """ # This is a blocking call, you receive response in the `res` variable print('Making a query first') res = client.query(query, variables={'userId': 2}) print('query result', res) # Subscription Example subscription_query = """ subscription getUser { users (id: 2) { id username
def getSocket(): return GraphQLClient('ws://localhost:4000/graphql')
from graphql_client import GraphQLClient from conf import badgeTemplateUri, serverUrl from printerStatus import getPrinters import gqlWs # start up sequence (APP starter) print(f'ws://{serverUrl}/graphql-ws') ws = GraphQLClient(f'ws://{serverUrl}/graphql') query = """ subscription { printRequested{ id success } } """ availablePrinters = getPrinters() def newData(ws,messege): availablePrinters[0].print("file:///home/boris/Documents/PrinterHub/test.html","badge-test","png") print(messege) # callback function gqlWs.start(newData)
sub = """ subscription { articles { id title } } """ 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)
def __init__(self, stop_id): from graphql_client import GraphQLClient self.client = GraphQLClient( 'https://api.digitransit.fi/routing/v1/routers/hsl/index/graphql') self.stop_id = stop_id
class Stop: def __init__(self, stop_id): from graphql_client import GraphQLClient self.client = GraphQLClient( 'https://api.digitransit.fi/routing/v1/routers/hsl/index/graphql') self.stop_id = stop_id def update(self): query_string = ''' { stop(id: "%s") { gtfsId name lat lon stoptimesWithoutPatterns { scheduledDeparture realtimeDeparture departureDelay realtime realtimeState serviceDay headsign trip { pattern { code } routeShortName alerts { alertHeaderText } directionId } } patterns { code directionId headsign route { gtfsId shortName longName mode } } } } ''' % self.stop_id result = self.client.query(query_string) return self.parse_result(result) def parse_result(self, result): data = result.get("data") stop = data.get("stop") stop_name = stop.get("name") stop_activities = stop.get("stoptimesWithoutPatterns") departures = list(map(self.parse_stoptime, stop_activities)) departures = list(filter(lambda x: x is not None, departures)) return departures def parse_stoptime(self, stoptime): headsign = stoptime.get("headsign") departuretime_timestamp = stoptime.get("serviceDay") + stoptime.get( "realtimeDeparture") departuretime = datetime.fromtimestamp(departuretime_timestamp) delay = stoptime.get("departureDelay") route = stoptime.get("trip").get("routeShortName") if headsign is not None: return { 'sign': headsign, 'departure': departuretime, 'delay': delay, 'route': route, 'timestamp': departuretime_timestamp }
from graphql_client import GraphQLClient from time import gmtime, strftime from datetime import timedelta import os.path import configparser endpoint_uri = "http://yourplatforminstance.com/graphql" client = GraphQLClient(endpoint_uri) def post_graphql_query(gql_query): gql_query = inject_utc_date_time(gql_query) print("Sending: " + gql_query) result = client.query(gql_query) print("Response: " + str(result)) return result def inject_utc_date_time(gql_query): start_time = strftime("%Y-%m-%dT%H:%M:%S", gmtime()) gql_query = gql_query.replace("UTCStart", str(start_time)) return gql_query