def test_no_inherit_object(): class Query: i = 1 with pytest.raises(SchemaError) as e: make_schema(Query) assert "Did you forget to inherit Object?" in str(e.value)
def test_input_type(): @dataclass class SubInput: aList: List[int] @dataclass class MyInput: anInt: int aStr: str aSubInput: SubInput class Query(Object): def serialize(self, i: MyInput) -> str: return json.dumps(asdict(i), sort_keys=True, indent=2) schema = make_schema(Query) result = graphql( schema, ''' query { serialize(i: { anInt: 1, aStr: "asdf", aSubInput: { aList: [1, 2, 3] } }) }''') assert not result.errors assert result.data['serialize'] == ''' { "aStr": "asdf", "aSubInput": { "aList": [ 1, 2, 3 ] }, "anInt": 1 } '''.strip()
def test_property(): class Query(Object): @property def anInt(self) -> int: return 1 schema = make_schema(Query) result = graphql(schema, 'query { anInt }', root=Query()) assert not result.errors assert result.data == dict(anInt=1)
def schema(): yield make_schema(query=Query)
def test_unnamed_union(): class Query(Object): requiredFoo: MyUnion with pytest.raises(SchemaError) as e: make_schema(Query)
def schema(): yield make_schema(query=Query, scalars=[DateTime, JSON])
class Date(graphotype.Scalar): t = datetime _format = '%Y-%m-%d %H:%M:%S' @classmethod def serialize(cls, instance: datetime) -> str: return instance.strftime(cls._format) @classmethod def parse(cls, value: str) -> datetime: return datetime.strptime(value, cls._format) MyUnion = Union[Foo, Bar] schema = graphotype.make_schema(query=Query, mutation=None, scalars=[Date]) if __name__ == '__main__': print(graphql.print_schema(schema)) query = ''' query { c(d: true, e: 1.0) { a c d } isGiraffes(g: GIRAFFES) unionReturner { ...on Bar { a