Skip to content

Turn off structural sharing for bigint queries #404

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from

Conversation

smaye81
Copy link
Member

@smaye81 smaye81 commented Sep 3, 2024

Tanstack seems to have added a noisy console.error log for queries that return data that is not JSON-serializable. They recommend to either turn off structuralSharing in your queries or to return JSON serializable data from your queryFn.

For the sake of simplicity, we are simply turning off structuralSharing in the tests that return a BigInt as part of their response.

@smaye81 smaye81 requested a review from timostamm September 3, 2024 16:09
@smaye81
Copy link
Member Author

smaye81 commented Sep 3, 2024

@timostamm was seeing a noisy log after upgrading tanstack-query that said:

StructuralSharing requires data to be JSON serializable. To fix this, turn off structuralSharing or return JSON-serializable data from your queryFn. [@"connectrpc.eliza.v1.PaginatedService","List",#page:0,,"infinite",]: TypeError: Do not know how to serialize a BigInt

We could also implement a select function here to convert the returned data, so if you'd rather do that, lmk.

Signed-off-by: Steve Ayers <sayers@buf.build>
Signed-off-by: Steve Ayers <sayers@buf.build>
@smaye81 smaye81 force-pushed the sayers/struct_sharing branch from d077c52 to 2f12c77 Compare September 3, 2024 16:11
@paul-sachs
Copy link
Collaborator

I think we can enable this in the default options we provide. Just on my phone so don't have a link but it should work if we add it there.

@smaye81
Copy link
Member Author

smaye81 commented Sep 3, 2024

Do you mean adding it here?

Would we want to disable this for all queries though by default? Or is this only used on a case-by-case basis and not added automatically to every query.

@paul-sachs
Copy link
Collaborator

Given the current default options hash function is also only really needed for bigint support, I think it follows that disabling structural sharing would be also be required.

Copy link
Member

@timostamm timostamm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's take a look at the feature before we turn it off:

React Query uses a technique called "structural sharing" to ensure that as many references as possible will be kept intact between re-renders. If data is fetched over the network, usually, you'll get a completely new reference by json parsing the response. However, React Query will keep the original reference if nothing changed in the data. If a subset changed, React Query will keep the unchanged parts and only replace the changed parts.

Given that React Query invalidates queries somewhat regularly, it would be nice not to re-render if the data hasn't changed. This can be implemented with a per-query option that uses protobuf-es' equals().

It might be better to defer until we finish compat with protobuf-es v2, since it will also require changes to query keys and the safe updater. I suggest we create an issue to track support for structural sharing - and mention how to disable the feature.

@paul-sachs
Copy link
Collaborator

It might be better to defer until we finish compat with protobuf-es v2

I'd be down for that

@smaye81
Copy link
Member Author

smaye81 commented Sep 4, 2024

OK, I'll close this PR and create an issue instead to figure out a better solution.

@smaye81 smaye81 mentioned this pull request Sep 4, 2024
@smaye81 smaye81 closed this Sep 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants