@@ -70,28 +70,28 @@ function printFilteredSchema(
70
70
}
71
71
72
72
function printSchemaDefinition ( schema : GraphQLSchema ) : Maybe < string > {
73
- if ( schema . description == null && isSchemaOfCommonNames ( schema ) ) {
74
- return ;
75
- }
76
-
77
- const operationTypes = [ ] ;
78
-
79
73
const queryType = schema . getQueryType ( ) ;
80
- if ( queryType ) {
81
- operationTypes . push ( ` query: ${ queryType . name } ` ) ;
82
- }
83
-
84
74
const mutationType = schema . getMutationType ( ) ;
85
- if ( mutationType ) {
86
- operationTypes . push ( ` mutation: ${ mutationType . name } ` ) ;
87
- }
88
-
89
75
const subscriptionType = schema . getSubscriptionType ( ) ;
90
- if ( subscriptionType ) {
91
- operationTypes . push ( ` subscription: ${ subscriptionType . name } ` ) ;
76
+
77
+ // Special case: When a schema has no root operation types, no valid schema
78
+ // definition can be printed.
79
+ if ( ! queryType && ! mutationType && ! subscriptionType ) {
80
+ return ;
92
81
}
93
82
94
- return printDescription ( schema ) + `schema {\n${ operationTypes . join ( '\n' ) } \n}` ;
83
+ // Only print a schema definition if there is a description or if it should
84
+ // not be omitted because of having default type names.
85
+ if ( schema . description || ! hasDefaultRootOperationTypes ( schema ) ) {
86
+ return (
87
+ printDescription ( schema ) +
88
+ 'schema {\n' +
89
+ ( queryType ? ` query: ${ queryType . name } \n` : '' ) +
90
+ ( mutationType ? ` mutation: ${ mutationType . name } \n` : '' ) +
91
+ ( subscriptionType ? ` subscription: ${ subscriptionType . name } \n` : '' ) +
92
+ '}'
93
+ ) ;
94
+ }
95
95
}
96
96
97
97
/**
@@ -109,29 +109,17 @@ function printSchemaDefinition(schema: GraphQLSchema): Maybe<string> {
109
109
*
110
110
* When using this naming convention, the schema description can be omitted so
111
111
* long as these names are only used for operation types.
112
+ *
113
+ * Note however that if any of these default names are used elsewhere in the
114
+ * schema but not as a root operation type, the schema definition must still
115
+ * be printed to avoid ambiguity.
112
116
*/
113
- function isSchemaOfCommonNames ( schema : GraphQLSchema ) : boolean {
114
- const queryOperationType = schema . getQueryType ( ) ?? null ;
115
- const mutationOperationType = schema . getMutationType ( ) ?? null ;
116
- const subscriptionOperationType = schema . getSubscriptionType ( ) ?? null ;
117
-
118
- // Special case for when there are no operation types
119
- if (
120
- ! queryOperationType &&
121
- ! mutationOperationType &&
122
- ! subscriptionOperationType
123
- ) {
124
- return true ;
125
- }
126
-
127
- const queryType = schema . getType ( 'Query' ) ?? null ;
128
- const mutationType = schema . getType ( 'Mutation' ) ?? null ;
129
- const subscriptionType = schema . getType ( 'Subscription' ) ?? null ;
130
-
117
+ function hasDefaultRootOperationTypes ( schema : GraphQLSchema ) : boolean {
118
+ /* eslint-disable eqeqeq */
131
119
return (
132
- queryOperationType === queryType &&
133
- mutationOperationType === mutationType &&
134
- subscriptionOperationType === subscriptionType
120
+ schema . getQueryType ( ) == schema . getType ( 'Query' ) &&
121
+ schema . getMutationType ( ) == schema . getType ( 'Mutation' ) &&
122
+ schema . getSubscriptionType ( ) == schema . getType ( 'Subscription' )
135
123
) ;
136
124
}
137
125
0 commit comments