Beispiel #1
0
async def test_async_batch_endpoint(async_graph):
    endpoint = AsyncBatchGraphQLEndpoint(Engine(AsyncIOExecutor()),
                                         async_graph)

    assert await endpoint.dispatch([]) == []

    result = await endpoint.dispatch({'query': '{answer}'})
    assert result == {'data': {'answer': '42'}}

    batch_result = await endpoint.dispatch([
        {
            'query': '{answer}'
        },
        {
            'query': '{__typename}'
        },
    ])
    assert batch_result == [
        {
            'data': {
                'answer': '42'
            }
        },
        {
            'data': {
                '__typename': 'Query'
            }
        },
    ]
Beispiel #2
0
async def execute_async(graph, query_dict):
    graphql_endpoint = AsyncFederatedGraphQLEndpoint(
        Engine(AsyncIOExecutor()),
        graph,
    )

    return await graphql_endpoint.dispatch(query_dict)
 async def _check(self, src, value):
     sa_engine = create_async_engine(self.db_dsn)
     engine = Engine(AsyncIOExecutor())
     try:
         result = await engine.execute(self.graph, read(src),
                                       {SA_ENGINE_KEY: sa_engine})
         check_result(result, value)
     finally:
         await greenlet_spawn(sa_engine.sync_engine.dispose)
Beispiel #4
0
 async def _check(self, src, value):
     sa_engine = await aiopg.sa.create_engine(self.db_dsn, minsize=0)
     engine = Engine(AsyncIOExecutor())
     try:
         result = await engine.execute(self.graph, read(src),
                                       {SA_ENGINE_KEY: sa_engine})
         check_result(result, value)
     finally:
         sa_engine.close()
         await sa_engine.wait_closed()
Beispiel #5
0
def main():
    logging.basicConfig()
    app = web.Application()
    app.add_routes([
        web.post('/graphql', handle_graphql),
    ])
    app['graphql-endpoint'] = AsyncGraphQLEndpoint(
        Engine(AsyncIOExecutor()), QUERY_GRAPH, MUTATION_GRAPH,
    )
    web.run_app(app)
Beispiel #6
0
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
Beispiel #7
0
async def test_awaitable_check(event_loop):
    executor = AsyncIOExecutor(event_loop)

    with pytest.raises(TypeError) as func_err:
        executor.submit(func)
    func_err.match('returned non-awaitable object')

    with pytest.raises(TypeError) as func2_err:
        executor.submit(func2)
    func2_err.match('returned non-awaitable object')

    with pytest.raises(TypeError) as gen_err:
        executor.submit(gen)
    gen_err.match('returned non-awaitable object')

    with pytest.raises(TypeError) as gen2_err:
        executor.submit(gen2)
    gen2_err.match('returned non-awaitable object')

    assert (await executor.submit(coroutine)) == 'smiting'
Beispiel #8
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'}
Beispiel #9
0
async def test_actor_to_character(db_dsn, event_loop):
    hiku_engine = Engine(AsyncIOExecutor(event_loop))
    async with aiopg.sa.create_engine(db_dsn, loop=event_loop) as sa_engine:
        result = await execute(hiku_engine, sa_engine, GRAPH,
                               '[{:actors [:name {:character [:name]}]}]')
        assert result == {
            'actors': [
                {
                    'name': 'William Shatner',
                    'character': {
                        'name': 'James T. Kirk'
                    },
                },
                {
                    'name': 'Leonard Nimoy',
                    'character': {
                        'name': 'Spock'
                    },
                },
                {
                    'name': 'DeForest Kelley',
                    'character': {
                        'name': 'Leonard McCoy'
                    },
                },
                {
                    'name': 'Chris Pine',
                    'character': {
                        'name': 'James T. Kirk'
                    },
                },
                {
                    'name': 'Zachary Quinto',
                    'character': {
                        'name': 'Spock'
                    },
                },
                {
                    'name': 'Karl Urban',
                    'character': {
                        'name': 'Leonard McCoy'
                    },
                },
            ],
        }
Beispiel #10
0
async def test_async_endpoint(async_graph):
    endpoint = AsyncGraphQLEndpoint(Engine(AsyncIOExecutor()), async_graph)
    result = await endpoint.dispatch({'query': '{answer}'})
    assert result == {'data': {'answer': '42'}}
Beispiel #11
0
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)
Beispiel #12
0
async def execute_async_executor(graph, query_, ctx=None):
    engine = Engine(AsyncIOExecutor())
    return await engine.execute(graph, query_, ctx=ctx)