Skip to content

Commit

Permalink
fix: caching issue in #11560 (reverts #11202) (#11576)
Browse files Browse the repository at this point in the history
  • Loading branch information
alessbell authored Feb 6, 2024
1 parent bb908a3 commit e855d00
Show file tree
Hide file tree
Showing 20 changed files with 144 additions and 206 deletions.
2 changes: 1 addition & 1 deletion .api-reports/api-report-core.md
Original file line number Diff line number Diff line change
Expand Up @@ -1677,7 +1677,7 @@ class QueryInfo {
// Warning: (ae-forgotten-export) The symbol "CacheWriteBehavior" needs to be exported by the entry point index.d.ts
//
// (undocumented)
markResult<T>(result: FetchResult<T>, document: DocumentNode, options: Pick<WatchQueryOptions, "variables" | "fetchPolicy" | "errorPolicy">, cacheWriteBehavior: CacheWriteBehavior): typeof result;
markResult<T>(result: FetchResult<T>, document: DocumentNode, options: Pick<WatchQueryOptions, "variables" | "fetchPolicy" | "errorPolicy">, cacheWriteBehavior: CacheWriteBehavior): void;
// (undocumented)
networkError?: Error | null;
// (undocumented)
Expand Down
2 changes: 1 addition & 1 deletion .api-reports/api-report-react.md
Original file line number Diff line number Diff line change
Expand Up @@ -1571,7 +1571,7 @@ class QueryInfo {
// Warning: (ae-forgotten-export) The symbol "CacheWriteBehavior" needs to be exported by the entry point index.d.ts
//
// (undocumented)
markResult<T>(result: FetchResult<T>, document: DocumentNode, options: Pick<WatchQueryOptions, "variables" | "fetchPolicy" | "errorPolicy">, cacheWriteBehavior: CacheWriteBehavior): typeof result;
markResult<T>(result: FetchResult<T>, document: DocumentNode, options: Pick<WatchQueryOptions, "variables" | "fetchPolicy" | "errorPolicy">, cacheWriteBehavior: CacheWriteBehavior): void;
// (undocumented)
networkError?: Error | null;
// (undocumented)
Expand Down
2 changes: 1 addition & 1 deletion .api-reports/api-report-react_components.md
Original file line number Diff line number Diff line change
Expand Up @@ -1271,7 +1271,7 @@ class QueryInfo {
// Warning: (ae-forgotten-export) The symbol "CacheWriteBehavior" needs to be exported by the entry point index.d.ts
//
// (undocumented)
markResult<T>(result: FetchResult<T>, document: DocumentNode, options: Pick<WatchQueryOptions, "variables" | "fetchPolicy" | "errorPolicy">, cacheWriteBehavior: CacheWriteBehavior): typeof result;
markResult<T>(result: FetchResult<T>, document: DocumentNode, options: Pick<WatchQueryOptions, "variables" | "fetchPolicy" | "errorPolicy">, cacheWriteBehavior: CacheWriteBehavior): void;
// (undocumented)
networkError?: Error | null;
// (undocumented)
Expand Down
2 changes: 1 addition & 1 deletion .api-reports/api-report-react_context.md
Original file line number Diff line number Diff line change
Expand Up @@ -1202,7 +1202,7 @@ class QueryInfo {
// Warning: (ae-forgotten-export) The symbol "CacheWriteBehavior" needs to be exported by the entry point index.d.ts
//
// (undocumented)
markResult<T>(result: FetchResult<T>, document: DocumentNode, options: Pick<WatchQueryOptions, "variables" | "fetchPolicy" | "errorPolicy">, cacheWriteBehavior: CacheWriteBehavior): typeof result;
markResult<T>(result: FetchResult<T>, document: DocumentNode, options: Pick<WatchQueryOptions, "variables" | "fetchPolicy" | "errorPolicy">, cacheWriteBehavior: CacheWriteBehavior): void;
// (undocumented)
networkError?: Error | null;
// (undocumented)
Expand Down
2 changes: 1 addition & 1 deletion .api-reports/api-report-react_hoc.md
Original file line number Diff line number Diff line change
Expand Up @@ -1247,7 +1247,7 @@ class QueryInfo {
// Warning: (ae-forgotten-export) The symbol "CacheWriteBehavior" needs to be exported by the entry point index.d.ts
//
// (undocumented)
markResult<T>(result: FetchResult<T>, document: DocumentNode, options: Pick<WatchQueryOptions, "variables" | "fetchPolicy" | "errorPolicy">, cacheWriteBehavior: CacheWriteBehavior): typeof result;
markResult<T>(result: FetchResult<T>, document: DocumentNode, options: Pick<WatchQueryOptions, "variables" | "fetchPolicy" | "errorPolicy">, cacheWriteBehavior: CacheWriteBehavior): void;
// (undocumented)
networkError?: Error | null;
// (undocumented)
Expand Down
2 changes: 1 addition & 1 deletion .api-reports/api-report-react_hooks.md
Original file line number Diff line number Diff line change
Expand Up @@ -1452,7 +1452,7 @@ class QueryInfo {
// Warning: (ae-forgotten-export) The symbol "CacheWriteBehavior" needs to be exported by the entry point index.d.ts
//
// (undocumented)
markResult<T>(result: FetchResult<T>, document: DocumentNode, options: Pick<WatchQueryOptions, "variables" | "fetchPolicy" | "errorPolicy">, cacheWriteBehavior: CacheWriteBehavior): typeof result;
markResult<T>(result: FetchResult<T>, document: DocumentNode, options: Pick<WatchQueryOptions, "variables" | "fetchPolicy" | "errorPolicy">, cacheWriteBehavior: CacheWriteBehavior): void;
// (undocumented)
networkError?: Error | null;
// (undocumented)
Expand Down
2 changes: 1 addition & 1 deletion .api-reports/api-report-react_internal.md
Original file line number Diff line number Diff line change
Expand Up @@ -1215,7 +1215,7 @@ class QueryInfo {
// Warning: (ae-forgotten-export) The symbol "CacheWriteBehavior" needs to be exported by the entry point index.d.ts
//
// (undocumented)
markResult<T>(result: FetchResult<T>, document: DocumentNode, options: Pick<WatchQueryOptions, "variables" | "fetchPolicy" | "errorPolicy">, cacheWriteBehavior: CacheWriteBehavior): typeof result;
markResult<T>(result: FetchResult<T>, document: DocumentNode, options: Pick<WatchQueryOptions, "variables" | "fetchPolicy" | "errorPolicy">, cacheWriteBehavior: CacheWriteBehavior): void;
// (undocumented)
networkError?: Error | null;
// (undocumented)
Expand Down
2 changes: 1 addition & 1 deletion .api-reports/api-report-react_ssr.md
Original file line number Diff line number Diff line change
Expand Up @@ -1187,7 +1187,7 @@ class QueryInfo {
// Warning: (ae-forgotten-export) The symbol "CacheWriteBehavior" needs to be exported by the entry point index.d.ts
//
// (undocumented)
markResult<T>(result: FetchResult<T>, document: DocumentNode, options: Pick<WatchQueryOptions, "variables" | "fetchPolicy" | "errorPolicy">, cacheWriteBehavior: CacheWriteBehavior): typeof result;
markResult<T>(result: FetchResult<T>, document: DocumentNode, options: Pick<WatchQueryOptions, "variables" | "fetchPolicy" | "errorPolicy">, cacheWriteBehavior: CacheWriteBehavior): void;
// (undocumented)
networkError?: Error | null;
// (undocumented)
Expand Down
2 changes: 1 addition & 1 deletion .api-reports/api-report-testing.md
Original file line number Diff line number Diff line change
Expand Up @@ -1264,7 +1264,7 @@ class QueryInfo {
// Warning: (ae-forgotten-export) The symbol "CacheWriteBehavior" needs to be exported by the entry point index.d.ts
//
// (undocumented)
markResult<T>(result: FetchResult<T>, document: DocumentNode, options: Pick<WatchQueryOptions, "variables" | "fetchPolicy" | "errorPolicy">, cacheWriteBehavior: CacheWriteBehavior): typeof result;
markResult<T>(result: FetchResult<T>, document: DocumentNode, options: Pick<WatchQueryOptions, "variables" | "fetchPolicy" | "errorPolicy">, cacheWriteBehavior: CacheWriteBehavior): void;
// (undocumented)
networkError?: Error | null;
// (undocumented)
Expand Down
2 changes: 1 addition & 1 deletion .api-reports/api-report-testing_core.md
Original file line number Diff line number Diff line change
Expand Up @@ -1219,7 +1219,7 @@ class QueryInfo {
// Warning: (ae-forgotten-export) The symbol "CacheWriteBehavior" needs to be exported by the entry point index.d.ts
//
// (undocumented)
markResult<T>(result: FetchResult<T>, document: DocumentNode, options: Pick<WatchQueryOptions, "variables" | "fetchPolicy" | "errorPolicy">, cacheWriteBehavior: CacheWriteBehavior): typeof result;
markResult<T>(result: FetchResult<T>, document: DocumentNode, options: Pick<WatchQueryOptions, "variables" | "fetchPolicy" | "errorPolicy">, cacheWriteBehavior: CacheWriteBehavior): void;
// (undocumented)
networkError?: Error | null;
// (undocumented)
Expand Down
2 changes: 1 addition & 1 deletion .api-reports/api-report-utilities.md
Original file line number Diff line number Diff line change
Expand Up @@ -2025,7 +2025,7 @@ class QueryInfo {
// Warning: (ae-forgotten-export) The symbol "CacheWriteBehavior" needs to be exported by the entry point index.d.ts
//
// (undocumented)
markResult<T>(result: FetchResult<T>, document: DocumentNode, options: Pick<WatchQueryOptions, "variables" | "fetchPolicy" | "errorPolicy">, cacheWriteBehavior: CacheWriteBehavior): typeof result;
markResult<T>(result: FetchResult<T>, document: DocumentNode, options: Pick<WatchQueryOptions, "variables" | "fetchPolicy" | "errorPolicy">, cacheWriteBehavior: CacheWriteBehavior): void;
// (undocumented)
networkError?: Error | null;
// (undocumented)
Expand Down
2 changes: 1 addition & 1 deletion .api-reports/api-report.md
Original file line number Diff line number Diff line change
Expand Up @@ -2138,7 +2138,7 @@ class QueryInfo {
// Warning: (ae-forgotten-export) The symbol "CacheWriteBehavior" needs to be exported by the entry point index.d.ts
//
// (undocumented)
markResult<T>(result: FetchResult<T>, document: DocumentNode, options: Pick<WatchQueryOptions, "variables" | "fetchPolicy" | "errorPolicy">, cacheWriteBehavior: CacheWriteBehavior): typeof result;
markResult<T>(result: FetchResult<T>, document: DocumentNode, options: Pick<WatchQueryOptions, "variables" | "fetchPolicy" | "errorPolicy">, cacheWriteBehavior: CacheWriteBehavior): void;
// (undocumented)
networkError?: Error | null;
// (undocumented)
Expand Down
5 changes: 5 additions & 0 deletions .changeset/quick-pears-give.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@apollo/client": patch
---

Revert PR [#11202](https://github.com/apollographql/apollo-client/pull/11202) to fix caching bug reported in [#11560](https://github.com/apollographql/apollo-client/issues/11560)
4 changes: 2 additions & 2 deletions .size-limits.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"dist/apollo-client.min.cjs": 39052,
"import { ApolloClient, InMemoryCache, HttpLink } from \"dist/index.js\" (production)": 32559
"dist/apollo-client.min.cjs": 39043,
"import { ApolloClient, InMemoryCache, HttpLink } from \"dist/index.js\" (production)": 32550
}
5 changes: 1 addition & 4 deletions src/__tests__/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2949,10 +2949,7 @@ describe("client", () => {
return client
.query({ query })
.then(({ data }) => {
const { price, ...todoWithoutPrice } = data.todos[0];
expect(data).toEqual({
todos: [todoWithoutPrice],
});
expect(data).toEqual(result.data);
})
.then(resolve, reject);
});
Expand Down
169 changes: 0 additions & 169 deletions src/cache/inmemory/__tests__/client.ts

This file was deleted.

23 changes: 10 additions & 13 deletions src/core/QueryInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -364,8 +364,7 @@ export class QueryInfo {
"variables" | "fetchPolicy" | "errorPolicy"
>,
cacheWriteBehavior: CacheWriteBehavior
): typeof result {
result = { ...result };
) {
const merger = new DeepMerger();
const graphQLErrors =
isNonEmptyArray(result.errors) ? result.errors.slice(0) : [];
Expand Down Expand Up @@ -411,10 +410,7 @@ export class QueryInfo {
});

this.lastWrite = {
// 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 },
result,
variables: options.variables,
dmCount: destructiveMethodCounts.get(this.cache),
};
Expand Down Expand Up @@ -476,19 +472,20 @@ export class QueryInfo {
this.updateWatch(options.variables);
}

// If we're allowed to write to the cache, update result.data to be
// the result as re-read from the cache, rather than the raw network
// result. Set without setDiff to avoid triggering a notify call,
// since we have other ways of notifying for this result.
// If we're allowed to write to the cache, and we can read a
// complete result from the cache, update result.data to be the
// result from the cache, rather than the raw network result.
// Set without setDiff to avoid triggering a notify call, since
// we have other ways of notifying for this result.
this.updateLastDiff(diff, diffOptions);
result.data = diff.result;
if (diff.complete) {
result.data = diff.result;
}
});
} else {
this.lastWrite = void 0;
}
}

return result;
}

public markReady() {
Expand Down
2 changes: 1 addition & 1 deletion src/core/QueryManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1194,7 +1194,7 @@ 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.
result = queryInfo.markResult(
queryInfo.markResult(
result,
linkDocument,
options,
Expand Down
Loading

0 comments on commit e855d00

Please sign in to comment.