Skip to content

Commit

Permalink
(core) - Fix (cloned) mutation operation being compared rather than a…
Browse files Browse the repository at this point in the history
… stable identity (#2228)

* (core) - Fix mutation operation's identity being compared

Instead of comparing the mutation operation's identity, we need
a stand-in object, whose identity can remain stable, even as
query document and other parameters are cloned and copied.

* Add changeset

* Simplify identity check

* Update tests
  • Loading branch information
kitten authored Jan 31, 2022
1 parent b79986a commit 8892ea8
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 10 deletions.
5 changes: 5 additions & 0 deletions .changeset/shaggy-frogs-attack.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@urql/core': patch
---

Fix mutation operation being used as compared identity and instead add a stand-in comparison.
2 changes: 1 addition & 1 deletion packages/core/src/client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ describe('promisified methods', () => {
expect(received.key).toBeDefined();
expect(received.variables).toEqual({ example: 1234 });
expect(received.kind).toEqual('mutation');
expect(received.context).toEqual({
expect(received.context).toMatchObject({
url: 'https://hostname.com',
requestPolicy: 'cache-and-network',
fetchOptions: undefined,
Expand Down
19 changes: 10 additions & 9 deletions packages/core/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,14 +174,12 @@ export const Client: new (opts: ClientOptions) => Client = function Client(
let result$ = pipe(
results$,
filter((res: OperationResult) => {
if (res.operation.kind === 'mutation') {
return res.operation === operation;
} else {
return (
res.operation.kind === operation.kind &&
res.operation.key === operation.key
);
}
return (
res.operation.kind === operation.kind &&
res.operation.key === operation.key &&
(!res.operation.context._instance ||
res.operation.context._instance === operation.context._instance)
);
})
);

Expand Down Expand Up @@ -281,6 +279,7 @@ export const Client: new (opts: ClientOptions) => Client = function Client(
if (!opts) opts = {};

return {
_instance: undefined,
url: client.url,
fetchOptions: client.fetchOptions,
fetch: client.fetch,
Expand All @@ -302,7 +301,9 @@ export const Client: new (opts: ClientOptions) => Client = function Client(
`Expected operation of type "${kind}" but found "${requestOperationType}"`
);
}
return makeOperation(kind, request, client.createOperationContext(opts));
const context = client.createOperationContext(opts);
if (kind === 'mutation') (context as any)._instance = [];
return makeOperation(kind, request, context);
},

executeRequestOperation(operation) {
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ export interface OperationDebugMeta {
/** Additional metadata passed to [exchange]{@link Exchange} functions. */
export interface OperationContext {
[key: string]: any;
readonly _instance?: [] | undefined;
additionalTypenames?: string[];
fetch?: typeof fetch;
fetchOptions?: RequestInit | (() => RequestInit);
Expand Down

0 comments on commit 8892ea8

Please sign in to comment.