Skip to content

Return result keys in the same order as the request #5011

Closed
@rmosolgo

Description

@rmosolgo

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions