Skip to content

Fixes Encoded JSON Result Ordering #94

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conversation

NeedleInAJayStack
Copy link
Member

If you encode a GraphQLResult with an ordinary JSONEncoder, there are no guarantees that the field order will match the query, violating the GraphQL spec.

This PR adds a GraphQLJSONEncoder type that will ensure that order is preserved when a Map is encoded. It is exactly the same as Foundation's JSONEncoder, but uses OrderedDictionary everywhere Dictionary was used.

@NeedleInAJayStack
Copy link
Member Author

NeedleInAJayStack commented Nov 11, 2021

I don't love duplicating so much code from Foundation but I couldn't think of another easy way to get this level of access to the base JSONEncoder class and I hesitated to make a PR to Foundation or swift-collections because our needs seem like an unusual edge-case.

However, if you have any thoughts on how to minimize copied code I'm interested in hearing them!

@paulofaria
Copy link
Member

paulofaria commented Nov 12, 2021

The ideal solution would be to add an option to Foundation's JSONEncoder, like the pretty print option API. However, that would take a while and it's not even guaranteed that they would accept it. So I guess it makes sense for us to provide the appropriate encoder ourselves. I'm refactoring Graphiti to add support for async await and ended up getting carried away and decided to update a lot of APIs. This would mean a major bump and now I'm thinking we might rename Map to JSON, since that's basically what it is. While doing that, we could just update MapEncoder to use ordered dictionaries (if it doesn't already, I have to look). What do you think?

@NeedleInAJayStack
Copy link
Member Author

The ideal solution would be to add an option to Foundation's JSONEncoder, like the pretty print option API. However, that would take a while and it's not even guaranteed that they would accept it. So I guess it makes sense for us to provide the appropriate encoder ourselves.

I agree on both counts.

I'm thinking we might rename Map to JSON, since that's basically what it is. While doing that, we could just update MapEncoder to use ordered dictionaries (if it doesn't already, I have to look).

It does already, since #84. Interestingly, that one preserved result ordering throughout the query -> GraphQLResult process, but I was getting randomized order at the final step when encoding GraphQLResult to JSON, prompting this PR.

What do you think?

I think this all sounds awesome! Nice work attacking the async/await transition!

@NeedleInAJayStack
Copy link
Member Author

Hey @paulofaria, do you have any other concerns on this? Do you mind approving if not?

@paulofaria
Copy link
Member

Sorry for the delay!

@NeedleInAJayStack NeedleInAJayStack merged commit 151c2f1 into GraphQLSwift:master Feb 2, 2022
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.

2 participants