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_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 execute_query(self, query: str) -> dict: return gql(self.get_schema(), query)