Skip to content

Change order of fields inside Response#384

Merged
leebyron merged 2 commits into
graphql:masterfrom
IvanGoncharov:response_order
Nov 29, 2017
Merged

Change order of fields inside Response#384
leebyron merged 2 commits into
graphql:masterfrom
IvanGoncharov:response_order

Conversation

@IvanGoncharov

Copy link
Copy Markdown
Member

Currently, the spec requires data to be the first field in a response.
But after my PR was merged into graphql-js, errors always precede data:
image

I still think that having errors as the first property is a big DX improvement so I updated the spec to reflect the change in graphql-js.

@acjay

acjay commented Nov 21, 2017

Copy link
Copy Markdown

Why should the order matter? I like having errors first, but it seems arbitrary. And what of JSON libraries that leave order undefined?

@jlouis

jlouis commented Nov 22, 2017

Copy link
Copy Markdown

I've always found the ordering requirements odd.

At most, the specification can put a should on them, because JSON encoders are free to do whatever they want. If the internal representation of a JSON dict is a large hash table, then the rendering order is going to be arbitrary. Furthermore, some languages (Go is one example) randomizes the order in which you visit a hash table in order to make it explicit to the programmer that they cannot rely on the ordering.

In short, requiring order here is rather brittle and is bound to create problems down the line.

@IvanGoncharov

Copy link
Copy Markdown
Member Author

@acjay @jlouis GraphQL spec only recommends order of properties, for more details see
b22c73c

This PR just makes graphql-js (reference implementation) in line with spec recommendation :)

@stubailo

Copy link
Copy Markdown
Contributor

I think order requirements in the spec are for parsers which rely on the order to more efficiently parse the result. IMO that makes errors at the front even more valuable, since it is super useful to know about errors first in the case of a huge result. If it's framed as a recommendation it shouldn't be an issue since things are still valid GraphQL even if they return data first.

@IvanGoncharov

Copy link
Copy Markdown
Member Author

If it's framed as a recommendation it shouldn't be an issue since things are still valid GraphQL even if they return data first.

You right. But I think the spec should recommend only best practices and error first approach provide better UX. Moreover spec recommendation should be implemented at least in the reference implementation.

@stubailo

Copy link
Copy Markdown
Contributor

Yep, I just think there's a big difference between "recommend" vs. "require" in a spec! I totally agree errors first is the best approach.

@leebyron

Copy link
Copy Markdown
Collaborator

"A response to a GraphQL operation must be a map." does not imply order (otherwise it would say "ordered map") - I agree that the "first" and "next" terminology is more ambiguous than helpful in this context, though I like declaring the order via the order of paragraphs.

I think we can add a more explicit phrase about ordering errors first in a formatted output as a convenience

Remove "first" and "next" and include an explicit note about errors appearing first when serialized as a convenience.
@IvanGoncharov

Copy link
Copy Markdown
Member Author

@leebyron Great improvment 👍
Hope it will be merged soon.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants