Skip to content

Commit 917f970

Browse files
committed
Attempt to simplify and rename things
Moves the "no operation types" special case up to the caller. Renames the function to match the spec text
1 parent ac3cb10 commit 917f970

File tree

1 file changed

+26
-38
lines changed

1 file changed

+26
-38
lines changed

src/utilities/printSchema.ts

Lines changed: 26 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -70,28 +70,28 @@ function printFilteredSchema(
7070
}
7171

7272
function printSchemaDefinition(schema: GraphQLSchema): Maybe<string> {
73-
if (schema.description == null && isSchemaOfCommonNames(schema)) {
74-
return;
75-
}
76-
77-
const operationTypes = [];
78-
7973
const queryType = schema.getQueryType();
80-
if (queryType) {
81-
operationTypes.push(` query: ${queryType.name}`);
82-
}
83-
8474
const mutationType = schema.getMutationType();
85-
if (mutationType) {
86-
operationTypes.push(` mutation: ${mutationType.name}`);
87-
}
88-
8975
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;
9281
}
9382

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+
}
9595
}
9696

9797
/**
@@ -109,29 +109,17 @@ function printSchemaDefinition(schema: GraphQLSchema): Maybe<string> {
109109
*
110110
* When using this naming convention, the schema description can be omitted so
111111
* 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.
112116
*/
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 */
131119
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')
135123
);
136124
}
137125

0 commit comments

Comments
 (0)