Skip to content

Commit

Permalink
Fix warning when cache.identify returns undefined
Browse files Browse the repository at this point in the history
  • Loading branch information
jerelmiller committed Sep 3, 2024
1 parent 40d21f1 commit 8fdbb5f
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
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 when passing `undefined` to `this.identify` below.
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

0 comments on commit 8fdbb5f

Please sign in to comment.