Query options getter / setters in queries hooks #3685
Replies: 1 comment
-
This is not a "search" operation - it's basically accessing a js object by a key. Yes we could pass the "previousData" to the QueryFunctionContext, but we would basically do the same thing. I think calling
we certainly can't just mutate if you want to control query options by a response from the current query, I would suggest something like this: https://twitter.com/TkDodo/status/1378732290789011457
it doesn't have to come from a header of course, it can also be set by something that's part of the regular response data. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Here is my use case :
I have an endpoint to stay authenticated :
/sessions/active
Which take a bearer Token in the Authorization HTTP header and returns a new refreshed bearer token with an expiration date and other datas of the session (userId, ...).
I want to build a hook that returns the current session and automatically refresh it before it expires. I began with something like that :
But doing so I faced two issues :
queryClient.getQueryData('session')
. It feels heavy to search through the entire client considering that I'm already in the context I need.What I would propose is to pass down more context to the query function and to the hooks. It could look like something like this :
To support this last point I think it's a better pattern to let the backend decides the lifetime of datas. The nature of datas, their modifications rates etc is known by the backend and could vary over time. HTTP defines a whole range of cache / stale headers that are really usefull to pass those informations.
What do you think about it ?
Beta Was this translation helpful? Give feedback.
All reactions