Skip to content

Commit 09f9c8d

Browse files
committed
print_schema: Fix printing of empty types
Replicates graphql/graphql-js@ebec940
1 parent e5660a3 commit 09f9c8d

File tree

3 files changed

+49
-46
lines changed

3 files changed

+49
-46
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ a query language for APIs created by Facebook.
1313
[![Code Style](https://img.shields.io/badge/code%20style-black-000000.svg)](https://github.com/ambv/black)
1414

1515
The current version 1.0.2 of GraphQL-core-next is up-to-date with GraphQL.js version
16-
14.1.1. All parts of the API are covered by an extensive test suite of currently 1725
16+
14.1.1. All parts of the API are covered by an extensive test suite of currently 1726
1717
unit tests.
1818

1919

graphql/utilities/schema_printer.py

Lines changed: 22 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -153,74 +153,51 @@ def print_object(type_: GraphQLObjectType) -> str:
153153
)
154154
return (
155155
print_description(type_)
156-
+ f"type {type_.name}{implemented_interfaces} "
157-
+ "{\n"
156+
+ f"type {type_.name}{implemented_interfaces}"
158157
+ print_fields(type_)
159-
+ "\n}"
160158
)
161159

162160

163161
def print_interface(type_: GraphQLInterfaceType) -> str:
164-
return (
165-
print_description(type_)
166-
+ f"interface {type_.name} "
167-
+ "{\n"
168-
+ print_fields(type_)
169-
+ "\n}"
170-
)
162+
return print_description(type_) + f"interface {type_.name}" + print_fields(type_)
171163

172164

173165
def print_union(type_: GraphQLUnionType) -> str:
174-
return (
175-
print_description(type_)
176-
+ f"union {type_.name} = "
177-
+ " | ".join(t.name for t in type_.types)
178-
)
166+
types = type_.types
167+
possible_types = " = " + " | ".join(t.name for t in types) if types else ""
168+
return print_description(type_) + f"union {type_.name}" + possible_types
179169

180170

181171
def print_enum(type_: GraphQLEnumType) -> str:
182-
return (
183-
print_description(type_)
184-
+ f"enum {type_.name} "
185-
+ "{\n"
186-
+ print_enum_values(type_.values)
187-
+ "\n}"
188-
)
189-
190-
191-
def print_enum_values(values: Dict[str, GraphQLEnumValue]) -> str:
192-
return "\n".join(
172+
values = [
193173
print_description(value, " ", not i) + f" {name}" + print_deprecated(value)
194-
for i, (name, value) in enumerate(values.items())
195-
)
174+
for i, (name, value) in enumerate(type_.values.items())
175+
]
176+
return print_description(type_) + f"enum {type_.name}" + print_block(values)
196177

197178

198179
def print_input_object(type_: GraphQLInputObjectType) -> str:
199-
fields = type_.fields.items()
200-
return (
201-
print_description(type_)
202-
+ f"input {type_.name} "
203-
+ "{\n"
204-
+ "\n".join(
205-
print_description(field, " ", not i)
206-
+ " "
207-
+ print_input_value(name, field)
208-
for i, (name, field) in enumerate(fields)
209-
)
210-
+ "\n}"
211-
)
180+
fields = [
181+
print_description(field, " ", not i) + " " + print_input_value(name, field)
182+
for i, (name, field) in enumerate(type_.fields.items())
183+
]
184+
return print_description(type_) + f"input {type_.name}" + print_block(fields)
212185

213186

214187
def print_fields(type_: Union[GraphQLObjectType, GraphQLInterfaceType]) -> str:
215-
fields = type_.fields.items()
216-
return "\n".join(
188+
fields = [
217189
print_description(field, " ", not i)
218190
+ f" {name}"
219191
+ print_args(field.args, " ")
220192
+ f": {field.type}"
221193
+ print_deprecated(field)
222-
for i, (name, field) in enumerate(fields)
223-
)
194+
for i, (name, field) in enumerate(type_.fields.items())
195+
]
196+
return print_block(fields)
197+
198+
199+
def print_block(items: List[str]) -> str:
200+
return " {\n" + "\n".join(items) + "\n}" if items else ""
224201

225202

226203
def print_args(args: Dict[str, GraphQLArgument], indentation="") -> str:

tests/utilities/test_schema_printer.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -437,6 +437,32 @@ def prints_enum():
437437
"""
438438
)
439439

440+
def prints_empty_types():
441+
schema = GraphQLSchema(
442+
types=[
443+
GraphQLEnumType("SomeEnum", {}),
444+
GraphQLInputObjectType("SomeInputObject", {}),
445+
GraphQLInterfaceType("SomeInterface", {}),
446+
GraphQLObjectType("SomeObject", {}),
447+
GraphQLUnionType("SomeUnion", []),
448+
]
449+
)
450+
451+
output = print_for_test(schema)
452+
assert output == dedent(
453+
"""
454+
enum SomeEnum
455+
456+
input SomeInputObject
457+
458+
interface SomeInterface
459+
460+
type SomeObject
461+
462+
union SomeUnion
463+
"""
464+
)
465+
440466
def prints_custom_directives():
441467
custom_directive = GraphQLDirective(
442468
name="customDirective", locations=[DirectiveLocation.FIELD]

0 commit comments

Comments
 (0)