@@ -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
/**
@@ -107,25 +107,20 @@ function printSchemaDefinition(schema: GraphQLSchema): Maybe<string> {
107
107
* }
108
108
* ```
109
109
*
110
- * When using this naming convention, the schema description can be omitted.
110
+ * When using this naming convention, the schema description can be omitted so
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.
111
116
*/
112
- function isSchemaOfCommonNames ( schema : GraphQLSchema ) : boolean {
113
- const queryType = schema . getQueryType ( ) ;
114
- if ( queryType && queryType . name !== 'Query' ) {
115
- return false ;
116
- }
117
-
118
- const mutationType = schema . getMutationType ( ) ;
119
- if ( mutationType && mutationType . name !== 'Mutation' ) {
120
- return false ;
121
- }
122
-
123
- const subscriptionType = schema . getSubscriptionType ( ) ;
124
- if ( subscriptionType && subscriptionType . name !== 'Subscription' ) {
125
- return false ;
126
- }
127
-
128
- return true ;
117
+ function hasDefaultRootOperationTypes ( schema : GraphQLSchema ) : boolean {
118
+ /* eslint-disable eqeqeq */
119
+ return (
120
+ schema . getQueryType ( ) == schema . getType ( 'Query' ) &&
121
+ schema . getMutationType ( ) == schema . getType ( 'Mutation' ) &&
122
+ schema . getSubscriptionType ( ) == schema . getType ( 'Subscription' )
123
+ ) ;
129
124
}
130
125
131
126
export function printType ( type : GraphQLNamedType ) : string {
0 commit comments