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): mutation would cause dependent operations and reexecuting operations become the same set #3665

Merged
merged 3 commits into from
Sep 9, 2024
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/tidy-spies-turn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@urql/exchange-graphcache': patch
---

fix bug that mutation would cause dependent operations and reexecuting operations to become the same set
142 changes: 142 additions & 0 deletions exchanges/graphcache/src/cacheExchange.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -913,6 +913,148 @@ describe('data dependencies', () => {
expect(reexecuteOperation).toHaveBeenCalledTimes(0);
expect(result.mock.calls[0][0]).toHaveProperty('data.author', null);
});

it('mutation does not change number of reexecute request after a query', () => {
const client = createClient({
url: 'http://0.0.0.0',
exchanges: [],
});

const { source: ops$, next: nextOp } = makeSubject<Operation>();

const reexec = vi
.spyOn(client, 'reexecuteOperation')
.mockImplementation(nextOp);

const mutation = gql`
mutation {
updateNode {
__typename
id
}
}
`;

const normalQuery = gql`
{
__typename
item {
__typename
id
}
}
`;

const extendedQuery = gql`
{
__typename
item {
__typename
extended: id
extra @_optional
}
}
`;

const mutationOp = client.createRequestOperation('mutation', {
key: 0,
query: mutation,
variables: undefined,
});

const normalOp = client.createRequestOperation(
'query',
{
key: 1,
query: normalQuery,
variables: undefined,
},
{
requestPolicy: 'cache-and-network',
}
);

const extendedOp = client.createRequestOperation(
'query',
{
key: 2,
query: extendedQuery,
variables: undefined,
},
{
requestPolicy: 'cache-only',
}
);

const response = vi.fn((forwardOp: Operation): OperationResult => {
if (forwardOp.key === 0) {
return {
operation: mutationOp,
data: {
__typename: 'Mutation',
updateNode: {
__typename: 'Node',
id: 'id',
},
},
stale: false,
hasNext: false,
};
} else if (forwardOp.key === 1) {
return {
operation: normalOp,
data: {
__typename: 'Query',
item: {
__typename: 'Node',
id: 'id',
},
},
stale: false,
hasNext: false,
};
} else if (forwardOp.key === 2) {
return {
operation: extendedOp,
data: {
__typename: 'Query',
item: {
__typename: 'Node',
extended: 'id',
extra: 'extra',
},
},
stale: false,
hasNext: false,
};
}

return undefined as any;
});

const forward = (ops$: Source<Operation>): Source<OperationResult> =>
pipe(ops$, map(response), share);

pipe(cacheExchange()({ forward, client, dispatchDebug })(ops$), publish);

nextOp(normalOp);
expect(reexec).toHaveBeenCalledTimes(0);

nextOp(extendedOp);
expect(reexec).toHaveBeenCalledTimes(0);

// re-execute first operation
reexec.mockClear();
nextOp(normalOp);
expect(reexec).toHaveBeenCalledTimes(4);

nextOp(mutationOp);

// re-execute first operation after mutation
reexec.mockClear();
nextOp(normalOp);
expect(reexec).toHaveBeenCalledTimes(4);
});
});

describe('directives', () => {
Expand Down
3 changes: 2 additions & 1 deletion exchanges/graphcache/src/cacheExchange.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,9 @@ export const cacheExchange =
// Upon completion, all dependent operations become reexecuting operations, preventing
// them from reexecuting prior operations again, causing infinite loops
const _reexecutingOperations = reexecutingOperations;
reexecutingOperations = dependentOperations;
if (operation.kind === 'query') {
(reexecutingOperations = dependentOperations).add(operation.key);
reexecutingOperations.add(operation.key);
}
(dependentOperations = _reexecutingOperations).clear();
}
Expand Down