graphql-dotnet/parser

Add SDLPrinterOptions.Sorted property

Shane32 opened this issue · 7 comments

Should sort all sortable AST nodes while printing.

@sungam3r Are you interested in adding this feature? I would find it very useful for tests on dynamically generated schema.

What does sortable mean?

Well, like the root of a SDL is equally valid no matter which order types are defined in. Same is probably true for a list of field definitions. However, changing the order of a selection set would change the order that the query is processed. So some list types can be sorted without affecting anything, and others cannot.

I'm specifically thinking of type definitions, where it is important for me to be able to produce a sorted list of type definitions, with the field definitions within each type definition sorted, and the argument definitions within each field definition sorted.

As it is now, I think the old SchemaPrinter could sort type names but did not sort field or argument names, or something. It would cause tests to intermittently fail because the types were dynamically generated from .NET classes, and .NET reflection would not consistently return methods/arguments in the same order even when the source did not change. (Mostly due to the Visual Studio partial-building optimizations, I think, because if I rebuild all or build from CI, then it would always compile the same.)

However, changing the order of a selection set would change the order that the query is processed.

The same for applied directives - the order matters. We have SchemaPrinterOptions.Comparer property to handle sorting. I'm rewritting all this stuff now. I can add some lost comparers (for example, for implemented interfaces) that I found when wrote code to convert Schema to GraphQLDocument. Note that ISchemaComparer works both for printing and introspection. Also there is AlphabeticalSchemaComparer that should already handle what you called Sorted.

I'm specifically thinking of type definitions, where it is important for me to be able to produce a sorted list

I understand. I widely use SchemaPrinter at work in API approval tests to verify unintentional changes in our GraphQL schemas.

Nice. 👍 So close this now?

OK to close.