Ejemplo n.º 1
0
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')
Ejemplo n.º 2
0
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)
Ejemplo n.º 3
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)
Ejemplo n.º 4
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'}
Ejemplo n.º 5
0
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']
Ejemplo n.º 6
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')
Ejemplo n.º 7
0
    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()