From 917f9701e2efd0daccf867dbd3ec8468dfaed09e Mon Sep 17 00:00:00 2001 From: Lee Byron Date: Wed, 8 Feb 2023 16:39:55 -0800 Subject: [PATCH] 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 --- src/utilities/printSchema.ts | 64 +++++++++++++++--------------------- 1 file changed, 26 insertions(+), 38 deletions(-) diff --git a/src/utilities/printSchema.ts b/src/utilities/printSchema.ts index 55a1758140c..0b9b1638d77 100644 --- a/src/utilities/printSchema.ts +++ b/src/utilities/printSchema.ts @@ -70,28 +70,28 @@ function printFilteredSchema( } function printSchemaDefinition(schema: GraphQLSchema): Maybe { - if (schema.description == null && isSchemaOfCommonNames(schema)) { - return; - } - - const operationTypes = []; - const queryType = schema.getQueryType(); - if (queryType) { - operationTypes.push(` query: ${queryType.name}`); - } - const mutationType = schema.getMutationType(); - if (mutationType) { - operationTypes.push(` mutation: ${mutationType.name}`); - } - const subscriptionType = schema.getSubscriptionType(); - if (subscriptionType) { - operationTypes.push(` subscription: ${subscriptionType.name}`); + + // Special case: When a schema has no root operation types, no valid schema + // definition can be printed. + if (!queryType && !mutationType && !subscriptionType) { + return; } - return printDescription(schema) + `schema {\n${operationTypes.join('\n')}\n}`; + // Only print a schema definition if there is a description or if it should + // not be omitted because of having default type names. + if (schema.description || !hasDefaultRootOperationTypes(schema)) { + return ( + printDescription(schema) + + 'schema {\n' + + (queryType ? ` query: ${queryType.name}\n` : '') + + (mutationType ? ` mutation: ${mutationType.name}\n` : '') + + (subscriptionType ? ` subscription: ${subscriptionType.name}\n` : '') + + '}' + ); + } } /** @@ -109,29 +109,17 @@ function printSchemaDefinition(schema: GraphQLSchema): Maybe { * * When using this naming convention, the schema description can be omitted so * long as these names are only used for operation types. + * + * Note however that if any of these default names are used elsewhere in the + * schema but not as a root operation type, the schema definition must still + * be printed to avoid ambiguity. */ -function isSchemaOfCommonNames(schema: GraphQLSchema): boolean { - const queryOperationType = schema.getQueryType() ?? null; - const mutationOperationType = schema.getMutationType() ?? null; - const subscriptionOperationType = schema.getSubscriptionType() ?? null; - - // Special case for when there are no operation types - if ( - !queryOperationType && - !mutationOperationType && - !subscriptionOperationType - ) { - return true; - } - - const queryType = schema.getType('Query') ?? null; - const mutationType = schema.getType('Mutation') ?? null; - const subscriptionType = schema.getType('Subscription') ?? null; - +function hasDefaultRootOperationTypes(schema: GraphQLSchema): boolean { + /* eslint-disable eqeqeq */ return ( - queryOperationType === queryType && - mutationOperationType === mutationType && - subscriptionOperationType === subscriptionType + schema.getQueryType() == schema.getType('Query') && + schema.getMutationType() == schema.getType('Mutation') && + schema.getSubscriptionType() == schema.getType('Subscription') ); }