apollo-feature-requests icon indicating copy to clipboard operation
apollo-feature-requests copied to clipboard

v3: Expose broadcastWatches() in public API

Open kinke opened this issue 5 years ago • 0 comments

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 });
});

kinke avatar Jun 24 '20 16:06 kinke