When creating a record directly against local cache, Apollo behaves unexpectedly if one of the optional fields is missing
See original GitHub issueIntended outcome: To create a record locally which will be synced at a later time to the server. The goal is to read from cache, append to the cached array and save directly to cache.
Actual outcome: When creating the record locally as described here Apollo “empties out” my local query cache.
This issue only happens if I create a record locally which misses any of the fields specified by the original query.
How to reproduce the issue:
Use a query such as this to fetch data from server and into local cache:
query fetchAssetsQuery {
assets {
contentState
id
createdAt
}
Now create a new record locally which is missing the createdAt
prop as such:
let assets = client.readQuery({
query: fetchAssetsQuery
}).assets;
client.writeQuery({
query: fetchAssetsQuery,
data: {
assets: [{
id: id,
}, ...assets]
}
});
Finally, sync to the server as such:
return mutate({
variables: assetListInput: {
assets: assets
},
updateQueries: {
getUserAssets: (previousQueryResult, { mutationResult }) => {
let currentAssets = previousQueryResult.assets;
return { assets: currentAssets };
}
}
})
This will result in the query turning into an empty array. To be more specific, updateQueries
does not get executed here.
However, If I create the local record with the missing createdAt
prop, this issue is resolved.
There are certain props that I’d like the server to create such as createdAt
.
Issue Analytics
- State:
- Created 6 years ago
- Comments:8 (5 by maintainers)
Top GitHub Comments
If I’m understanding correctly, this is intended behavior since if you update the store with a result that’s missing
createdAt
, it’s going to break the existing query observer on the page associated with the query containing thecreatedAt
field.You could just set your
createdAt
field to a null-like value initially and then insert it once it hits the server and comes back.Got it. Cool. Just wanted to make sure it’s being tracked.