graphql-tools icon indicating copy to clipboard operation
graphql-tools copied to clipboard

Allow sorting only top level declarations in schema

Open kbrooks opened this issue 2 years ago • 1 comments
trafficstars

Is your feature request related to a problem? Please describe.

This library is a dependency of graphql-code-generator which is used to produce a generated schema file.

https://github.com/dotansimha/graphql-code-generator/issues/6935

I opened an issue on graphql-js here https://github.com/graphql/graphql-js/issues/3971 but it was closed due to there being a trivial work-around. We can use this approach in this library to resolve my issue.

Describe the solution you'd like

Add a field to the options on loadSchemaSync which enables sorting only the top level declarations

    return options.sort
       ? lexicographicSortSchema(sources[0].schema) 
       : options.sortShallow // naming?
       ? shallowSortSchema(sources[0].schema) 
       : sources[0].schema;

...

function shallowSortSchema(schema) {
  const schemaConfig = schema.toConfig();
  const sortedSchema = new GraphQLSchema({
    ...schemaConfig,
    types: [...schemaConfig.types].sort((a, b) => a.name.localeCompare(b.name)),
  });
}

I can prepare a PR for this myself.

Describe alternatives you've considered

Currently I am applying a patch to the graphql-js lexicographicSortSchema function in order to produce my desired output.

Additional context

kbrooks avatar Sep 19 '23 19:09 kbrooks

Facing the same issue so would love to see this :)

erwan-joly avatar Nov 01 '23 10:11 erwan-joly