def test_missing_variables(): with pytest.raises(TypeError) as err: read(""" query Belinda($asides: Int!) { ebonics } """) err.match('Variable "asides" is not provided for query Belinda')
def test_duplicated_fragment_names(): with pytest.raises(TypeError) as err: read(""" query Pelota { sinope } fragment Splosh on Makai { saggier } fragment Splosh on Whether { refits } """) err.match('Duplicated fragment name: "Splosh"')
def test(dt): dt.now = Mock(return_value=_NOW) query = read('{ now }') result = hiku_engine.execute(GRAPH, query) simple_result = denormalize(GRAPH, result, query) assert simple_result == {'now': '2015-10-21T07:28:00'}
def test_query(dt): dt.now = Mock(return_value=_NOW) query = read('{ now }') result = hiku_engine.execute(GRAPH, query) simple_result = denormalize(GRAPH, result, query) assert simple_result == {'now': '2015-10-21T07:28:00'}
def test_reference_cycle_in_fragments(): with pytest.raises(TypeError) as err: read(""" query Suckle { roguish ...Pakol } fragment Pakol on Crees { fatuous ...Varlet } fragment Varlet on Bribee { penfold ...Pakol } """) err.match('Cyclic fragment usage: "Pakol"')
def test(self): with reqs_eq_patcher(): self.assertEqual( read(""" { hello } """), Edge([Field('hello')]), )
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)
def test_typename(): graph = Graph([ Node('Bar', [ Field('baz', Integer, _), ]), Node('Foo', [ Link('bar', Sequence[TypeRef['Bar']], _, requires=None), ]), Root([ Link('foo', TypeRef['Foo'], _, requires=None), ]), ]) query = read(""" query { __typename foo { __typename bar { __typename baz } } } """) index = Index() index[ROOT.node][ROOT.ident].update({ 'foo': Reference('Foo', 1), }) index['Foo'][1].update({ 'bar': [Reference('Bar', 2), Reference('Bar', 3)], }) index['Bar'][2].update({ 'baz': 42 }) index['Bar'][3].update({ 'baz': 43 }) result = Proxy(index, ROOT, query) assert DenormalizeGraphQL(graph, result, 'Query').process(query) == { '__typename': 'Query', 'foo': { '__typename': 'Foo', 'bar': [ { '__typename': 'Bar', 'baz': 42, }, { '__typename': 'Bar', 'baz': 43, }, ], }, }
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_undefined_variables(): with pytest.raises(TypeError) as err: read(""" { selma(djin: $geeky) } """) err.match(r'Variable \$geeky is not defined in query <unnamed>') with pytest.raises(TypeError) as err: read(""" query Oriolus { ve(sac: $bd) } """) err.match(r'Variable \$bd is not defined in query Oriolus') with pytest.raises(TypeError) as err: read(""" query Had { ...Fulgent } fragment Fulgent on Ashlee { chewie(newton: $aliyah) } """) err.match(r'Variable \$aliyah is not defined in query Had')
def test_strip(): query = read(""" query { __typename foo { __typename bar { __typename baz } } } """) assert _StripQuery().visit(query) == read(""" query { foo { bar { baz } } } """)
def test_directive_invalid_arguments(directive): with pytest.raises( TypeError, match=('@{} directive accepts exactly one argument, 2 provided'. format(directive))): read( """ {{ foo bar @{}(if: true, extra: true) }} """.format(directive), ) with pytest.raises( TypeError, match=('@{} directive does not accept "unknown" argument'.format( directive))): read( """ {{ foo bar @{}(unknown: true) }} """.format(directive), )
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)
def handler(): hiku_engine = app.config['HIKU_ENGINE'] data = request.get_json() 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)
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'}
def check_read(source, query, variables=None): parsed_query = read(source, variables) with reqs_eq_patcher(): assert parsed_query == query
def test_multiple_operations(): with pytest.raises(TypeError) as err: read('{ gummed } { calce } { aaron }') err.match('Only single operation per document is supported, ' '3 operations was provided')
def test_mutation_operation(): with pytest.raises(TypeError) as err: read('mutation { doSomething(kokam: "screens") }') err.match('Only "query" operations are supported, "mutation" operation ' 'was provided')
def check_read(source, query, variables=None): parsed_query = read(source, variables) assert parsed_query == query
def test_multiple_operations(): with pytest.raises(TypeError) as err: read('{ gummed } { calce } { aaron }') err.match('Duplicate operation definition: None')
def test_field_alias(): with pytest.raises(TypeError) as err: read('{ glamors: foule }') err.match('Field aliases are not supported')