Skip to content

Commit

Permalink
do partialRefetch directly in the render execution
Browse files Browse the repository at this point in the history
  • Loading branch information
brainkim committed Aug 10, 2021
1 parent 3ebf11e commit 482a98d
Showing 1 changed file with 15 additions and 22 deletions.
37 changes: 15 additions & 22 deletions src/react/hooks/useQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -273,34 +273,27 @@ export function useQuery<

let partial: boolean | undefined;
({ partial, ...result } = result);
// An effect which refetches queries when there is incomplete data in the
// cache.
//
// TODO: This effect should be removed when the partialRefetch option is
// removed.
useEffect(() => {
// When a `Query` component is mounted, and a mutation is executed
// that returns the same ID as the mounted `Query`, but has less
// fields in its result, Apollo Client's `QueryManager` returns the
// data as `undefined` since a hit can't be found in the cache.
// This can lead to application errors when the UI elements rendered by
// the original `Query` component are expecting certain data values to
// exist, and they're all of a sudden stripped away. To help avoid
// this we'll attempt to refetch the `Query` data.
{
// TODO: This code should be removed when the partialRefetch option is
// removed.
// I was unable to get this hook to behave reasonably when this block was
// put in an effect, so we’re doing side effects in the render. Forgive me.
if (
partialRefetch &&
partial &&
partialRefetch &&
!result.loading &&
(!result.data || Object.keys(result.data).length === 0) &&
obsQuery.options.fetchPolicy !== 'cache-only'
) {
setTimeout(() => obsQuery.refetch());
result = {
...result,
loading: true,
networkStatus: NetworkStatus.refetch,
};

obsQuery.refetch();
}
}, [
partialRefetch,
partial,
result.data,
obsQuery.options.fetchPolicy,
]);
}

if (
ssr === false &&
Expand Down

0 comments on commit 482a98d

Please sign in to comment.