`shallow` doesn't compare symbol properties #226
Describe the bug#
While investigating an issue in Apollo Client (https://github.com/apollographql/apollo-client/issues/12885) for a feature that integrates with @tanstack/react-router, we found that an old object was returned from the loader function when changing params. Apollo Client has a preloadQuery API that makes it possible to begin fetching in a loader function and use the returned object to then receive additional cache updates.
That preloadQuery function returns an object we call a queryRef, which is an opaque object used by other hooks in Apollo Client's library that provides information about the fetched query. This information is buried in symbol keys. With our recent release of 4.0, we removed a function property between versions so integrating 4.0 with TanStack router is now causing issues where a stale queryRef is returned from Route.useLoaderData(). I traced it back to the useStore hook provided in this library.
The shallow function used in useStore uses Object.keys to compare keys for the object, but this means symbol keys aren't compared. With the removal of the function property in 4.0, queryRef now only contains symbol keys, so this comparison is doing the equivalent of checking two empty objects.
Steps to Reproduce the Bug or Issue#
See https://github.com/apollographql/apollo-client/issues/12885. This provides the following reproduction:
https://github.com/BrennenRocks/preload-apollo-client-v4. The $countryId.tsx route is where you can see the bug.
Expected behavior#
When new queryRef objects are returned from the loader function, it should use the new queryRef object instead of returning the stale queryRef. shallow should be updated to compare symbol keys as well as string keys.
Screenshots or Videos#
No response
Platform#
- OS: all
- Browser: all
- Version: ^0.7.0
Additional context#
See https://github.com/apollographql/apollo-client/issues/12885 for more context as reported to Apollo Clent.
Note: We could fix this in Apollo Client by adding a property to the queryRef that fails the shallow check, but we'd like to save bundle size if we can by fixing it in this library if at all possible.
crutchcorn(GitHub profile, opens in a new tab) closed this
Sign in to join this conversation.