From 8fdbb5f53883872d68ad547523a61259e4cbeea0 Mon Sep 17 00:00:00 2001 From: Jerel Miller Date: Tue, 3 Sep 2024 13:54:17 -0600 Subject: [PATCH 1/4] Fix warning when cache.identify returns undefined --- src/cache/core/cache.ts | 11 +++++++- .../hooks/__tests__/useFragment.test.tsx | 28 +++++++++++++++++++ 2 files changed, 38 insertions(+), 1 deletion(-) diff --git a/src/cache/core/cache.ts b/src/cache/core/cache.ts index cb953152c45..21659404799 100644 --- a/src/cache/core/cache.ts +++ b/src/cache/core/cache.ts @@ -229,7 +229,16 @@ export abstract class ApolloCache implements DataProxy { const diffOptions: Cache.DiffOptions = { ...otherOptions, returnPartialData: true, - id: typeof from === "string" ? from : this.identify(from), + id: + // While our TypeScript types do not allow for `undefined` as a valid + // `from`, its possible `useFragment` gives us an `undefined` since it + // calls` cache.identify` and provides that value to `from`. We are + // adding this fix here however to ensure those using plain JavaScript + // and using `cache.identify` themselves will avoid seeing the obscure + // warning when passing `undefined` to `this.identify` below. + typeof from === "undefined" || typeof from === "string" ? + from + : this.identify(from), query, optimistic, }; diff --git a/src/react/hooks/__tests__/useFragment.test.tsx b/src/react/hooks/__tests__/useFragment.test.tsx index 046c1c538c9..378f48f9a4d 100644 --- a/src/react/hooks/__tests__/useFragment.test.tsx +++ b/src/react/hooks/__tests__/useFragment.test.tsx @@ -1725,6 +1725,34 @@ describe("useFragment", () => { }); }); }); + + // https://github.com/apollographql/apollo-client/issues/12051 + it("does not warn when the cache identifier is invalid", async () => { + using _ = spyOnConsole("warn"); + const cache = new InMemoryCache(); + + const ProfiledHook = profileHook(() => + useFragment({ + fragment: ItemFragment, + // Force a value that results in cache.identify === undefined + from: { __typename: "Item" }, + }) + ); + + render(, { + wrapper: ({ children }) => ( + {children} + ), + }); + + expect(console.warn).not.toHaveBeenCalled(); + + const { data, complete } = await ProfiledHook.takeSnapshot(); + + // TODO: Update when https://github.com/apollographql/apollo-client/issues/12003 is fixed + expect(complete).toBe(true); + expect(data).toEqual({}); + }); }); describe("has the same timing as `useQuery`", () => { From 468af8083a065019a5bf900a1cadb6badaaa257f Mon Sep 17 00:00:00 2001 From: Jerel Miller Date: Tue, 3 Sep 2024 14:02:21 -0600 Subject: [PATCH 2/4] Fix regression for warning --- .changeset/forty-news-turn.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/forty-news-turn.md diff --git a/.changeset/forty-news-turn.md b/.changeset/forty-news-turn.md new file mode 100644 index 00000000000..0754e5621d9 --- /dev/null +++ b/.changeset/forty-news-turn.md @@ -0,0 +1,5 @@ +--- +"@apollo/client": patch +--- + +Fixes a regression from where passing an invalid identifier to `from` in `useFragment` would result in the warning `TypeError: Cannot read properties of undefined (reading '__typename')`. From b4e55683fbd9e84aaaf2e373ae9487c69dbba17a Mon Sep 17 00:00:00 2001 From: Jerel Miller Date: Tue, 3 Sep 2024 14:03:46 -0600 Subject: [PATCH 3/4] Minor tweak to comment --- src/cache/core/cache.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cache/core/cache.ts b/src/cache/core/cache.ts index 21659404799..1186605c0b5 100644 --- a/src/cache/core/cache.ts +++ b/src/cache/core/cache.ts @@ -235,7 +235,7 @@ export abstract class ApolloCache implements DataProxy { // calls` cache.identify` and provides that value to `from`. We are // adding this fix here however to ensure those using plain JavaScript // and using `cache.identify` themselves will avoid seeing the obscure - // warning when passing `undefined` to `this.identify` below. + // warning. typeof from === "undefined" || typeof from === "string" ? from : this.identify(from), From e712cc9492811674a452448471abb34373b9ee3a Mon Sep 17 00:00:00 2001 From: Jerel Miller Date: Wed, 4 Sep 2024 10:23:33 -0600 Subject: [PATCH 4/4] Update size limits --- .size-limits.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.size-limits.json b/.size-limits.json index 3cafe22147e..01bb281164e 100644 --- a/.size-limits.json +++ b/.size-limits.json @@ -1,4 +1,4 @@ { - "dist/apollo-client.min.cjs": 40249, - "import { ApolloClient, InMemoryCache, HttpLink } from \"dist/index.js\" (production)": 33058 + "dist/apollo-client.min.cjs": 40252, + "import { ApolloClient, InMemoryCache, HttpLink } from \"dist/index.js\" (production)": 33066 }