@@ -137,61 +137,58 @@ protected static function printFilteredSchema(Schema $schema, callable $directiv
137
137
return \implode ("\n\n" , \array_filter ($ elements )) . "\n" ;
138
138
}
139
139
140
- protected static function printSchemaDefinition (Schema $ schema ): string
140
+ protected static function printSchemaDefinition (Schema $ schema ): ? string
141
141
{
142
- if (static ::isSchemaOfCommonNames ($ schema )) {
143
- return '' ;
144
- }
145
-
146
- $ operationTypes = [];
147
-
148
142
$ queryType = $ schema ->getQueryType ();
149
- if ($ queryType !== null ) {
150
- $ operationTypes [] = " query: {$ queryType ->name }" ;
151
- }
152
-
153
143
$ mutationType = $ schema ->getMutationType ();
154
- if ($ mutationType !== null ) {
155
- $ operationTypes [] = " mutation: {$ mutationType ->name }" ;
156
- }
157
-
158
144
$ subscriptionType = $ schema ->getSubscriptionType ();
159
- if ($ subscriptionType !== null ) {
160
- $ operationTypes [] = " subscription: {$ subscriptionType ->name }" ;
145
+
146
+ // Special case: When a schema has no root operation types, no valid schema
147
+ // definition can be printed.
148
+ if ($ queryType === null && $ mutationType === null && $ subscriptionType === null ) {
149
+ return null ;
161
150
}
162
151
163
- $ typesString = \implode ("\n" , $ operationTypes );
152
+ // TODO add condition for schema.description
153
+ // Only print a schema definition if there is a description or if it should
154
+ // not be omitted because of having default type names.
155
+ if (! self ::hasDefaultRootOperationTypes ($ schema )) {
156
+ return "schema { \n"
157
+ . ($ queryType !== null ? " query: {$ queryType ->name }\n" : '' )
158
+ . ($ mutationType !== null ? " mutation: {$ mutationType ->name }\n" : '' )
159
+ . ($ subscriptionType !== null ? " subscription: {$ subscriptionType ->name }\n" : '' )
160
+ . '} ' ;
161
+ }
164
162
165
- return " schema { \n{ $ typesString }\n } " ;
163
+ return null ;
166
164
}
167
165
168
166
/**
169
167
* GraphQL schema define root types for each type of operation. These types are
170
168
* the same as any other type and can be named in any manner, however there is
171
169
* a common naming convention:.
172
170
*
171
+ * ```graphql
173
172
* schema {
174
173
* query: Query
175
174
* mutation: Mutation
175
+ * subscription: Subscription
176
176
* }
177
+ * ```
177
178
*
178
179
* When using this naming convention, the schema description can be omitted.
180
+ * When using this naming convention, the schema description can be omitted so
181
+ * long as these names are only used for operation types.
182
+ *
183
+ * Note however that if any of these default names are used elsewhere in the
184
+ * schema but not as a root operation type, the schema definition must still
185
+ * be printed to avoid ambiguity.
179
186
*/
180
- protected static function isSchemaOfCommonNames (Schema $ schema ): bool
187
+ protected static function hasDefaultRootOperationTypes (Schema $ schema ): bool
181
188
{
182
- $ queryType = $ schema ->getQueryType ();
183
- if ($ queryType !== null && $ queryType ->name !== 'Query ' ) {
184
- return false ;
185
- }
186
-
187
- $ mutationType = $ schema ->getMutationType ();
188
- if ($ mutationType !== null && $ mutationType ->name !== 'Mutation ' ) {
189
- return false ;
190
- }
191
-
192
- $ subscriptionType = $ schema ->getSubscriptionType ();
193
-
194
- return $ subscriptionType === null || $ subscriptionType ->name === 'Subscription ' ;
189
+ return $ schema ->getQueryType () === $ schema ->getType ('Query ' )
190
+ && $ schema ->getMutationType () === $ schema ->getType ('Mutation ' )
191
+ && $ schema ->getSubscriptionType () === $ schema ->getType ('Subscription ' );
195
192
}
196
193
197
194
/**
0 commit comments