Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix obscure warning in useFragment when cache.identify returns undefined #12052

Merged
merged 4 commits into from
Sep 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/forty-news-turn.md
Original file line number Diff line number Diff line change
@@ -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')`.
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": 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
}
11 changes: 10 additions & 1 deletion src/cache/core/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,16 @@ export abstract class ApolloCache<TSerialized> implements DataProxy {
const diffOptions: Cache.DiffOptions<TData, TVars> = {
...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.
typeof from === "undefined" || typeof from === "string" ?
from
: this.identify(from),
query,
optimistic,
};
Expand Down
28 changes: 28 additions & 0 deletions src/react/hooks/__tests__/useFragment.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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(<ProfiledHook />, {
wrapper: ({ children }) => (
<MockedProvider cache={cache}>{children}</MockedProvider>
),
});

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`", () => {
Expand Down
Loading