Skip to content

Commit

Permalink
Attempt to simplify and rename things
Browse files Browse the repository at this point in the history
Moves the "no operation types" special case up to the caller. Renames
the function to match the spec text
  • Loading branch information
leebyron committed Feb 9, 2023
1 parent ac3cb10 commit 917f970
Showing 1 changed file with 26 additions and 38 deletions.
64 changes: 26 additions & 38 deletions src/utilities/printSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,28 +70,28 @@ function printFilteredSchema(
}

function printSchemaDefinition(schema: GraphQLSchema): Maybe<string> {
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` : '') +
'}'
);
}
}

/**
Expand All @@ -109,29 +109,17 @@ function printSchemaDefinition(schema: GraphQLSchema): Maybe<string> {
*
* 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')
);
}

Expand Down

0 comments on commit 917f970

Please sign in to comment.