def test_empty_nodes(): graph = Graph([ Node('Foo', []), Root([]), ]) with pytest.raises(ValueError) as err: apply(graph, [GraphQLIntrospection(graph)]) assert err.match('No fields in the Foo node') assert err.match('No fields in the Root node')
def test_with_pass_context(graph_name, sample_count): def root_fields1(fields): return [1 for _ in fields] @pass_context def root_fields2(ctx, fields): return [2 for _ in fields] graph = Graph([ Root([ Field('a', None, root_fields1), Field('b', None, root_fields2), ]), ]) graph = apply(graph, [GraphMetrics(graph_name)]) assert sample_count('Root', 'a') is None assert sample_count('Root', 'b') is None result = Engine(SyncExecutor()).execute( graph, q.Node([ q.Field('a'), q.Field('b'), ])) check_result(result, { 'a': 1, 'b': 2, }) assert sample_count('Root', 'a') == 1.0 assert sample_count('Root', 'b') == 1.0
def test_invalid_names(): graph = Graph([ Node('Baz-Baz', [ Field('bzz-bzz', Integer, _noop), ]), Root([ Field('foo-foo', Integer, _noop, options=[Option('bar-bar', Integer)]), Link('baz-baz', Sequence[TypeRef['Baz-Baz']], _noop, requires='foo-foo'), ]), ]) with pytest.raises(ValueError) as err: apply(graph, [GraphQLIntrospection(graph)]) assert err.match('bzz-bzz') assert err.match('foo-foo') assert err.match('bar-bar') assert err.match('baz-baz') assert err.match('Baz-Baz')
def introspect(graph): engine = Engine(SyncExecutor()) graph = apply(graph, [GraphQLIntrospection()]) query = read(QUERY) errors = validate(graph, query) assert not errors norm_result = engine.execute(graph, query) return denormalize(graph, norm_result, query)
async def test_simple_async(graph_name, sample_count): async def x_fields(fields, ids): return [[42 for _ in fields] for _ in ids] async def root_fields(fields): return [1 for _ in fields] async def x_link(): return 2 ll_graph = Graph([ Node('X', [ Field('id', None, x_fields), ]), ]) x_sg = SubGraph(ll_graph, 'X') hl_graph = Graph([ Node('X', [ Field('id', None, x_sg), ]), Root([ Field('a', None, root_fields), Link('x', TypeRef['X'], x_link, requires=None), ]), ]) hl_graph = apply(hl_graph, [AsyncGraphMetrics(graph_name)]) query = q.Node([ q.Field('a'), q.Link('x', q.Node([ q.Field('id'), ])), ]) engine = Engine(AsyncIOExecutor()) assert sample_count('Root', 'a') is None assert sample_count('Root', 'x') is None assert sample_count('X', 'id') is None result = await engine.execute(hl_graph, query) check_result(result, { 'a': 1, 'x': { 'id': 42, }, }) assert sample_count('Root', 'a') == 1.0 assert sample_count('Root', 'x') == 1.0 assert sample_count('X', 'id') == 1.0
def introspect(query_graph, mutation_graph=None): engine = Engine(SyncExecutor()) query_graph = apply(query_graph, [ GraphQLIntrospection(query_graph, mutation_graph), ]) query = read(QUERY) errors = validate(query_graph, query) assert not errors norm_result = engine.execute(query_graph, query) return denormalize(query_graph, norm_result)
def test_sync_introspection(): graph = GRAPH from hiku.graph import apply from hiku.introspection.graphql import GraphQLIntrospection graph = apply(graph, [GraphQLIntrospection()]) query = read('{ __typename }') result = hiku_engine.execute(graph, query) simple_result = denormalize(graph, result, query) assert simple_result == {'__typename': 'Root'}
def test_typename(): graph = apply(GRAPH, [GraphQLIntrospection()]) assert graph.root.fields_map['__typename'].type is String assert graph.root.fields_map['__typename'].func([None]) == ['Root'] decian = graph.nodes_map['decian'] assert decian.fields_map['__typename'].type is String assert decian.fields_map['__typename'].func([None]) == ['decian'] flexed = graph.nodes_map['flexed'] assert flexed.fields_map['__typename'].type is String assert flexed.fields_map['__typename'].func([None]) == ['flexed']
async def test_async_introspection(event_loop): from hiku.executors.asyncio import AsyncIOExecutor from hiku.introspection.graphql import MakeAsync graph = MakeAsync().visit(GRAPH) async_hiku_engine = Engine(AsyncIOExecutor(event_loop)) from hiku.graph import apply from hiku.introspection.graphql import AsyncGraphQLIntrospection graph = apply(graph, [AsyncGraphQLIntrospection()]) query = read('{ __typename }') result = await async_hiku_engine.execute(graph, query) simple_result = denormalize(graph, result, query) assert simple_result == {'__typename': 'Root'}
from unittest import TestCase from hiku.federation.endpoint import FederatedGraphQLEndpoint from hiku.federation.engine import Engine from hiku.federation.introspection import FederatedGraphQLIntrospection from hiku.federation.sdl import print_sdl from hiku.executors.sync import SyncExecutor from hiku.graph import apply from tests.test_federation.utils import GRAPH INTROSPECTED_GRAPH = apply(GRAPH, [ FederatedGraphQLIntrospection(GRAPH), ]) def execute(graph, query_string): graphql_endpoint = FederatedGraphQLEndpoint( Engine(SyncExecutor()), graph, ) return graphql_endpoint.dispatch(query_string) class TestSDL(TestCase): def test_print_graph_sdl(self): sdl = print_sdl(GRAPH) expected = [ 'scalar Any', '', 'type Status {', ' id: Int!', ' title: String!', '}', '',
try: query = read(data['query'], data.get('variables')) errors = validate(app.config['GRAPH'], query) if errors: result = {'errors': [{'message': e} for e in errors]} else: result = hiku_engine.execute(app.config['GRAPH'], query, ctx=app.config['HIKU_CTX']) result = {'data': denormalize(app.config['GRAPH'], result, query)} except Exception as err: result = {'errors': [{'message': repr(err)}]} return jsonify(result) if __name__ == "__main__": sa_engine = create_engine('sqlite://', connect_args={'check_same_thread': False}, poolclass=StaticPool) setup_db(sa_engine) app.config['HIKU_ENGINE'] = Engine(SyncExecutor()) app.config['HIKU_CTX'] = {SA_ENGINE_KEY: sa_engine} graph = get_graph(get_queries(hiku.sources.sqlalchemy, SA_ENGINE_KEY, SyncQueries)) graph = apply(graph, [GraphQLIntrospection()]) app.config['GRAPH'] = graph app.run()
GRAPH = Graph([ Root([ Field('foo', String, foo_field_func), ]), ]) async def handler(request): hiku_engine = request.app['HIKU_ENGINE'] data = await request.json() try: query = read(data['query'], data.get('variables')) errors = validate(request.app['GRAPH'], query) if errors: result = {'errors': [{'message': e} for e in errors]} else: result = await hiku_engine.execute(request.app['GRAPH'], query) result = {'data': denormalize(request.app['GRAPH'], result, query)} except Exception as err: result = {'errors': [{'message': repr(err)}]} return web.json_response(result) if __name__ == "__main__": app = web.Application() app.router.add_post('/', handler) app['HIKU_ENGINE'] = Engine(AsyncIOExecutor(asyncio.get_event_loop())) app['GRAPH'] = apply(GRAPH, [AsyncGraphQLIntrospection()]) web.run_app(app)