From d5d8cb10c468a1f256286b354a428afd69765c15 Mon Sep 17 00:00:00 2001 From: Lenz Weber-Tronic Date: Thu, 16 Nov 2023 18:09:58 +0100 Subject: [PATCH 1/6] `print`: use `WeakCache` instead of `WeakMap` --- .changeset/polite-avocados-warn.md | 5 +++++ .size-limit.cjs | 5 +++-- package-lock.json | 12 ++++++++++++ package.json | 1 + src/utilities/graphql/print.ts | 13 +++++-------- 5 files changed, 26 insertions(+), 10 deletions(-) create mode 100644 .changeset/polite-avocados-warn.md diff --git a/.changeset/polite-avocados-warn.md b/.changeset/polite-avocados-warn.md new file mode 100644 index 00000000000..dd04015cf3d --- /dev/null +++ b/.changeset/polite-avocados-warn.md @@ -0,0 +1,5 @@ +--- +"@apollo/client": patch +--- + +`print`: use `WeakCache` instead of `WeakMap` diff --git a/.size-limit.cjs b/.size-limit.cjs index 63819405fd1..e2233d201f3 100644 --- a/.size-limit.cjs +++ b/.size-limit.cjs @@ -1,7 +1,7 @@ const checks = [ { path: "dist/apollo-client.min.cjs", - limit: "38164", + limit: "38178", }, { path: "dist/main.cjs", @@ -10,7 +10,7 @@ const checks = [ { path: "dist/index.js", import: "{ ApolloClient, InMemoryCache, HttpLink }", - limit: "32188", + limit: "32206", }, ...[ "ApolloProvider", @@ -35,6 +35,7 @@ const checks = [ "react", "react-dom", "@graphql-typed-document-node/core", + "@wry/caches", "@wry/context", "@wry/equality", "@wry/trie", diff --git a/package-lock.json b/package-lock.json index 9cf8221c770..2217dbde199 100644 --- a/package-lock.json +++ b/package-lock.json @@ -11,6 +11,7 @@ "license": "MIT", "dependencies": { "@graphql-typed-document-node/core": "^3.1.1", + "@wry/caches": "^1.0.0", "@wry/context": "^0.7.3", "@wry/equality": "^0.5.6", "@wry/trie": "^0.4.3", @@ -3294,6 +3295,17 @@ "url": "https://opencollective.com/typescript-eslint" } }, + "node_modules/@wry/caches": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/@wry/caches/-/caches-1.0.0.tgz", + "integrity": "sha512-FHRUDe2tqrXAj6A/1D39No68lFWbbnh+NCpG9J/6idhL/2Mb/AaxBTYg/sbUVImEo8a4mWeOewUlB1W7uLjByA==", + "dependencies": { + "tslib": "^2.3.0" + }, + "engines": { + "node": ">=8" + } + }, "node_modules/@wry/context": { "version": "0.7.3", "resolved": "https://registry.npmjs.org/@wry/context/-/context-0.7.3.tgz", diff --git a/package.json b/package.json index 3a7d6f0196f..da96797c4ea 100644 --- a/package.json +++ b/package.json @@ -89,6 +89,7 @@ }, "dependencies": { "@graphql-typed-document-node/core": "^3.1.1", + "@wry/caches": "^1.0.0", "@wry/context": "^0.7.3", "@wry/equality": "^0.5.6", "@wry/trie": "^0.4.3", diff --git a/src/utilities/graphql/print.ts b/src/utilities/graphql/print.ts index d90a15611d0..572584e4ca3 100644 --- a/src/utilities/graphql/print.ts +++ b/src/utilities/graphql/print.ts @@ -1,23 +1,20 @@ import type { ASTNode } from "graphql"; import { print as origPrint } from "graphql"; -import { canUseWeakMap } from "../common/canUse.js"; +import { WeakCache } from "@wry/caches" -let printCache: undefined | WeakMap; -// further TODO: replace with `optimism` with a `WeakCache` once those are available +let printCache!: WeakCache; export const print = Object.assign( (ast: ASTNode) => { - let result; - result = printCache?.get(ast); + let result = printCache.get(ast); if (!result) { - result = origPrint(ast); - printCache?.set(ast, result); + printCache.set(ast, result = origPrint(ast)); } return result; }, { reset() { - printCache = canUseWeakMap ? new WeakMap() : undefined; + printCache = new WeakCache(/** TODO: decide on a maximum size (will do all max sizes in a combined separate PR) */); }, } ); From 29bf59da53b2846f9f713e93000c9d1d16d6b0a1 Mon Sep 17 00:00:00 2001 From: Lenz Weber-Tronic Date: Thu, 16 Nov 2023 18:11:41 +0100 Subject: [PATCH 2/6] format --- src/utilities/graphql/print.ts | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/utilities/graphql/print.ts b/src/utilities/graphql/print.ts index 572584e4ca3..d536ff8bb77 100644 --- a/src/utilities/graphql/print.ts +++ b/src/utilities/graphql/print.ts @@ -1,6 +1,6 @@ import type { ASTNode } from "graphql"; import { print as origPrint } from "graphql"; -import { WeakCache } from "@wry/caches" +import { WeakCache } from "@wry/caches"; let printCache!: WeakCache; export const print = Object.assign( @@ -8,13 +8,16 @@ export const print = Object.assign( let result = printCache.get(ast); if (!result) { - printCache.set(ast, result = origPrint(ast)); + printCache.set(ast, (result = origPrint(ast))); } return result; }, { reset() { - printCache = new WeakCache(/** TODO: decide on a maximum size (will do all max sizes in a combined separate PR) */); + printCache = new WeakCache< + ASTNode, + string + >(/** TODO: decide on a maximum size (will do all max sizes in a combined separate PR) */); }, } ); From bd40e8e4f16b6ae69249d41c539eb16bfa72794c Mon Sep 17 00:00:00 2001 From: Lenz Weber-Tronic Date: Fri, 17 Nov 2023 15:48:22 +0100 Subject: [PATCH 3/6] update size limit --- .size-limits.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.size-limits.json b/.size-limits.json index f71b98fe37c..8a945778178 100644 --- a/.size-limits.json +++ b/.size-limits.json @@ -1,4 +1,4 @@ { - "dist/apollo-client.min.cjs": 37975, - "import { ApolloClient, InMemoryCache, HttpLink } from \"dist/index.js\" (production)": 32019 + "dist/apollo-client.min.cjs": 38178, + "import { ApolloClient, InMemoryCache, HttpLink } from \"dist/index.js\" (production)": 32206 } From f84da806411314c19a9fda8d76bf1e9bd46833c1 Mon Sep 17 00:00:00 2001 From: Lenz Weber-Tronic Date: Fri, 17 Nov 2023 15:56:17 +0100 Subject: [PATCH 4/6] size-limit --- .size-limits.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.size-limits.json b/.size-limits.json index 52108cf4fb9..8a945778178 100644 --- a/.size-limits.json +++ b/.size-limits.json @@ -1,4 +1,4 @@ { - "dist/apollo-client.min.cjs": 38164, - "import { ApolloClient, InMemoryCache, HttpLink } from \"dist/index.js\" (production)": 32188 + "dist/apollo-client.min.cjs": 38178, + "import { ApolloClient, InMemoryCache, HttpLink } from \"dist/index.js\" (production)": 32206 } From 4b8587c05943c0ebef340110c9b8e1bb1bada3f6 Mon Sep 17 00:00:00 2001 From: Lenz Weber-Tronic Date: Wed, 29 Nov 2023 11:31:34 +0100 Subject: [PATCH 5/6] Update src/utilities/graphql/print.ts Co-authored-by: Jerel Miller --- src/utilities/graphql/print.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/utilities/graphql/print.ts b/src/utilities/graphql/print.ts index d536ff8bb77..3260781a79e 100644 --- a/src/utilities/graphql/print.ts +++ b/src/utilities/graphql/print.ts @@ -8,7 +8,8 @@ export const print = Object.assign( let result = printCache.get(ast); if (!result) { - printCache.set(ast, (result = origPrint(ast))); + result = origPrint(ast) + printCache.set(ast, result); } return result; }, From 3b60d3a8033dc103e7393883040277548d595824 Mon Sep 17 00:00:00 2001 From: phryneas Date: Wed, 29 Nov 2023 10:36:43 +0000 Subject: [PATCH 6/6] Clean up Prettier, Size-limit, and Api-Extractor --- .size-limits.json | 4 ++-- src/utilities/graphql/print.ts | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/.size-limits.json b/.size-limits.json index 7bc50667da7..d5dd8296590 100644 --- a/.size-limits.json +++ b/.size-limits.json @@ -1,4 +1,4 @@ { - "dist/apollo-client.min.cjs": 38600, - "import { ApolloClient, InMemoryCache, HttpLink } from \"dist/index.js\" (production)": 32187 + "dist/apollo-client.min.cjs": 38603, + "import { ApolloClient, InMemoryCache, HttpLink } from \"dist/index.js\" (production)": 32203 } diff --git a/src/utilities/graphql/print.ts b/src/utilities/graphql/print.ts index 3260781a79e..3ba1134c968 100644 --- a/src/utilities/graphql/print.ts +++ b/src/utilities/graphql/print.ts @@ -8,7 +8,7 @@ export const print = Object.assign( let result = printCache.get(ast); if (!result) { - result = origPrint(ast) + result = origPrint(ast); printCache.set(ast, result); } return result;