-
Notifications
You must be signed in to change notification settings - Fork 2.7k
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
Issue with writeData() and nested array fields #4785
Comments
I tracked down the issue... friends: [
{ id: '2', name: 'Sara Smith', favoriteColors: [], __typename: 'Person' },
{
id: '3',
name: 'Budd Deey',
favoriteColors: [
{ id: 'magenta', label: 'Magenta', __typename: 'Color' }
],
__typename: 'Person'
}
] The I was thinking about working on a PR for this, but I'm not sure of the best solution here. For one thing, I'm not sure how smart |
I stumbled across similar issue with writeData today regards storing object with nested object data. The writeData utils generate query with all keys nested in the query, so it will obviously write the data such way that it requires using the id and __typename in the nested objects which in turn are cached separately in flat cache data. However with custom writeQuery with no field nesting, the nested objects can be saved and queried without the id and __typename requirement as cache stores the data as "type: 'json'" objects as it makes the fields work as values (although objects) and not as graphql objects. I guess it's intended functionality, but was a bit gotcha. |
We should probably add a note to apollo documentation about the usage of |
|
In some conditions involving queries with nested arrays,
client.writeData()
doesn't work, causing subsequent queries to fail.Intended outcome:
The following demo should work without errors:
https://github.com/mbrowne/react-apollo-error-template/tree/cache-array-issue
Actual outcome:
The
client.writeData()
apparently fails to update the cache correctly. The next time the component renders, no data is returned from the query:How to reproduce the issue:
Clone the
cache-array-issue
branch of my demo repo:git clone git@github.com:mbrowne/react-apollo-error-template.git --branch cache-array-issue --single-branch
It seems that this issue only happens when querying a nested array field more than one level deep. For example, if you comment out the
favoriteColors
field in my demo, there are no errors and everything works as expected:It also only happens with particular data...for example, in my demo data, 'John Smith' has the following friends:
friendIds: [2, 3]
If you simply reverse the order to:
friendIds: [3, 2]
...then it works. When I was debugging this, it seemed to have something to do with the graphql query that
writeData
generates automatically behind the scenes (via thefragmentFromPojo
helper). SincefragmentFromPojo
cannot determine the nested fields from an empty array, it generates a query that doesn't include the necessary fields, e.g. justfavoriteColors
instead of:Versions
The text was updated successfully, but these errors were encountered: