Example #1
0
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'}
Example #2
0
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 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)
Example #4
0
def index():
    fn = lookup.get('index/view')
    query_str = str(fn.query())
    print('QUERY:', query_str)

    hiku_query = read(query_str)
    result = engine.execute(GRAPH, hiku_query,
                            ctx=hiku_engine_ctx)
    print('RESULT:')
    pprint(denormalize(GRAPH, result, hiku_query))
    return fn.render(result)
Example #5
0
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 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)
Example #7
0
def test_denormalize_non_merged_query():
    index = Index()
    index.root.update({
        'x': Reference('X', 'xN'),
    })
    index['X']['xN'].update({
        'a': 1,
        'b': 2,
    })
    index.finish()
    graph = Graph([
        Node('X', [
            Field('a', None, None),
            Field('b', None, None),
        ]),
        Root([
            Link('x', TypeRef['X'], lambda: 'xN', requires=None),
        ]),
    ])
    non_merged_query = hiku_query.Node([
        hiku_query.Link('x', hiku_query.Node([
            hiku_query.Field('a'),
        ])),
        hiku_query.Link('x', hiku_query.Node([
            hiku_query.Field('b'),
        ])),
    ])

    with pytest.raises(KeyError) as err:
        denormalize(graph, Proxy(index, ROOT, non_merged_query))
    err.match("Field u?'a' wasn't requested in the query")

    merged_query = merge([non_merged_query])
    assert denormalize(graph, Proxy(index, ROOT, merged_query)) == {
        'x': {
            'a': 1,
            'b': 2
        },
    }
Example #8
0
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)
Example #9
0
def test_falsy_link_result():
    x_fields = Mock(return_value=[[42]])
    graph = Graph([
        Node('X', [
            Field('a', None, x_fields),
        ]),
        Root([
            Link('x', TypeRef['X'], lambda: 0, requires=None),
        ]),
    ])
    result = execute(graph, q.Node([
        q.Link('x', q.Node([q.Field('a')])),
    ]))
    assert denormalize(graph, result) == {'x': {'a': 42}}
    x_fields.assert_called_once_with([q.Field('a')], [0])
Example #10
0
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'}
Example #11
0
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)
Example #12
0
def test_denormalize_with_alias():
    index = Index()
    index.root.update({
        'x': Reference('X', 'xN'),
    })
    index['X']['xN'].update({
        'a': 1,
        'b': 2,
    })
    index.finish()

    graph = Graph([
        Node('X', [
            Field('a', None, None),
            Field('b', None, None),
        ]),
        Root([
            Link('x', TypeRef['X'], lambda: 'xN', requires=None),
        ]),
    ])

    query = hiku_query.Node([
        hiku_query.Link('x',
                        hiku_query.Node([
                            hiku_query.Field('a', alias='a1'),
                        ]),
                        alias='x1'),
        hiku_query.Link('x',
                        hiku_query.Node([
                            hiku_query.Field('b', alias='b1'),
                        ]),
                        alias='x2'),
    ])

    result = Proxy(index, ROOT, query)

    assert denormalize(graph, result) == {
        'x1': {
            'a1': 1
        },
        'x2': {
            'b1': 2
        },
    }
Example #13
0
def test_denormalize_data_type():
    index = Index()
    index.root.update({'foo': {'a': 42}})
    index.finish()
    graph = Graph([
        Root([
            Field('foo', TypeRef['Foo'], None),
        ]),
    ],
                  data_types={
                      'Foo': Record[{
                          'a': Integer
                      }],
                  })
    query = hiku_query.Node([
        hiku_query.Link('foo', hiku_query.Node([
            hiku_query.Field('a'),
        ])),
    ])
    assert denormalize(graph, Proxy(index, ROOT, query)) == {'foo': {'a': 42}}
Example #14
0
def test():
    for query in [query_graphql(), query_simple(), query_python()]:
        hiku_engine = Engine(SyncExecutor())
        result = hiku_engine.execute(GRAPH, query)
        result = denormalize(GRAPH, result, query)
        assert result == \
        {
            "characters": [
                {
                    "name": "James T. Kirk",
                    "species": "Human"
                },
                {
                    "name": "Spock",
                    "species": "Vulcan/Human"
                },
                {
                    "name": "Leonard McCoy",
                    "species": "Human"
                }
            ]
        }
Example #15
0
def execute(graph, query_string):
    query = read(query_string)
    result = hiku_engine.execute(graph, query)
    return denormalize(graph, result)
Example #16
0
def check_result(query_string, result):
    new_result = denormalize(GRAPH, RESULT, read(query_string))
    json.dumps(new_result)  # using json to check for circular references
    assert new_result == result
Example #17
0
def execute(graph, query_string):
    query = read(query_string)
    result = hiku_engine.execute(graph, query, {SA_ENGINE_KEY: sa_engine})
    return denormalize(graph, result)
Example #18
0
async def execute(hiku_engine, sa_engine, graph, query_string):
    query = read(query_string)
    result = await hiku_engine.execute(graph, query,
                                       {SA_ENGINE_KEY: sa_engine})
    return denormalize(graph, result, query)
Example #19
0
def check_result(query_string, result):
    new_result = denormalize(GRAPH, RESULT, read(query_string))
    json.dumps(new_result)  # using json to check for circular references
    assert new_result == result
Example #20
0
def execute(graph, query_string):
    query = read(query_string)
    result = hiku_engine.execute(graph, query, {SA_ENGINE_KEY: sa_engine})
    return denormalize(graph, result, query)
Example #21
0
def check_result(query_string, result):
    query = merge([read(query_string)])
    new_result = denormalize(GRAPH, get_result(query))
    json.dumps(new_result)  # using json to check for circular references
    assert new_result == result
Example #22
0
def execute(graph, query_string):
    query = read(query_string)
    result = hiku_engine.execute(graph, query)
    return denormalize(graph, result, query)
Example #23
0
def graph_endpoint():
    request_data = request.data.decode("utf-8")
    request_data = read(request_data)
    result = hiku_engine.execute(GRAPH, request_data)
    result = denormalize(GRAPH, result, request_data)
    return jsonify(result)