mutation results are not updating cached objects in store
See original GitHub issueApollo store seems to not update object with results coming from a mutation. Not sure if this is a bug, not implemented feature or misunderstanding of me?
The docs say for updateQueries:
UI should just update automatically based on the result of a mutation as long as modified fields of objects and the object identifiers of modified objects are returned with the mutation (see the dataIdFromObject documentation above for more information).
I’m running a mutation against the server and return the updated object. My expected behavior of apollo-client store is that the returned object should updated the internal store and propagate changes reactively without having to write updateQueries statements if the dataIdFromObject is set. In this case the ui is updated lazy and not optimistic.
apollo-client cache updates are set up as in the docs.
const client = new ApolloClient({
dataIdFromObject: (o) => {
console.log("dataIdFromObject",o);
return`${o.__typename}-${o.id},`;
}
});
I have a split application view, left a list view and right a detail editor
ngInit() {
this.apollo.watchQuery({
query: gqlQueryMyPosts
}).subscribe(
(d) => {
console.log("query", d);
this.posts = d.data;
}
);
}
function onSave() {
this.apollo.mutate({
mutation: gqlSavePost,
variables: {
id: post.id,
title: post.title
}
}).subscribe(
(d) => console.log("mutation", d);
);
}
this results in the following console.log:
dataIdFromObject Object
dataIdFromObject Object
...
query [Object, {id:2, title:"old", ...}...]
>> editing post pressing post save button triggering onSave()
query [Object, {id:2, title:"old", ...}...] << reactively fired update with the changed object not being updated, thats ok. response ist not yet here
mutation {id:2, title:"new", ...} << this is updated object from server with changed Object
query [Object, {id:2, title:"old", ...} ...] << reactively fired update, but still returning old data from the first query
I would have expected that with the mutation the dataIdFromObject would fire to identify the returned object. But this is not happening either.
I’m not sure if this is related bug discussed in issue #1123
using:
"apollo-client": "^0.7.0",
"angular2-apollo": "^0.9.0-rc.5",
Issue Analytics
- State:
- Created 7 years ago
- Reactions:1
- Comments:11 (3 by maintainers)
Top GitHub Comments
I had this same issue, but discovered that if your mutation (or whatever interaction is triggering the cache update) doesn’t return fields that are designated nonnull in your schema, the cache will not update.
Not sure if this is relevant, but I have the store objects updating fine when they have a standard id like __typename:id but not when they are singular “$ROOT_QUERY.typename”, probably right?