Provide a context object (client, queryKey, meta) to the placeholderData function #11140
Unanswered
RodolfoSilva
asked this question in
Ideas
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Summary
placeholderDatais the only option that produces data without access to theQueryClient. I'd like to propose passing it acontextargument, matching whatqueryFnand the mutation callbacks already receive.Current signature
Proposal
The parameter is added at the end, so existing code keeps working.
Why
queryFnreceivesQueryFunctionContext, which carriesclient,queryKey,signalandmeta.mutationFnand every mutation callback receivedMutationFunctionContext(client,meta,mutationKey) in #9615, shipped in v5.89.0.placeholderDatawas left out of that convergence.The practical consequence is that the "placeholder data from cache" example in the docs can only live inside a component:
That collides with the
queryOptionspattern the docs recommend elsewhere. To move these options into a shared file today, the client has to be threaded in by hand:And that spreads. Every caller now needs a client just to build the options: components,
prefetchQueryon the server, route loaders, tests. ThequeryFnsitting right next to it never asked for one. The options object also stops being a plain value, which makes it harder to reuse across adapters and to keep next to the rest of the query definition.With a context argument, the factory depends only on its own arguments again:
Why the existing parameters don't cover this
previousQueryisundefinedon the first mount, which is exactly when you want to seed from another cache entry. When it is defined, the client sits in a private field (#clientonQuery), so there is no way to read it from there. The same applies toqueryKeyandmeta.#10427 is open and asks for the current query as a parameter for a related reason: a globally configured
placeholderDatahas no way to know which key it is running for. One context object covers both cases and avoids adding another positional parameter later.Implementation
The call site in
QueryObserver#createResultalready holds everything it needs:I can send a PR with tests and a changeset if this direction makes sense.
All reactions