Order of keys returned from apollo/client are changed


I recently upgraded to using @apollo/client instead of @apollo/react-components and “@apollo/react-hooks”.

In doing so one of my implementation that depended on object key order was broken (for localization of json).

It seems the stored order of the keys in my returned data were changed to alphabetical order. Is there anything I can do to stop that from happening? Is this a bug that should be reported?

1 Like

It appears changing the fetchPolicy to no-cache fixed this. Which is unfortunate since caching would have been nice.

Yeah my immediate guess is that the implementation of the cache uses a sorted map for a speed boost.

Yep, and it causes naming consistency for the keys in the in-memory cache, no matter in what order the query variables are passed. You can check out the source code for an in-depth documentation: apollo-client/object-canon.ts at main · apollographql/apollo-client · GitHub

Aside from that, it is generally never a good idea to rely on a specific key order in javascript objects, see here for a good explanation why: Does JavaScript guarantee object property order? - Stack Overflow

That is not the case anymore you can rely on it in es6 and later and I actually need it for localized objects.

In fact the graphql specification says that it will return in order when possible so this violates that contract. See here: http://spec.graphql.org/ section 3.6

Feel free to file a bug report in github.