diff --git a/gqlrequests/builder.py b/gqlrequests/builder.py index 86c9b1c..6e37a99 100644 --- a/gqlrequests/builder.py +++ b/gqlrequests/builder.py @@ -39,6 +39,13 @@ def __setattr__(cls, name, value): super().__setattr__("_resolved_fields", old_fields) + def __str__(cls): + output = f"type {cls.__name__} {{\n" + for key, value in cls._resolved_fields.items(): + output += f" {key}: {value.__name__}\n" + output += "}\n" + return output + class QueryBuilder(metaclass=QueryBuilderMeta): """An abstract class used to build GraphQL queries. diff --git a/tests/test_creating_builder.py b/tests/test_creating_builder.py index 4fee826..02adf10 100644 --- a/tests/test_creating_builder.py +++ b/tests/test_creating_builder.py @@ -84,4 +84,38 @@ class InvalidType(gqlrequests.QueryBuilder): def test_invalid_type_throws_value_error(): with pytest.raises(ValueError) as e: - InvalidType().build() \ No newline at end of file + InvalidType().build() + + +def test_string_representation_of_class_shows_graphql_syntax(): + class EveryType(gqlrequests.QueryBuilder): + id: int + age: int + money: float + name: str + company: bool + + correct_string = """ +type EveryType { + id: int + age: int + money: float + name: str + company: bool +} +"""[1:] + assert str(EveryType) == correct_string + +def test_string_representation_of_nested_class_shows_graphql_syntax(): + correct_string = """ +type NestedType { + something: SomethingType +} +"""[1:] + class SomethingType(gqlrequests.QueryBuilder): + id: int + + class NestedType(gqlrequests.QueryBuilder): + something: SomethingType + + assert str(NestedType) == correct_string \ No newline at end of file