question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

mutation results are not updating cached objects in store

See original GitHub issue

Apollo 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:closed
  • Created 7 years ago
  • Reactions:1
  • Comments:11 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
spcbrncommented, Sep 3, 2019

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.

2reactions
pennyandseancommented, Feb 5, 2018

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?

Read more comments on GitHub >

github_iconTop Results From Across the Web

How exactly do you update an array in the cache after a ...
Hi, after executing a query getItems, it returns an array of Items which consist of an _id and title, and this appears in...
Read more >
How to update the Apollo Client's cache after a mutation
Basically, you should make your mutation results have all of the data necessary to update the queries previously fetched.
Read more >
unable to update cache after mutation - Stack Overflow
The values shown here are the ids. I am already returning ids in both, the query and mutation. How can I fix it...
Read more >
Runtime Architecture - Relay
A mechanism for reading data from the cache and subscribing for updates when these results change due to a mutation, subscription update, etc....
Read more >
Updating the Store – Angular - GraphQL Code Generator
Using update gives you full control over the cache, allowing you to make changes to your data model in response to a mutation...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found