From bf3919825b9daa42bc723cb7fbd0c9a755be3719 Mon Sep 17 00:00:00 2001 From: Wojciech Trocki Date: Sun, 19 Jan 2020 12:38:52 +0000 Subject: [PATCH 1/2] feat: Ignore sorting for queries and mutations when printing schema --- src/utilities/schemaPrinter.js | 8 +++++- src/utilities/typeComparators.js | 48 ++++++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+), 1 deletion(-) diff --git a/src/utilities/schemaPrinter.js b/src/utilities/schemaPrinter.js index 4edcc19dbb..3c8319b202 100644 --- a/src/utilities/schemaPrinter.js +++ b/src/utilities/schemaPrinter.js @@ -87,7 +87,13 @@ function printFilteredSchema( const directives = schema.getDirectives().filter(directiveFilter); const typeMap = schema.getTypeMap(); const types = objectValues(typeMap) - .sort((type1, type2) => type1.name.localeCompare(type2.name)) + .sort((type1, type2) => { + if (isMutationType(schema, type1)) return -1; + if (isQueryType(schema, type1)) return -1; + if (isSubscriptionType(schema, type1)) return -1; + + return type1.name.localeCompare(type2.name); + }) .filter(typeFilter); return ( diff --git a/src/utilities/typeComparators.js b/src/utilities/typeComparators.js index 18e4447812..9432ada116 100644 --- a/src/utilities/typeComparators.js +++ b/src/utilities/typeComparators.js @@ -120,3 +120,51 @@ export function doTypesOverlap( // Otherwise the types do not overlap. return false; } + +/** + * Checks if type is query + * + * @internal + */ +export function isQueryType(schema: GraphQLSchema, type: GraphQLCompositeType) { + const schemaType = schema.getQueryType(); + if (schemaType) { + return schemaType.name === type.name; + } + + return false; +} + +/** + * Checks if type is mutation + * + * @internal + */ +export function isMutationType( + schema: GraphQLSchema, + type: GraphQLCompositeType, +) { + const schemaType = schema.getMutationType(); + if (schemaType) { + return schemaType.name === type.name; + } + + return false; +} + +/** + * Checks if type is subscription + * + * @internal + */ +export function isSubscriptionType( + schema: GraphQLSchema, + type: GraphQLCompositeType, +) { + const schemaType = schema.getSubscriptionType(); + if (schemaType) { + return schemaType.name === type.name; + } + + return false; +} From 0912c06453b5d4864945bcace4a33cb1f02d93ce Mon Sep 17 00:00:00 2001 From: Wojciech Trocki Date: Thu, 23 Jan 2020 09:55:11 +0000 Subject: [PATCH 2/2] BREAKING: remove sorting by name from printSchema --- src/utilities/schemaPrinter.js | 10 +------ src/utilities/typeComparators.js | 48 -------------------------------- 2 files changed, 1 insertion(+), 57 deletions(-) diff --git a/src/utilities/schemaPrinter.js b/src/utilities/schemaPrinter.js index 3c8319b202..3889f10285 100644 --- a/src/utilities/schemaPrinter.js +++ b/src/utilities/schemaPrinter.js @@ -86,15 +86,7 @@ function printFilteredSchema( ): string { const directives = schema.getDirectives().filter(directiveFilter); const typeMap = schema.getTypeMap(); - const types = objectValues(typeMap) - .sort((type1, type2) => { - if (isMutationType(schema, type1)) return -1; - if (isQueryType(schema, type1)) return -1; - if (isSubscriptionType(schema, type1)) return -1; - - return type1.name.localeCompare(type2.name); - }) - .filter(typeFilter); + const types = objectValues(typeMap).filter(typeFilter); return ( [printSchemaDefinition(schema)] diff --git a/src/utilities/typeComparators.js b/src/utilities/typeComparators.js index 9432ada116..18e4447812 100644 --- a/src/utilities/typeComparators.js +++ b/src/utilities/typeComparators.js @@ -120,51 +120,3 @@ export function doTypesOverlap( // Otherwise the types do not overlap. return false; } - -/** - * Checks if type is query - * - * @internal - */ -export function isQueryType(schema: GraphQLSchema, type: GraphQLCompositeType) { - const schemaType = schema.getQueryType(); - if (schemaType) { - return schemaType.name === type.name; - } - - return false; -} - -/** - * Checks if type is mutation - * - * @internal - */ -export function isMutationType( - schema: GraphQLSchema, - type: GraphQLCompositeType, -) { - const schemaType = schema.getMutationType(); - if (schemaType) { - return schemaType.name === type.name; - } - - return false; -} - -/** - * Checks if type is subscription - * - * @internal - */ -export function isSubscriptionType( - schema: GraphQLSchema, - type: GraphQLCompositeType, -) { - const schemaType = schema.getSubscriptionType(); - if (schemaType) { - return schemaType.name === type.name; - } - - return false; -}