-
-
Notifications
You must be signed in to change notification settings - Fork 570
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
Request: consistent sorting within exported GQL schema #989
Comments
Workaround available (borrowing from #512 and graphql/graphql-js#941). I think this should be implemented as default behavior (perhaps with a import { writeFileSync } from 'fs';
import { parse, buildASTSchema, GraphQLSchema } from 'graphql';
import { printSchema } from 'graphql/utilities';
import { resolve } from 'path';
import { createPostGraphileSchema } from 'postgraphile';
import {
common,
PG_POOL,
PG_SCHEMA,
EXPORT_GQL_SCHEMA_PATH,
} from './config';
createPostGraphileSchema(PG_POOL, PG_SCHEMA, common)
.then((original) => {
const filepath = resolve(EXPORT_GQL_SCHEMA_PATH);
console.log(`Writing GQL schema: ${filepath}`);
writeFileSync(filepath, printSchemaOrdered(original));
process.exit(0);
})
.catch(() => {
console.error('Failed to generate GraphQL schema!');
process.exit(1);
});
function printSchemaOrdered(original: GraphQLSchema) {
// Clone schema so we don't damage anything
const schema = buildASTSchema(parse(printSchema(original)));
const typeMap = schema.getTypeMap();
Object.keys(typeMap).forEach(name => {
const Type = typeMap[name];
// Object?
if ('getFields' in Type) {
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 ('args' in value) {
value.args.sort((a, b) => a.name.localeCompare(b.name));
}
});
}
// Enum?
if ('getValues' in Type) {
Type.getValues().sort((a, b) => a.name.localeCompare(b.name));
}
});
return printSchema(schema);
}; |
I'm not sure this should be default behaviour, the ordering generated by the schema often makes more sense (e.g. I'm not keen to add more command-line flags, I think this is better served by a small generic utility that sorts a GraphQL schema. |
I'm submitting a ...
PostGraphile version: v4.3.2
Current behavior:
Since the auto-generated connections all take mostly the same parameters,
git
easily confuses the adding/removing of types in the schema as individual line changes. Taking this partial diff as an example, theartifactsToVehicles
connection didn't actually change, it was therequirements
connection that was added, but this is not immediately obvious:Expected behavior:
When using
exportGqlSchemaPath
, the server should sort the generated output within each block so that the output remains mostly the same when types are added and removed from the schema.The text was updated successfully, but these errors were encountered: