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)
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 }'))
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)
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 }')
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 } } }')
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')
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
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
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
def test_execution(self): schema = slothql.Schema(query=self.query_class) query = 'query { hello }' assert 'world' == graphql(schema, query).data['hello']
def test_can_init_with_callable_query(self): slothql.Schema(query=self.query_class)
def test_can_init(self): slothql.Schema(query=self.query_class())