Skip to content

Commit

Permalink
Add reset method to DocumentTransform, hook `InMemoryCache.addTyp…
Browse files Browse the repository at this point in the history
…enameTransform` up to `InMemoryCache.gc` (#11344)

Co-authored-by: Jerel Miller <jerelmiller@gmail.com>
  • Loading branch information
phryneas and jerelmiller authored Nov 9, 2023
1 parent 950cae8 commit bd26676
Show file tree
Hide file tree
Showing 15 changed files with 46 additions and 10 deletions.
2 changes: 2 additions & 0 deletions .api-reports/api-report-core.md
Original file line number Diff line number Diff line change
Expand Up @@ -601,6 +601,8 @@ export class DocumentTransform {
// (undocumented)
static identity(): DocumentTransform;
// (undocumented)
resetCache(): void;
// (undocumented)
static split(predicate: (document: DocumentNode) => boolean, left: DocumentTransform, right?: DocumentTransform): DocumentTransform;
// (undocumented)
transformDocument(document: DocumentNode): DocumentNode;
Expand Down
4 changes: 3 additions & 1 deletion .api-reports/api-report-react.md
Original file line number Diff line number Diff line change
Expand Up @@ -728,6 +728,8 @@ class DocumentTransform {
// (undocumented)
static identity(): DocumentTransform;
// (undocumented)
resetCache(): void;
// (undocumented)
static split(predicate: (document: DocumentNode) => boolean, left: DocumentTransform, right?: DocumentTransform): DocumentTransform;
// (undocumented)
transformDocument(document: DocumentNode): DocumentNode;
Expand Down Expand Up @@ -2221,7 +2223,7 @@ interface WatchQueryOptions<TVariables extends OperationVariables = OperationVar
// src/core/watchQueryOptions.ts:191:3 - (ae-forgotten-export) The symbol "UpdateQueryFn" needs to be exported by the entry point index.d.ts
// src/react/hooks/useBackgroundQuery.ts:24:3 - (ae-forgotten-export) The symbol "FetchMoreFunction" needs to be exported by the entry point index.d.ts
// src/react/hooks/useBackgroundQuery.ts:25:3 - (ae-forgotten-export) The symbol "RefetchFunction" needs to be exported by the entry point index.d.ts
// src/utilities/graphql/DocumentTransform.ts:122:7 - (ae-forgotten-export) The symbol "DocumentTransformCacheKey" needs to be exported by the entry point index.d.ts
// src/utilities/graphql/DocumentTransform.ts:130:7 - (ae-forgotten-export) The symbol "DocumentTransformCacheKey" needs to be exported by the entry point index.d.ts

// (No @packageDocumentation comment for this package)

Expand Down
4 changes: 3 additions & 1 deletion .api-reports/api-report-react_components.md
Original file line number Diff line number Diff line change
Expand Up @@ -635,6 +635,8 @@ class DocumentTransform {
// (undocumented)
static identity(): DocumentTransform;
// (undocumented)
resetCache(): void;
// (undocumented)
static split(predicate: (document: DocumentNode) => boolean, left: DocumentTransform, right?: DocumentTransform): DocumentTransform;
// (undocumented)
transformDocument(document: DocumentNode): DocumentNode;
Expand Down Expand Up @@ -1753,7 +1755,7 @@ interface WatchQueryOptions<TVariables extends OperationVariables = OperationVar
// src/core/types.ts:178:3 - (ae-forgotten-export) The symbol "MutationQueryReducer" needs to be exported by the entry point index.d.ts
// src/core/types.ts:205:5 - (ae-forgotten-export) The symbol "Resolver" needs to be exported by the entry point index.d.ts
// src/core/watchQueryOptions.ts:191:3 - (ae-forgotten-export) The symbol "UpdateQueryFn" needs to be exported by the entry point index.d.ts
// src/utilities/graphql/DocumentTransform.ts:122:7 - (ae-forgotten-export) The symbol "DocumentTransformCacheKey" needs to be exported by the entry point index.d.ts
// src/utilities/graphql/DocumentTransform.ts:130:7 - (ae-forgotten-export) The symbol "DocumentTransformCacheKey" needs to be exported by the entry point index.d.ts

// (No @packageDocumentation comment for this package)

Expand Down
4 changes: 3 additions & 1 deletion .api-reports/api-report-react_context.md
Original file line number Diff line number Diff line change
Expand Up @@ -618,6 +618,8 @@ class DocumentTransform {
// (undocumented)
static identity(): DocumentTransform;
// (undocumented)
resetCache(): void;
// (undocumented)
static split(predicate: (document: DocumentNode) => boolean, left: DocumentTransform, right?: DocumentTransform): DocumentTransform;
// (undocumented)
transformDocument(document: DocumentNode): DocumentNode;
Expand Down Expand Up @@ -1649,7 +1651,7 @@ interface WatchQueryOptions<TVariables extends OperationVariables = OperationVar
// src/core/types.ts:178:3 - (ae-forgotten-export) The symbol "MutationQueryReducer" needs to be exported by the entry point index.d.ts
// src/core/types.ts:205:5 - (ae-forgotten-export) The symbol "Resolver" needs to be exported by the entry point index.d.ts
// src/core/watchQueryOptions.ts:191:3 - (ae-forgotten-export) The symbol "UpdateQueryFn" needs to be exported by the entry point index.d.ts
// src/utilities/graphql/DocumentTransform.ts:122:7 - (ae-forgotten-export) The symbol "DocumentTransformCacheKey" needs to be exported by the entry point index.d.ts
// src/utilities/graphql/DocumentTransform.ts:130:7 - (ae-forgotten-export) The symbol "DocumentTransformCacheKey" needs to be exported by the entry point index.d.ts

// (No @packageDocumentation comment for this package)

Expand Down
4 changes: 3 additions & 1 deletion .api-reports/api-report-react_hoc.md
Original file line number Diff line number Diff line change
Expand Up @@ -620,6 +620,8 @@ class DocumentTransform {
// (undocumented)
static identity(): DocumentTransform;
// (undocumented)
resetCache(): void;
// (undocumented)
static split(predicate: (document: DocumentNode) => boolean, left: DocumentTransform, right?: DocumentTransform): DocumentTransform;
// (undocumented)
transformDocument(document: DocumentNode): DocumentNode;
Expand Down Expand Up @@ -1694,7 +1696,7 @@ export function withSubscription<TProps extends TGraphQLVariables | {} = {}, TDa
// src/core/types.ts:178:3 - (ae-forgotten-export) The symbol "MutationQueryReducer" needs to be exported by the entry point index.d.ts
// src/core/types.ts:205:5 - (ae-forgotten-export) The symbol "Resolver" needs to be exported by the entry point index.d.ts
// src/core/watchQueryOptions.ts:191:3 - (ae-forgotten-export) The symbol "UpdateQueryFn" needs to be exported by the entry point index.d.ts
// src/utilities/graphql/DocumentTransform.ts:122:7 - (ae-forgotten-export) The symbol "DocumentTransformCacheKey" needs to be exported by the entry point index.d.ts
// src/utilities/graphql/DocumentTransform.ts:130:7 - (ae-forgotten-export) The symbol "DocumentTransformCacheKey" needs to be exported by the entry point index.d.ts

// (No @packageDocumentation comment for this package)

Expand Down
4 changes: 3 additions & 1 deletion .api-reports/api-report-react_hooks.md
Original file line number Diff line number Diff line change
Expand Up @@ -699,6 +699,8 @@ class DocumentTransform {
// (undocumented)
static identity(): DocumentTransform;
// (undocumented)
resetCache(): void;
// (undocumented)
static split(predicate: (document: DocumentNode) => boolean, left: DocumentTransform, right?: DocumentTransform): DocumentTransform;
// (undocumented)
transformDocument(document: DocumentNode): DocumentNode;
Expand Down Expand Up @@ -2115,7 +2117,7 @@ interface WatchQueryOptions<TVariables extends OperationVariables = OperationVar
// src/core/watchQueryOptions.ts:191:3 - (ae-forgotten-export) The symbol "UpdateQueryFn" needs to be exported by the entry point index.d.ts
// src/react/hooks/useBackgroundQuery.ts:24:3 - (ae-forgotten-export) The symbol "FetchMoreFunction" needs to be exported by the entry point index.d.ts
// src/react/hooks/useBackgroundQuery.ts:25:3 - (ae-forgotten-export) The symbol "RefetchFunction" needs to be exported by the entry point index.d.ts
// src/utilities/graphql/DocumentTransform.ts:122:7 - (ae-forgotten-export) The symbol "DocumentTransformCacheKey" needs to be exported by the entry point index.d.ts
// src/utilities/graphql/DocumentTransform.ts:130:7 - (ae-forgotten-export) The symbol "DocumentTransformCacheKey" needs to be exported by the entry point index.d.ts

// (No @packageDocumentation comment for this package)

Expand Down
4 changes: 3 additions & 1 deletion .api-reports/api-report-react_ssr.md
Original file line number Diff line number Diff line change
Expand Up @@ -588,6 +588,8 @@ class DocumentTransform {
// (undocumented)
static identity(): DocumentTransform;
// (undocumented)
resetCache(): void;
// (undocumented)
static split(predicate: (document: DocumentNode) => boolean, left: DocumentTransform, right?: DocumentTransform): DocumentTransform;
// (undocumented)
transformDocument(document: DocumentNode): DocumentNode;
Expand Down Expand Up @@ -1635,7 +1637,7 @@ interface WatchQueryOptions<TVariables extends OperationVariables = OperationVar
// src/core/types.ts:178:3 - (ae-forgotten-export) The symbol "MutationQueryReducer" needs to be exported by the entry point index.d.ts
// src/core/types.ts:205:5 - (ae-forgotten-export) The symbol "Resolver" needs to be exported by the entry point index.d.ts
// src/core/watchQueryOptions.ts:191:3 - (ae-forgotten-export) The symbol "UpdateQueryFn" needs to be exported by the entry point index.d.ts
// src/utilities/graphql/DocumentTransform.ts:122:7 - (ae-forgotten-export) The symbol "DocumentTransformCacheKey" needs to be exported by the entry point index.d.ts
// src/utilities/graphql/DocumentTransform.ts:130:7 - (ae-forgotten-export) The symbol "DocumentTransformCacheKey" needs to be exported by the entry point index.d.ts

// (No @packageDocumentation comment for this package)

Expand Down
4 changes: 3 additions & 1 deletion .api-reports/api-report-testing.md
Original file line number Diff line number Diff line change
Expand Up @@ -582,6 +582,8 @@ class DocumentTransform {
// (undocumented)
static identity(): DocumentTransform;
// (undocumented)
resetCache(): void;
// (undocumented)
static split(predicate: (document: DocumentNode) => boolean, left: DocumentTransform, right?: DocumentTransform): DocumentTransform;
// (undocumented)
transformDocument(document: DocumentNode): DocumentNode;
Expand Down Expand Up @@ -1698,7 +1700,7 @@ export function withWarningSpy<TArgs extends any[], TResult>(it: (...args: TArgs
// src/core/types.ts:178:3 - (ae-forgotten-export) The symbol "MutationQueryReducer" needs to be exported by the entry point index.d.ts
// src/core/types.ts:205:5 - (ae-forgotten-export) The symbol "Resolver" needs to be exported by the entry point index.d.ts
// src/core/watchQueryOptions.ts:191:3 - (ae-forgotten-export) The symbol "UpdateQueryFn" needs to be exported by the entry point index.d.ts
// src/utilities/graphql/DocumentTransform.ts:122:7 - (ae-forgotten-export) The symbol "DocumentTransformCacheKey" needs to be exported by the entry point index.d.ts
// src/utilities/graphql/DocumentTransform.ts:130:7 - (ae-forgotten-export) The symbol "DocumentTransformCacheKey" needs to be exported by the entry point index.d.ts

// (No @packageDocumentation comment for this package)

Expand Down
4 changes: 3 additions & 1 deletion .api-reports/api-report-testing_core.md
Original file line number Diff line number Diff line change
Expand Up @@ -581,6 +581,8 @@ class DocumentTransform {
// (undocumented)
static identity(): DocumentTransform;
// (undocumented)
resetCache(): void;
// (undocumented)
static split(predicate: (document: DocumentNode) => boolean, left: DocumentTransform, right?: DocumentTransform): DocumentTransform;
// (undocumented)
transformDocument(document: DocumentNode): DocumentNode;
Expand Down Expand Up @@ -1654,7 +1656,7 @@ export function withWarningSpy<TArgs extends any[], TResult>(it: (...args: TArgs
// src/core/types.ts:178:3 - (ae-forgotten-export) The symbol "MutationQueryReducer" needs to be exported by the entry point index.d.ts
// src/core/types.ts:205:5 - (ae-forgotten-export) The symbol "Resolver" needs to be exported by the entry point index.d.ts
// src/core/watchQueryOptions.ts:191:3 - (ae-forgotten-export) The symbol "UpdateQueryFn" needs to be exported by the entry point index.d.ts
// src/utilities/graphql/DocumentTransform.ts:122:7 - (ae-forgotten-export) The symbol "DocumentTransformCacheKey" needs to be exported by the entry point index.d.ts
// src/utilities/graphql/DocumentTransform.ts:130:7 - (ae-forgotten-export) The symbol "DocumentTransformCacheKey" needs to be exported by the entry point index.d.ts

// (No @packageDocumentation comment for this package)

Expand Down
2 changes: 2 additions & 0 deletions .api-reports/api-report-utilities.md
Original file line number Diff line number Diff line change
Expand Up @@ -759,6 +759,8 @@ export class DocumentTransform {
// (undocumented)
static identity(): DocumentTransform;
// (undocumented)
resetCache(): void;
// (undocumented)
static split(predicate: (document: DocumentNode) => boolean, left: DocumentTransform, right?: DocumentTransform): DocumentTransform;
// (undocumented)
transformDocument(document: DocumentNode): DocumentNode;
Expand Down
2 changes: 2 additions & 0 deletions .api-reports/api-report.md
Original file line number Diff line number Diff line change
Expand Up @@ -751,6 +751,8 @@ export class DocumentTransform {
// (undocumented)
static identity(): DocumentTransform;
// (undocumented)
resetCache(): void;
// (undocumented)
static split(predicate: (document: DocumentNode) => boolean, left: DocumentTransform, right?: DocumentTransform): DocumentTransform;
// (undocumented)
transformDocument(document: DocumentNode): DocumentNode;
Expand Down
5 changes: 5 additions & 0 deletions .changeset/hot-ducks-burn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@apollo/client": patch
---

Add a `resetCache` method to `DocumentTransform` and hook `InMemoryCache.addTypenameTransform` up to `InMemoryCache.gc`
4 changes: 2 additions & 2 deletions .size-limit.cjs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const checks = [
{
path: "dist/apollo-client.min.cjs",
limit: "38101",
limit: "38124",
},
{
path: "dist/main.cjs",
Expand All @@ -10,7 +10,7 @@ const checks = [
{
path: "dist/index.js",
import: "{ ApolloClient, InMemoryCache, HttpLink }",
limit: "32132",
limit: "32162",
},
...[
"ApolloProvider",
Expand Down
1 change: 1 addition & 0 deletions src/cache/inmemory/inMemoryCache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,7 @@ export class InMemoryCache extends ApolloCache<NormalizedCacheObject> {
}) {
canonicalStringify.reset();
print.reset();
this.addTypenameTransform.resetCache();
const ids = this.optimisticData.gc();
if (options && !this.txCount) {
if (options.resetResultCache) {
Expand Down
8 changes: 8 additions & 0 deletions src/utilities/graphql/DocumentTransform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,14 @@ export class DocumentTransform {
}
}

/**
* Resets the internal cache of this transform, if it has one.
*/
resetCache() {
this.stableCacheKeys =
this.stableCacheKeys && new Trie(canUseWeakMap, (key) => ({ key }));
}

transformDocument(document: DocumentNode) {
// If a user passes an already transformed result back to this function,
// immediately return it.
Expand Down

0 comments on commit bd26676

Please sign in to comment.