10000 print_schema: correctly print empty description · graphql-python/graphql-core@4659d0a · GitHub
[go: up one dir, main page]

Skip to content

Commit 4659d0a

Browse files
committed
print_schema: correctly print empty description
Replicates graphql/graphql-js@3cf08e6
1 parent 5899a61 commit 4659d0a

File tree

3 files changed

+108
-10
lines changed

3 files changed

+108
-10
lines changed

src/graphql/utilities/extend_schema.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,13 @@ def extend_schema_args(
245245

246246
# Then produce and return the kwargs for a Schema with these types.
247247
get_operation = operation_types.get
248+
description = (
249+
schema_def.description.value
250+
if schema_def and schema_def.description
251+
else None
252+
)
253+
if description is None:
254+
description = schema_kwargs["description"]
248255
return GraphQLSchemaKwargs(
249256
query=get_operation(OperationType.QUERY), # type: ignore
250257
mutation=get_operation(OperationType.MUTATION), # type: ignore
@@ -255,12 +262,7 @@ def extend_schema_args(
255262
for directive in schema_kwargs["directives"]
256263
)
257264
+ tuple(self.build_directive(directive) for directive in directive_defs),
258-
description=(
259-
schema_def.description.value
260-
if schema_def and schema_def.description
261-
else None
262-
)
263-
or schema_kwargs["description"],
265+
description=description,
264266
extensions=schema_kwargs["extensions"],
265267
ast_node=schema_def or schema_kwargs["ast_node"],
266268
extension_ast_nodes=schema_kwargs["extension_ast_nodes"]

src/graphql/utilities/print_schema.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ def print_schema_definition(schema: GraphQLSchema) -> str | None:
8383

8484
# Only print a schema definition if there is a description or if it should
8585
# not be omitted because of having default type names.
86-
if schema.description or not has_default_root_operation_types(schema):
86+
if not (schema.description is None and has_default_root_operation_types(schema)):
8787
return (
8888
print_description(schema)
8989
+ "schema {\n"
@@ -235,7 +235,7 @@ def print_args(args: dict[str, GraphQLArgument], indentation: str = "") -> str:
235235
return ""
236236

237237
# If every arg does not have a description, print them on one line.
238-
if not any(arg.description for arg in args.values()):
238+
if all(arg.description is None for arg in args.values()):
239239
return (
240240
"("
241241
+ ", ".join(print_input_value(name, arg) for name, arg in args.items())

tests/utilities/test_print_schema.py

Lines changed: 98 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
GraphQLBoolean,
99
GraphQLDirective,
1010
GraphQLEnumType,
11+
GraphQLEnumValue,
1112
GraphQLField,
1213
GraphQLFloat,
1314
GraphQLInputField,
@@ -602,13 +603,108 @@ def prints_custom_directives():
602603
)
603604

604605
def prints_an_empty_description():
605-
schema = build_single_field_schema(GraphQLField(GraphQLString, description=""))
606+
args = {
607+
"someArg": GraphQLArgument(GraphQLString, description=""),
608+
"anotherArg": GraphQLArgument(GraphQLString, description=""),
609+
}
610+
fields = {
611+
"someField": GraphQLField(GraphQLString, args, description=""),
612+
"anotherField": GraphQLField(GraphQLString, args, description=""),
613+
}
614+
query_type = GraphQLObjectType("Query", fields, description="")
615+
scalar_type = GraphQLScalarType("SomeScalar", description="")
616+
interface_type = GraphQLInterfaceType("SomeInterface", fields, description="")
617+
union_type = GraphQLUnionType("SomeUnion", [query_type], description="")
618+
enum_type = GraphQLEnumType(
619+
"SomeEnum",
620+
{
621+
"SOME_VALUE": GraphQLEnumValue("Some Value", description=""),
622+
"ANOTHER_VALUE": GraphQLEnumValue("Another Value", description=""),
623+
},
624+
description="",
625+
)
626+
some_directive = GraphQLDirective(
627+
"someDirective", [DirectiveLocation.QUERY], args, description=""
628+
)
629+
630+
schema = GraphQLSchema(
631+
query_type,
632+
types=[scalar_type, interface_type, union_type, enum_type],
633+
directives=[some_directive],
634+
description="",
635+
)
606636

607637
assert expect_printed_schema(schema) == dedent(
608638
'''
639+
""""""
640+
schema {
641+
query: Query
642+
}
643+
644+
""""""
645+
directive @someDirective(
646+
""""""
647+
someArg: String
648+
649+
""""""
650+
anotherArg: String
651+
) on QUERY
652+
653+
""""""
654+
scalar SomeScalar
655+
656+
""""""
657+
interface SomeInterface {
658+
""""""
659+
someField(
660+
""""""
661+
someArg: String
662+
663+
""""""
664+
anotherArg: String
665+
): String
666+
667+
""""""
668+
anotherField(
669+
""""""
670+
someArg: String
671+
672+
""""""
673+
anotherArg: String
674+
): String
675+
}
676+
677+
""""""
678+
union SomeUnion = Query
679+
680+
""""""
609681
type Query {
610682
""""""
611-
singleField: String
683+
someField(
684+
""""""
685+
someArg: String
686+
687+
""""""
688+
anotherArg: String
689+
): String
690+
691+
""""""
692+
anotherField(
693+
""""""
694+
someArg: String
695+
696+
""""""
697+
anotherArg: String
698+
): String
699+
}
700+
701+
""""""
702+
enum SomeEnum {
703+
""""""
704+
SOME_VALUE
705+
706+
""""""
707+
ANOTHER_VALUE
612708
}
613709
'''
614710
)

0 commit comments

Comments
 (0)
0