apollo-feature-requests
apollo-feature-requests copied to clipboard
v3: Expose broadcastWatches() in public API
Hi,
I'm experimenting with v3 and very much like the new broadcast option for writeQuery(), thanks.
Some of my use cases would be very much simplified if InCacheMemory.broadcastWatches() was public though.
The reason is that I'd like to broadcast once after a batch of updates, but I don't know in advance whether the last one of those isn't skipped (depending on the cached data at that point, which in turn may depend on previous updates of that batch). The logic is something like this:
const batchedUpdates = [
{ query: ..., variables: ..., propName: ..., updateProp: ... }, ...
];
batchedUpdates.forEach(({ query, variables, propName, updateProp }) => {
const cachedData = client.readQuery({ query, variables });
const oldProp = cachedData[propName];
const newProp = updateProp(oldProp); // returns oldProp for a no-op
if (newProp !== oldProp)
client.writeQuery({ query, variables, data: { ...cachedData, [propName]: newProp }, broadcast: false });
});