Skipped Query returns old data after cache clearing

I have query to get user data (roles, username)

   const token = getCookie(CookieNames.TOKEN);
   const { data, refetch, loading } = useUserGetCurrentQuery({
     skip: !token,
   });

Also I have storeClearing on logout

export const logout = async () => {
  const apolloClient = initializeApollo();

  removeCookie(CookieNames.TOKEN);
  localStorage.deleteItem('refreshToken');

  logoutEvent();

  await apolloClient.clearStore();
  await redirect({ route: routes.AUTH });
};

After cache clearing on auth page we call getUserData hook. As token is empty I expected, what query will skipped and data would be undefined. But it still returns old data, cache is clear though.

Any help please, I blew my mind :exploding_head:

Answer from issue with this problem:

If your hook was mounted all that time, that is possible.

Generally, cache updates are forwarded to the hooks, but skipping a hook also means skipping any updates from the cache - including a cache reset.

If the component with that hook would be unmounted and remounted, it would start with a blank state, but since it seems to stay mounted in your case, the moment you “un-skip”, it will pick back up with the last state it had and wait for future cache updates.

Looks like intended behavior. I clear cache, and then call query with skip: true. Apollo can’t get new data, so it returns the last one it has.
Further discussion will be in issue

1 Like