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
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()
# 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 } } """ # Our callback function, which will be called and passed data everytime new data is available
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) print(f'subscription stopped. waiting {wait} seconds to close the connection') time.sleep(wait)
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 }