Skip to content

Commit

Permalink
Move default back to useFragment
Browse files Browse the repository at this point in the history
  • Loading branch information
jerelmiller committed Sep 3, 2024
1 parent b4e5568 commit 74674ec
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 11 deletions.
11 changes: 1 addition & 10 deletions src/cache/core/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -229,16 +229,7 @@ export abstract class ApolloCache<TSerialized> implements DataProxy {
const diffOptions: Cache.DiffOptions<TData, TVars> = {
...otherOptions,
returnPartialData: true,
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),
id: typeof from === "string" ? from : this.identify(from),
query,
optimistic,
};
Expand Down
12 changes: 11 additions & 1 deletion src/react/hooks/useFragment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,17 @@ function _useFragment<TData = any, TVars = OperationVariables>(
[cache, from]
);

const stableOptions = useDeepMemo(() => ({ ...rest, from: id! }), [rest, id]);
const stableOptions = useDeepMemo(
// We default the `id` to a falsey string value to ensure `undefined` does
// not get passed to cache.identify in cache.watchFragment. If that happens,
// an obscure warning is displayed to the user
// (https://github.com/apollographql/apollo-client/pull/12052).
//
// `ROOT_QUERY` will be used as the default `id` in `cache.diff` if `id`
// is a falsey value.
() => ({ ...rest, from: id || "" }),
[rest, id]
);

// Since .next is async, we need to make sure that we
// get the correct diff on the next render given new diffOptions
Expand Down

0 comments on commit 74674ec

Please sign in to comment.