From 7a850ac60d382cce6ca94dea5d86ca2850da2402 Mon Sep 17 00:00:00 2001 From: Lenz Weber-Tronic Date: Mon, 6 Nov 2023 15:44:03 +0100 Subject: [PATCH 1/4] Add `reset` method to `print`, hook up to `ApolloCache.gc` --- .changeset/wild-dolphins-jog.md | 5 +++++ src/cache/inmemory/inMemoryCache.ts | 2 ++ src/utilities/graphql/print.ts | 29 ++++++++++++++++++++--------- 3 files changed, 27 insertions(+), 9 deletions(-) create mode 100644 .changeset/wild-dolphins-jog.md diff --git a/.changeset/wild-dolphins-jog.md b/.changeset/wild-dolphins-jog.md new file mode 100644 index 00000000000..589af17940c --- /dev/null +++ b/.changeset/wild-dolphins-jog.md @@ -0,0 +1,5 @@ +--- +"@apollo/client": patch +--- + +Add `reset` method to `print`, hook up to `ApolloCache.gc` diff --git a/src/cache/inmemory/inMemoryCache.ts b/src/cache/inmemory/inMemoryCache.ts index 3fd230b94e0..3fe7ab56fdf 100644 --- a/src/cache/inmemory/inMemoryCache.ts +++ b/src/cache/inmemory/inMemoryCache.ts @@ -17,6 +17,7 @@ import { isReference, DocumentTransform, canonicalStringify, + print, } from "../../utilities/index.js"; import type { InMemoryCacheConfig, NormalizedCacheObject } from "./types.js"; import { StoreReader } from "./readFromStore.js"; @@ -293,6 +294,7 @@ export class InMemoryCache extends ApolloCache { resetResultIdentities?: boolean; }) { canonicalStringify.reset(); + print.reset(); const ids = this.optimisticData.gc(); if (options && !this.txCount) { if (options.resetResultCache) { diff --git a/src/utilities/graphql/print.ts b/src/utilities/graphql/print.ts index 5fb1cb68599..d90a15611d0 100644 --- a/src/utilities/graphql/print.ts +++ b/src/utilities/graphql/print.ts @@ -1,14 +1,25 @@ +import type { ASTNode } from "graphql"; import { print as origPrint } from "graphql"; import { canUseWeakMap } from "../common/canUse.js"; -const printCache = canUseWeakMap ? new WeakMap() : undefined; -export const print: typeof origPrint = (ast) => { - let result; - result = printCache?.get(ast); +let printCache: undefined | WeakMap; +// further TODO: replace with `optimism` with a `WeakCache` once those are available +export const print = Object.assign( + (ast: ASTNode) => { + let result; + result = printCache?.get(ast); - if (!result) { - result = origPrint(ast); - printCache?.set(ast, result); + if (!result) { + result = origPrint(ast); + printCache?.set(ast, result); + } + return result; + }, + { + reset() { + printCache = canUseWeakMap ? new WeakMap() : undefined; + }, } - return result; -}; +); + +print.reset(); From fdd9721601883d1312d57878bb94138dbf1c37c4 Mon Sep 17 00:00:00 2001 From: Lenz Weber-Tronic Date: Mon, 6 Nov 2023 16:18:31 +0100 Subject: [PATCH 2/4] Update wild-dolphins-jog.md --- .changeset/wild-dolphins-jog.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.changeset/wild-dolphins-jog.md b/.changeset/wild-dolphins-jog.md index 589af17940c..8030414fffe 100644 --- a/.changeset/wild-dolphins-jog.md +++ b/.changeset/wild-dolphins-jog.md @@ -2,4 +2,4 @@ "@apollo/client": patch --- -Add `reset` method to `print`, hook up to `ApolloCache.gc` +Add `reset` method to `print`, hook up to `InMemoryCache.gc` From 22d6c4583c4520ae754e13249a67b2215fc2c5f1 Mon Sep 17 00:00:00 2001 From: Lenz Weber-Tronic Date: Mon, 6 Nov 2023 16:22:20 +0100 Subject: [PATCH 3/4] api-extractor --- .api-reports/api-report-core.md | 5 +++-- .api-reports/api-report-link_batch-http.md | 5 +++-- .api-reports/api-report-link_http.md | 5 +++-- .api-reports/api-report-utilities.md | 5 +++-- .api-reports/api-report.md | 5 +++-- 5 files changed, 15 insertions(+), 10 deletions(-) diff --git a/.api-reports/api-report-core.md b/.api-reports/api-report-core.md index 5fd2e468bf4..935e04317d7 100644 --- a/.api-reports/api-report-core.md +++ b/.api-reports/api-report-core.md @@ -20,7 +20,6 @@ import { InvariantError } from 'ts-invariant'; import { Observable } from 'zen-observable-ts'; import type { Subscription as ObservableSubscription } from 'zen-observable-ts'; import type { Observer } from 'zen-observable-ts'; -import { print as print_3 } from 'graphql'; import { resetCaches } from 'graphql-tag'; import type { SelectionSetNode } from 'graphql'; import { setVerbosity as setLogVerbosity } from 'ts-invariant'; @@ -1614,7 +1613,9 @@ export type PossibleTypesMap = { }; // @public (undocumented) -const print_2: typeof print_3; +const print_2: ((ast: ASTNode) => string) & { + reset(): void; +}; // @public (undocumented) interface Printer { diff --git a/.api-reports/api-report-link_batch-http.md b/.api-reports/api-report-link_batch-http.md index 0e39566b61a..0081bc227be 100644 --- a/.api-reports/api-report-link_batch-http.md +++ b/.api-reports/api-report-link_batch-http.md @@ -10,7 +10,6 @@ import type { ExecutionResult } from 'graphql'; import type { GraphQLError } from 'graphql'; import { Observable } from 'zen-observable-ts'; import type { Observer } from 'zen-observable-ts'; -import { print as print_3 } from 'graphql'; // @public (undocumented) class ApolloLink { @@ -226,7 +225,9 @@ interface Operation { type Path = ReadonlyArray; // @public (undocumented) -const print_2: typeof print_3; +const print_2: ((ast: ASTNode) => string) & { + reset(): void; +}; // @public (undocumented) interface Printer { diff --git a/.api-reports/api-report-link_http.md b/.api-reports/api-report-link_http.md index 09b1e6459dd..d106d1f3e6b 100644 --- a/.api-reports/api-report-link_http.md +++ b/.api-reports/api-report-link_http.md @@ -11,7 +11,6 @@ import type { GraphQLError } from 'graphql'; import { InvariantError } from 'ts-invariant'; import { Observable } from 'zen-observable-ts'; import type { Observer } from 'zen-observable-ts'; -import { print as print_3 } from 'graphql'; // @public (undocumented) class ApolloLink { @@ -261,7 +260,9 @@ export function parseAndCheckHttpResponse(operations: Operation | Operation[]): type Path = ReadonlyArray; // @public (undocumented) -const print_2: typeof print_3; +const print_2: ((ast: ASTNode) => string) & { + reset(): void; +}; // @public (undocumented) interface Printer { diff --git a/.api-reports/api-report-utilities.md b/.api-reports/api-report-utilities.md index 2ec8f95a895..4661d99d1f3 100644 --- a/.api-reports/api-report-utilities.md +++ b/.api-reports/api-report-utilities.md @@ -20,7 +20,6 @@ import { Observable } from 'zen-observable-ts'; import type { Subscription as ObservableSubscription } from 'zen-observable-ts'; import type { Observer } from 'zen-observable-ts'; import type { OperationDefinitionNode } from 'graphql'; -import { print as print_3 } from 'graphql'; import type { SelectionNode } from 'graphql'; import type { SelectionSetNode } from 'graphql'; import type { Subscriber } from 'zen-observable-ts'; @@ -1882,7 +1881,9 @@ type PossibleTypesMap = { type Primitive = null | undefined | string | number | boolean | symbol | bigint; // @public (undocumented) -const print_2: typeof print_3; +const print_2: ((ast: ASTNode) => string) & { + reset(): void; +}; export { print_2 as print } // Warning: (ae-forgotten-export) The symbol "PendingPromise" needs to be exported by the entry point index.d.ts diff --git a/.api-reports/api-report.md b/.api-reports/api-report.md index e48ae78fd4e..227e554222e 100644 --- a/.api-reports/api-report.md +++ b/.api-reports/api-report.md @@ -22,7 +22,6 @@ import { InvariantError } from 'ts-invariant'; import { Observable } from 'zen-observable-ts'; import type { Subscription as ObservableSubscription } from 'zen-observable-ts'; import type { Observer } from 'zen-observable-ts'; -import { print as print_3 } from 'graphql'; import * as React_2 from 'react'; import { ReactNode } from 'react'; import { resetCaches } from 'graphql-tag'; @@ -1952,7 +1951,9 @@ export type PossibleTypesMap = { type Primitive = null | undefined | string | number | boolean | symbol | bigint; // @public (undocumented) -const print_2: typeof print_3; +const print_2: ((ast: ASTNode) => string) & { + reset(): void; +}; // @public (undocumented) interface Printer { From aff5cb09a3cf743b9e874b3386e6edb858635622 Mon Sep 17 00:00:00 2001 From: Lenz Weber-Tronic Date: Mon, 6 Nov 2023 16:26:41 +0100 Subject: [PATCH 4/4] size --- .size-limit.cjs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.size-limit.cjs b/.size-limit.cjs index 031272a0c63..5637af876f6 100644 --- a/.size-limit.cjs +++ b/.size-limit.cjs @@ -1,7 +1,7 @@ const checks = [ { path: "dist/apollo-client.min.cjs", - limit: "38062", + limit: "38074", }, { path: "dist/main.cjs", @@ -10,7 +10,7 @@ const checks = [ { path: "dist/index.js", import: "{ ApolloClient, InMemoryCache, HttpLink }", - limit: "32113", + limit: "32132", }, ...[ "ApolloProvider",