Opening this PR for discussion, I'm very open to scope changes.
This PR tackles two problems with `defaultStructuralSharing: true`:
1. symbol properties are not taken into account for "object equality"
and are not copied over when creating a new copy
2. non-enumerable properties are not taken into account for "object
equality" and are not copied over when creating a new copy
Point 1. actually is causing some of our users a headache, see
https://github.com/apollographql/apollo-client/issues/12619 and
https://community.apollographql.com/t/preloadquery-with-tanstack-router-loader/9100
.
Users expect that they're able to pass an object with symbol properties
over from a loader into components, but when having
`defaultStructuralSharing` enabled, from the second object that is
passed over, it creates a copy and strips off symbol properties.
That new object without the expected symbol properties will then crash
our `useReadQuery` hook.
In other words, the first loader call returns
```js
{
refObject: { toPromise: Function1, [secretSymbol]: ImplementationDetail1 }
}
```
the component receives
```js
{
refObject: { toPromise: Function1, [secretSymbol]: ImplementationDetail1 }
}
```
and the second loader call returns
```js
{
refObject: { toPromise: Function2, [secretSymbol]: ImplementationDetail2 }
}
```
but the component receives
```js
{
refObject: { toPromise: Function2 }
}
```
`toPromise` is a different function, so it goes into the "clone" path,
but skips the `secretSymbol` property.
If we wouldn't have that `toPromise` property on there (and we're
considering removing it!), it would even be worse to debug - users would
always stay on the first `refObject` and never get the second, since
both objects would be considered equal.
On our side, passing these objects from loaders to components is
actually an intended pattern, so this is causing quite a bit of problems
- see this example from our docs (see [Initiating queries outside
React](https://www.apollographql.com/docs/react/data/suspense#initiating-queries-outside-react)):
```ts
import { useLoaderData } from 'react-router-dom';
export function loader() {
return preloadQuery(GET_DOGS_QUERY);
}
export function RouteComponent() {
const queryRef = useLoaderData();
const { data } = useReadQuery(queryRef);
return (
// ...
);
}
```
Problem 2. is purely hypothetical and we haven't encountered it - but it
should probably be handled in some way.
Both of these problems can be handled in two ways:
* take these "special properties" into account for comparisons and copy
them over (my naive approach here would lose the "non-enumerability" of
these properties, more code would be necessary to keep that)
* in `isPlainObject` just return `false` if an object has non-enumerable
or symbol properties. This would opt out of structural sharing for these
objects (which is probably perfectly fine)
I'd be open for either of those solutions, and also for completely
dropping the "non-enumerable" case for simplicity, but I would be very
happy if we could get *something* in to help with symbol properties.
In React Query, these values can be assumed to be serializable JSON, so
the current implementation is perfectly fine, but with client-side
loaders, users can just pass anything, and here it's causing quite the
problem.
Telling our users to situationally turn off structural sharing would be
an educational nightmare.
This allows `Route.Link` and `routeApi.Link` bindings instead of manual
`from`.
We never used to be able to do this due to circular issues but it seems
this is no longer an issue since all the changes to the types
---------
Co-authored-by: TkDodo <office@dorfmeister.cc>