···11+---
22+'@urql/exchange-graphcache': patch
33+---
44+55+Adjust mutation results priority to always override query results as they arrive, similarly to subscriptions. This will prevent race conditions when mutations are slow to execute at the cost of some consistency.
+1-1
exchanges/graphcache/src/cacheExchange.test.ts
···15451545 });
1546154615471547 expect(reexec).toHaveBeenCalledTimes(2);
15481548- expect(data).toHaveProperty('node.name', 'query b');
15481548+ expect(data).toHaveProperty('node.name', 'mutation');
15491549 });
1550155015511551 it('applies optimistic updates on top of commutative queries', () => {
···80808181 if (!layerKey) {
8282 currentOptimisticKey = null;
8383- } else if (
8484- isOptimistic ||
8585- (data.optimisticOrder.length > 1 &&
8686- data.optimisticOrder.indexOf(layerKey) > -1)
8787- ) {
8383+ } else if (isOptimistic || data.optimisticOrder.length > 0) {
8884 // If this operation isn't optimistic and we see it for the first time,
8985 // then it must've been optimistic in the past, so we can proactively
9086 // clear the optimistic data before writing
9187 if (!isOptimistic && !data.commutativeKeys.has(layerKey)) {
9292- clearLayer(data, layerKey);
9393- data.commutativeKeys.add(layerKey);
8888+ reserveLayer(data, layerKey);
8989+ } else if (isOptimistic) {
9090+ // NOTE: This optimally shouldn't happen as it implies that an optimistic
9191+ // write is being performed after a concrete write.
9292+ data.commutativeKeys.delete(layerKey);
9493 }
9494+9595 // An optimistic update of a mutation may force an optimistic layer,
9696 // or this Query update may be applied optimistically since it's part
9797 // of a commutate chain
···457457458458/** Reserves an optimistic layer and preorders it */
459459export const reserveLayer = (data: InMemoryData, layerKey: number) => {
460460- if (data.optimisticOrder.indexOf(layerKey) === -1) {
460460+ const index = data.optimisticOrder.indexOf(layerKey);
461461+ if (index === -1) {
461462 // The new layer needs to be reserved in front of all other commutative
462463 // keys but after all non-commutative keys (which are added by `forceUpdate`)
464464+ data.optimisticOrder.unshift(layerKey);
465465+ } else if (!data.commutativeKeys.has(layerKey)) {
466466+ // Protect optimistic layers from being turned into non-optimistic layers
467467+ // while preserving optimistic data
468468+ clearLayer(data, layerKey);
469469+ // If the layer was an optimistic layer prior to this call, it'll be converted
470470+ // to a new non-optimistic layer and shifted ahead
471471+ data.optimisticOrder.splice(index, 1);
463472 data.optimisticOrder.unshift(layerKey);
464473 }
465474