@@ -70,51 +70,59 @@ def print_filtered_schema(
7070
7171def print_schema_definition (schema : GraphQLSchema ) -> Optional [str ]:
7272 """Print GraphQL schema definitions."""
73- if schema .description is None and is_schema_of_common_names (schema ):
74- return None
75-
76- operation_types = []
77-
7873 query_type = schema .query_type
79- if query_type :
80- operation_types .append (f" query: { query_type .name } " )
81-
8274 mutation_type = schema .mutation_type
83- if mutation_type :
84- operation_types .append (f" mutation: { mutation_type .name } " )
85-
8675 subscription_type = schema .subscription_type
87- if subscription_type :
88- operation_types .append (f" subscription: { subscription_type .name } " )
8976
90- return print_description (schema ) + "schema {\n " + "\n " .join (operation_types ) + "\n }"
77+ # Special case: When a schema has no root operation types, no valid schema
78+ # definition can be printed.
79+ if not query_type and not mutation_type and not subscription_type :
80+ return None
81+
82+ # Only print a schema definition if there is a description or if it should
83+ # not be omitted because of having default type names.
84+ if schema .description or not has_default_root_operation_types (schema ):
85+ return (
86+ print_description (schema )
87+ + "schema {\n "
88+ + (f" query: { query_type .name } \n " if query_type else "" )
89+ + (f" mutation: { mutation_type .name } \n " if mutation_type else "" )
90+ + (
91+ f" subscription: { subscription_type .name } \n "
92+ if subscription_type
93+ else ""
94+ )
95+ + "}"
96+ )
97+
98+ return None
9199
92100
93- def is_schema_of_common_names (schema : GraphQLSchema ) -> bool :
94- """Check whether this schema uses the common naming convention .
101+ def has_default_root_operation_types (schema : GraphQLSchema ) -> bool :
102+ """Check whether a schema uses the default root operation type names .
95103
96104 GraphQL schema define root types for each type of operation. These types are the
97105 same as any other type and can be named in any manner, however there is a common
98- naming convention:
106+ naming convention::
99107
100- schema {
101- query: Query
102- mutation: Mutation
103- subscription: Subscription
104- }
108+ schema {
109+ query: Query
110+ mutation: Mutation
111+ subscription: Subscription
112+ }
105113
106- When using this naming convention, the schema description can be omitted.
107- """
108- query_type = schema .query_type
109- if query_type and query_type .name != "Query" :
110- return False
111-
112- mutation_type = schema .mutation_type
113- if mutation_type and mutation_type .name != "Mutation" :
114- return False
114+ When using this naming convention, the schema description can be omitted so
115+ long as these names are only used for operation types.
115116
116- subscription_type = schema .subscription_type
117- return not subscription_type or subscription_type .name == "Subscription"
117+ Note however that if any of these default names are used elsewhere in the
118+ schema but not as a root operation type, the schema definition must still
119+ be printed to avoid ambiguity.
120+ """
121+ return (
122+ schema .query_type is schema .get_type ("Query" )
123+ and schema .mutation_type is schema .get_type ("Mutation" )
124+ and schema .subscription_type is schema .get_type ("Subscription" )
125+ )
118126
119127
120128def print_type (type_ : GraphQLNamedType ) -> str :
0 commit comments