-
Notifications
You must be signed in to change notification settings - Fork 2k
Lexographically sorted printSchema for comparison/diff stability? #941
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
For anyone who needs this functionality for whatever reason; here's an ugly hack to tide you over, currently it sorts fields, args and enums via duck-typing: const { parse, buildASTSchema } = require("graphql");
const { printSchema } = require("graphql/utilities");
const printSchemaOrdered = originalSchema => {
// Clone schema so we don't damage anything
const schema = buildASTSchema(parse(printSchema(originalSchema)));
const typeMap = schema.getTypeMap();
Object.keys(typeMap).forEach(name => {
const Type = typeMap[name];
// Object?
if (Type.getFields) {
const fields = Type.getFields();
const keys = Object.keys(fields).sort();
keys.forEach(key => {
const value = fields[key];
// Move the key to the end of the object
delete fields[key];
fields[key] = value;
// Sort args
if (value.args) {
value.args.sort((a, b) => a.name.localeCompare(b.name));
}
});
}
// Enum?
if (Type.getValues) {
Type.getValues().sort((a, b) => a.name.localeCompare(b.name));
}
});
return printSchema(schema);
}; |
The order of fields and arguments is often meaningful. I don't think such a feature should be part of Feel free to send a PR if you're still interested, though I'm closing this issue for repo maintenance. |
@leebyron I'm already working on utility function to sort the result of introspection query. |
printSchema
already sorts the type names alphabetically:graphql-js/src/utilities/schemaPrinter.js
Line 74 in ff4338d
It would also be useful for diffing if fields, arguments, enum values and various other entities were also sorted alphabetically. I have performed some local modifications and it seems the changes required are quite minor. However, I believe it's possibly the case that a schema sorted in one way is not identical to a schema sorted in another because the order of fields/arguments may affect the order in which they are resolved?
I therefore have the following questions:
Keywords (to help others find this issue, also detailing what I searched for before): alphabetic, lexographic, lexical, order, sort, printSchema, print, stable
The text was updated successfully, but these errors were encountered: