Updating Redux store based on changes to Apollo store


#1

Hi,

I would like to react to any updates to specific fields in my Apollo store. In my case, I am trying to cache computations based on Apollo store data within my UI state reducer – data comes in, I do some math, store the result, and all pages of my SPA can access it; when the source data in the Apollo store changes, I want to re-run the computation.

I have tried setting up a watchQuery for this purpose, but watchQuery doesn’t seem to deep-diff the new data against the old – to be fair, that’s React’s job – so it’s re-running the computation every time, defeating the point of the whole exercise.

For example, say I set up

client.watchQuery({
  query: gql`query {project { customers { id, name }}}`,
  reducer: SharedReducer
});

This will trigger an APOLLO_QUERY_RESULT action (and thus my re-computation) every time someone, somewhere queries the customers, even if the customers end up being exactly the same as before.

Is there a better way to accomplish this that I just haven’t thought of, maybe using a different technique altogether? Or if there isn’t, is there a library or example code that I can look at? Sometimes the deep-diffing may be more expensive than the re-computation itself, so that’s another consideration as well. I’m grateful for anyone’s input!