示例#1
0
def test_circular_dependency():
    class A(slothql.Object):
        b = slothql.Field(lambda: B)
        field = slothql.String()

    class B(slothql.Object):
        a = slothql.Field(A)
        field = slothql.String()

    class Query(slothql.Object):
        root = slothql.Field(A,
                             resolver=lambda: {'b': {
                                 'a': {
                                     'field': 'foo'
                                 }
                             }})

    schema = slothql.Schema(Query)

    query = 'query { root { b { a { field } } } }'
    assert {
        'data': {
            'root': {
                'b': {
                    'a': {
                        'field': 'foo'
                    }
                }
            }
        }
    } == slothql.gql(schema, query)
示例#2
0
def test_gql__exception_not_handled():
    def resolver(*_):
        raise RuntimeError

    class Query(slothql.Object):
        hello = slothql.String(resolver=resolver)

    with pytest.raises(RuntimeError):
        print(slothql.gql(slothql.Schema(query=Query), 'query { hello }'))
示例#3
0
def test_duplicate_references():
    class A(slothql.Object):
        field = slothql.String()

    class Query(slothql.Object):
        a1 = slothql.Field(A)
        a2 = slothql.Field(A)

    slothql.Schema(query=Query)
示例#4
0
def test_list_field(resolver, expected):
    class Query(slothql.Object):
        list = slothql.Integer(resolver=resolver, many=True)

    schema = slothql.Schema(query=Query)

    assert {
        'data': {
            'list': expected
        }
    } == slothql.gql(schema, 'query { list }')
示例#5
0
def test_nested_model_query():
    class A(slothql.Object):
        field = slothql.String()

    class B(slothql.Object):
        a = slothql.Field(A)

    class Query(slothql.Object):
        def get_b(self, obj, info):
            return {'a': {'field': 'resolved'}}

        b = slothql.Field(B, resolver=get_b)

    schema = slothql.Schema(query=Query)

    assert {
        'data': {
            'b': {
                'a': {
                    'field': 'resolved'
                }
            }
        }
    } == slothql.gql(schema, 'query { b { a { field } } }')
示例#6
0
def test_gql__syntax_error(query):
    class Query(slothql.Object):
        hello = slothql.String(resolver=lambda *_: 'world')

    result = gql(slothql.Schema(query=Query), query)
    assert result.get('errors')
示例#7
0
def test_gql__valid_query(query):
    class Query(slothql.Object):
        hello = slothql.String(resolver=lambda *_: 'world')

    result = gql(slothql.Schema(query=Query), query)
    assert {'data': {'hello': 'world'}} == result
示例#8
0
    def test_nested_in_null(self):
        class Nested(slothql.Object):
            nested = slothql.Field(self.query_class(), resolver=lambda *_: None)

        query = 'query { nested { hello } }'
        assert {'nested': None} == graphql(slothql.Schema(query=Nested), query).data
示例#9
0
    def test_complex_schema(self, call):
        class Nested(slothql.Object):
            nested = slothql.Field(self.query_class() if call else self.query_class, lambda *_: {'world': 'not hello'})

        query = 'query { nested { hello } }'
        assert {'nested': {'hello': 'world'}} == graphql(slothql.Schema(query=Nested), query).data
示例#10
0
 def test_execution(self):
     schema = slothql.Schema(query=self.query_class)
     query = 'query { hello }'
     assert 'world' == graphql(schema, query).data['hello']
示例#11
0
 def test_can_init_with_callable_query(self):
     slothql.Schema(query=self.query_class)
示例#12
0
 def test_can_init(self):
     slothql.Schema(query=self.query_class())