Skip to content

Commit

Permalink
Avoid destructively modifying result.data in QueryInfo#markResult.
Browse files Browse the repository at this point in the history
Should help with #9293.
  • Loading branch information
benjamn committed Jun 28, 2023
1 parent 3a05815 commit 623d259
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 7 deletions.
22 changes: 16 additions & 6 deletions src/core/QueryInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,7 @@ export class QueryInfo {
| "fetchPolicy"
| "errorPolicy">,
cacheWriteBehavior: CacheWriteBehavior,
) {
): typeof result {
const merger = new DeepMerger();
const graphQLErrors = isNonEmptyArray(result.errors)
? result.errors.slice(0)
Expand Down Expand Up @@ -413,7 +413,10 @@ export class QueryInfo {
});

this.lastWrite = {
result,
// Make a shallow defensive copy of the result object, in case we
// later later modify result.data in place, since we don't want
// that mutation affecting the saved lastWrite.result.data.
result: { ...result },
variables: options.variables,
dmCount: destructiveMethodCounts.get(this.cache),
};
Expand Down Expand Up @@ -454,7 +457,10 @@ export class QueryInfo {
this.lastDiff.diff.complete) {
// Reuse data from the last good (complete) diff that we
// received, when possible.
result.data = this.lastDiff.diff.result;
result = {
...result,
data: this.lastDiff.diff.result,
};
return;
}
// If the previous this.diff was incomplete, fall through to
Expand All @@ -479,14 +485,18 @@ export class QueryInfo {
// Set without setDiff to avoid triggering a notify call, since
// we have other ways of notifying for this result.
this.updateLastDiff(diff, diffOptions);
if (diff.complete) {
result.data = diff.result;
}
result = {
...result,
// TODO Improve types so we don't need this cast.
data: diff.result as any,
};
});
} else {
this.lastWrite = void 0;
}
}

return result;
}

public markReady() {
Expand Down
7 changes: 6 additions & 1 deletion src/core/QueryManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1148,7 +1148,12 @@ export class QueryManager<TStore> {
// Use linkDocument rather than queryInfo.document so the
// operation/fragments used to write the result are the same as the
// ones used to obtain it from the link.
queryInfo.markResult(result, linkDocument, options, cacheWriteBehavior);
result = queryInfo.markResult(
result,
linkDocument,
options,
cacheWriteBehavior,
);
queryInfo.markReady();
}

Expand Down

0 comments on commit 623d259

Please sign in to comment.