0

I'm trying to achieve optimistic updates with apollo client.

I already have it working in different places in the app however I'm facing a wall here.

I'm running

store.writeQuery({query, variables, data});

within the update mutation callback and I tried to store.read it, it works fine. However, I can't manage to get this data from something else in the app.

The data I write to the cache is this:

{
                    playlist: {
                      __typename: 'Playlist',
                      elements: [{
                        __typename: 'Element',
                        id: 'test',
                        rating: 3,
                      }],
                    },
},

However, in the component where I want to use this cached data, my query is way more complex and I fetch way more fields. Not only that, but I also expect most of playlist.elements to be fetched from the network, only this specific one I expect to be coming from cache.

Is there to get only the element with the id: 'test updated, and the others updated from network normally ? Currently, I only get data from network and this cache written data doesn't seem to affect my query result.

Thank you :)

gomes
  • 681
  • 1
  • 7
  • 20
  • 1
    Optimistic updates are usually done by returning `optimisticResponse`, no? You seem to be permanently writing to the store. Maybe also using `writeFragment` can help if you are just updating a single element. Find the documentation [here](https://www.apollographql.com/docs/react/features/caching.html#writequery-and-writefragment) – Herku Mar 08 '19 at 18:45
  • @Herku Unfortunately using `optimisticResponse` isn't an option here as I don't return the data I'd need in cache from mutation, but a boolean (`success: boolean` pretty much). Without going into implementation details, I will need to update cache and read from it. Indeed I've checked the docs, but using a fragment didn't help, the data was still fully fetched from server ( or empty if using `cache-first` fetchPolicy. – gomes Mar 08 '19 at 23:36
  • Do you own the GraphQL implementation (as in you can change it)? Because it seems not very docmatic. Also I am no longer sure if we are talking about optimistic response (optimistic means it acts as a placeholder until the real result comes in). Maybe you are misunderstanding quite a lot of things about GraphQL. Best place to start would be reading the whole page I linked. – Herku Mar 09 '19 at 11:07
  • The whole idea of (optimistically) updating the cache is that you are (manually) syncronising the state with the server. This is why I don't understand why getting "all data from the server" is a problem. I think you are missing something else and it is hard to say without more code. – Herku Mar 09 '19 at 11:10
  • It mostly has to do with the GraphQL API. To cut a long story short, it's a wrapper over a REST API and there's not as much freedom as it'd be possible with a fully GraphQL API. With that being said, `writeFragment` and `readFragment` worked. Feels like I somehow reinvented the wheel here so I'll still investigate a more optimal solution, but as far as the question goes, that's a solved :) – gomes Mar 10 '19 at 09:57

0 Answers0