Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(graphcache): Prevent marking operations as reexecuting after optimistic mutation #3265

Merged
merged 1 commit into from
Jun 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/serious-dots-sit.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@urql/exchange-graphcache': patch
---

Optimistic mutation results should never result in dependent operations being blocked.
23 changes: 13 additions & 10 deletions exchanges/graphcache/src/cacheExchange.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,8 @@ export const cacheExchange =

const executePendingOperations = (
operation: Operation,
pendingOperations: Operations
pendingOperations: Operations,
isOptimistic: boolean
) => {
// Reexecute collected operations and delete them from the mapping
for (const key of pendingOperations.values()) {
Expand All @@ -130,13 +131,15 @@ export const cacheExchange =
}
}

// Upon completion, all dependent operations become reexecuting operations, preventing
// them from reexecuting prior operations again, causing infinite loops
const _reexecutingOperations = reexecutingOperations;
if (operation.kind === 'query') {
(reexecutingOperations = dependentOperations).add(operation.key);
if (!isOptimistic) {
// Upon completion, all dependent operations become reexecuting operations, preventing
// them from reexecuting prior operations again, causing infinite loops
const _reexecutingOperations = reexecutingOperations;
if (operation.kind === 'query') {
(reexecutingOperations = dependentOperations).add(operation.key);
}
(dependentOperations = _reexecutingOperations).clear();
}
(dependentOperations = _reexecutingOperations).clear();
};

// This registers queries with the data layer to ensure commutativity
Expand Down Expand Up @@ -171,7 +174,7 @@ export const cacheExchange =
// Update related queries
const pendingOperations: Operations = new Set();
collectPendingOperations(pendingOperations, dependencies);
executePendingOperations(operation, pendingOperations);
executePendingOperations(operation, pendingOperations, true);
}
}

Expand Down Expand Up @@ -424,7 +427,7 @@ export const cacheExchange =
// Update the cache with the incoming API result
const cacheResult = updateCacheWithResult(result, pendingOperations);
// Execute all dependent queries
executePendingOperations(result.operation, pendingOperations);
executePendingOperations(result.operation, pendingOperations, false);
return cacheResult;
})
);
Expand Down Expand Up @@ -458,7 +461,7 @@ export const cacheExchange =
);

// Execute all dependent queries as a single batch
executePendingOperations(result.operation, pendingOperations);
executePendingOperations(result.operation, pendingOperations, false);

return fromArray(results);
})
Expand Down