Skip to content

Commit

Permalink
Revert changes from #3317 partially
Browse files Browse the repository at this point in the history
  • Loading branch information
kitten committed Aug 15, 2023
1 parent a1def12 commit 3c06744
Showing 1 changed file with 20 additions and 22 deletions.
42 changes: 20 additions & 22 deletions exchanges/graphcache/src/cacheExchange.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ export const cacheExchange =

// This registers queries with the data layer to ensure commutativity
const prepareForwardedOperation = (operation: Operation) => {
let optimistic = false;
if (operation.kind === 'query') {
// Pre-reserve the position of the result layer
reserveLayer(store.data, operation.key);
Expand All @@ -155,22 +156,7 @@ export const cacheExchange =
// Mark operation layer as done
noopDataState(store.data, operation.key);
return operation;
}

const query = formatDocument(operation.query);
operation = makeOperation(
operation.kind,
{
key: operation.key,
query,
variables: operation.variables
? filterVariables(getMainOperation(query), operation.variables)
: operation.variables,
},
{ ...operation.context }
);

if (
} else if (
operation.kind === 'mutation' &&
operation.context.requestPolicy !== 'network-only'
) {
Expand All @@ -187,21 +173,31 @@ export const cacheExchange =
if (dependencies.size) {
// Update blocked optimistic dependencies
for (const dep of dependencies.values()) blockedDependencies.add(dep);

// Store optimistic dependencies for update
optimisticKeysToDependencies.set(operation.key, dependencies);

// Update related queries
const pendingOperations: Operations = new Set();
collectPendingOperations(pendingOperations, dependencies);
executePendingOperations(operation, pendingOperations, true);

// Mark operation as optimistic
operation.context.optimistic = true;
optimistic = true;
}
}

return operation;
return makeOperation(
operation.kind,
{
key: operation.key,
query: formatDocument(operation.query),
variables: operation.variables
? filterVariables(
getMainOperation(operation.query),
operation.variables
)
: operation.variables,
},
{ ...operation.context, optimistic }
);
};

// This updates the known dependencies for the passed operation
Expand Down Expand Up @@ -250,7 +246,9 @@ export const cacheExchange =
result: OperationResult,
pendingOperations: Operations
): OperationResult => {
const { operation } = result;
// Retrieve the original operation to get unfiltered variables
const operation =
operations.get(result.operation.key) || result.operation;
if (operation.kind === 'mutation') {
// Collect previous dependencies that have been written for optimistic updates
const dependencies = optimisticKeysToDependencies.get(operation.key);
Expand Down

0 comments on commit 3c06744

Please sign in to comment.