Skip to content

Commit bf39198

Browse files
committed
feat: Ignore sorting for queries and mutations when printing schema
1 parent 9dba58e commit bf39198

File tree

2 files changed

+55
-1
lines changed

2 files changed

+55
-1
lines changed

src/utilities/schemaPrinter.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,13 @@ function printFilteredSchema(
8787
const directives = schema.getDirectives().filter(directiveFilter);
8888
const typeMap = schema.getTypeMap();
8989
const types = objectValues(typeMap)
90-
.sort((type1, type2) => type1.name.localeCompare(type2.name))
90+
.sort((type1, type2) => {
91+
if (isMutationType(schema, type1)) return -1;
92+
if (isQueryType(schema, type1)) return -1;
93+
if (isSubscriptionType(schema, type1)) return -1;
94+
95+
return type1.name.localeCompare(type2.name);
96+
})
9197
.filter(typeFilter);
9298

9399
return (

src/utilities/typeComparators.js

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,3 +120,51 @@ export function doTypesOverlap(
120120
// Otherwise the types do not overlap.
121121
return false;
122122
}
123+
124+
/**
125+
* Checks if type is query
126+
*
127+
* @internal
128+
*/
129+
export function isQueryType(schema: GraphQLSchema, type: GraphQLCompositeType) {
130+
const schemaType = schema.getQueryType();
131+
if (schemaType) {
132+
return schemaType.name === type.name;
133+
}
134+
135+
return false;
136+
}
137+
138+
/**
139+
* Checks if type is mutation
140+
*
141+
* @internal
142+
*/
143+
export function isMutationType(
144+
schema: GraphQLSchema,
145+
type: GraphQLCompositeType,
146+
) {
147+
const schemaType = schema.getMutationType();
148+
if (schemaType) {
149+
return schemaType.name === type.name;
150+
}
151+
152+
return false;
153+
}
154+
155+
/**
156+
* Checks if type is subscription
157+
*
158+
* @internal
159+
*/
160+
export function isSubscriptionType(
161+
schema: GraphQLSchema,
162+
type: GraphQLCompositeType,
163+
) {
164+
const schemaType = schema.getSubscriptionType();
165+
if (schemaType) {
166+
return schemaType.name === type.name;
167+
}
168+
169+
return false;
170+
}

0 commit comments

Comments
 (0)