Sorting the fields in an introspection query of a federated schema

Hey folks. I’m writing my own node service embedding an Apollo federation server with a custom SupergraphManager instead of the built-in IntrospectAndCompose or the highly recommended static files for various irrelevant reasons. It’s actually replacing a previous version of the service written against much older versions of the Apollo libraries which have evolved significantly. It’s fine, we all make choices.

It’s all working great, except that the supergraph schema’s objects’ fields are not sorted in the graphiql browser, or, perhaps, other interactive graphql explorers any longer. This seems to have been a behavioral change in the Apollo libraries, but I can’t find any documentation thereof or any obvious way to control it. I feel confident I could sort each subgraph’s schema as I collect it and do my own set of transformations, but I assume that the federated server will then just present the fields in subgraph composition order, not lexicographically sorting the entire supergraph.

I have tried parsing the supergraph sdl string, applying lexical sort ast function from the graphql library, and rendering it again, but Apollo then complains the schema is not a valid federated schema; presumably something is being discarded thereby.

Any suggestions for how I might solve the problem? I would be happiest having a sorted merged schema to satisfy all introspection query callers, but I could readily live with telling graphiql to sort the schema for presentation, if only I could find such an option.

Hello :wave:
Introspection query is not guaranteed to have any specific order and may have different order than what is specified in SDL file. You could potentially write your own custom introspection resolver but I don’t think that’s a good idea.

IMHO proper place to fix the ordering is within the tooling (i.e. GraphiQL) itself. I’d raise a corresponding issue in GitHub - graphql/graphiql: GraphiQL & the GraphQL LSP Reference Ecosystem for building browser & IDE tools.. You could also try raising similar issue against GitHub - graphql/graphql-js: A reference implementation of GraphQL for JavaScript (which Apollo Server relies on for this functionality) but IMHO this should be addressed in the tooling instead.