Graphql-engine: fields in query response don't respect the order in the query

Created on 11 Jul 2018  路  3Comments  路  Source: hasura/graphql-engine

eg: query {author {name id}} should return name before id in response

server hard bug medium

Most helpful comment

As per the latest GraphQL spec

7.2.2 Serialized Map Ordering

Since the result of evaluating a selection set is ordered, the serialized Map of results should preserve this order by writing the map entries in the same order as those fields were requested as defined by query execution. Producing a serialized response where fields are represented in the same order in which they appear in the request improves human readability during debugging and enables more efficient parsing of responses if the order of properties can be anticipated.

Serialization formats which represent an ordered map should preserve the order of requested fields as defined by CollectFields() in the Execution section. Serialization formats which only represent unordered maps but where order is still implicit in the serialization鈥檚 textual order (such as JSON) should preserve the order of requested fields textually.

For example, if the request was { name, age }, a GraphQL service responding in JSON should respond with { "name": "Mark", "age": 30 } and should not respond with { "age": 30, "name": "Mark" }.

While JSON Objects are specified as an unordered collection of key鈥恦alue pairs the pairs are represented in an ordered manner. In other words, while the JSON strings { "name": "Mark", "age": 30 } and { "age": 30, "name": "Mark" } encode the same value, they also have observably different property orderings.

All 3 comments

@rikinsk, author object will be a JSON object, right? How does the order of fields matter in a object? Isn't {name: something, id: 1} same as {id: 1, name: something}?

Or is it something in the GraphQL spec that I am missing?

As per the latest GraphQL spec

7.2.2 Serialized Map Ordering

Since the result of evaluating a selection set is ordered, the serialized Map of results should preserve this order by writing the map entries in the same order as those fields were requested as defined by query execution. Producing a serialized response where fields are represented in the same order in which they appear in the request improves human readability during debugging and enables more efficient parsing of responses if the order of properties can be anticipated.

Serialization formats which represent an ordered map should preserve the order of requested fields as defined by CollectFields() in the Execution section. Serialization formats which only represent unordered maps but where order is still implicit in the serialization鈥檚 textual order (such as JSON) should preserve the order of requested fields textually.

For example, if the request was { name, age }, a GraphQL service responding in JSON should respond with { "name": "Mark", "age": 30 } and should not respond with { "age": 30, "name": "Mark" }.

While JSON Objects are specified as an unordered collection of key鈥恦alue pairs the pairs are represented in an ordered manner. In other words, while the JSON strings { "name": "Mark", "age": 30 } and { "age": 30, "name": "Mark" } encode the same value, they also have observably different property orderings.

closed with 32ae105279c4bd94cb7b60a3fb8699126312ac2a

Was this page helpful?
0 / 5 - 0 ratings