rmosolgo/graphql-ruby

Return result keys in the same order as the request

rmosolgo opened this issue · 0 comments

The spec says that response maps should have keys in the same order that the query requested fields (https://spec.graphql.org/draft/#sec-Serialized-Map-Ordering), but GraphQL-Ruby doesn't do that. Instead, it creates a Hash to store the result and writes keys as soon as they're available. Consequently, results from GraphQL-Ruby violate the spec: instead of coming back in the order requested by the client, they come back in a library-determined order.

The simplest implementation would be to make a new, properly ordered Hash, something like:

field_indexes = {}
ast_nodes.each_with_index { |node, idx| field_indexes[node.name] = idx }
ordered_result = result.sort_by { |k,v| field_indexes[k] } 

But I'll have to check the memory and runtime impact of adding that.

Fixes #4252