-
Hi... I'm just getting into react-query, and have a question about how best to compose dependent queries. I've looked at the examples, and am using the recommended approach of setting up custom hooks for each query. All of my API calls depend on at least two other pieces of data - a base URL, and the user's JWT auth token. Those get fetched through individual queries, like so:
So far, so good, this all works as expected. When composing these into a dependent query though, there's a weird behavior I've noticed. That query is set up like so:
When the api call runs the first time, neither the config nor auth queries have previously run, so this kicks those off. Using the dev tools, it looks like this creates 3 separate api queries with different keys, as each of the dependencies resolves, which look like:
The first 2 queries with null params are disabled and inactive, have 0 observers, and eventually get garbage collected after a few minutes. Only the last query with all params actuallly fires, so this seems to work OK...? The unnecessary queries were bugging me though, so I tried to mitigate that by setting the cacheTime to zero until the query becomes enabled:
This seems to work to kill off the inactive queries, as they get garbage collected immediately. Feels a little hacky though, and maybe unnecessary? I'm wondering if this is the expected behavior, and if this is a legit way to handle this, or if there's a better way to set up these kinds of queries? Am I overthinking this? I have a lot of API calls I'm porting over to react-query, so I want to make sure I set those up with a solid pattern from the outset. Thanks for any feedback! |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
This is a very comprehensive post / question. I thought a bit about it, but I couldn't really come up with a better way than you already have. I don't think the extra entries in the cache are a problem. Only thing you could do is not add baseUrl and token to your key. This depends on what you want to do when those keys change. If the baseUrl is static per deployment and getting a new jwt token should not trigger a hard loading state (which I would assume), then you can do:
But if the tokens are issued once per user, and you get a new token when a new user logs in, you actually do want it as part of the key to get a new entry in the cache. Everything that the fetch function received should be almost always part of the key, so I like your solution... |
Beta Was this translation helpful? Give feedback.
-
Thanks so much for the feedback! Since I'm just bashing my way through this stuff on my own, it's great to get input from someone with more experience with the library and best practices around it. You're right about your assumptions - the base URL is environment specific, and loaded from a static json file that is generated during the deployment, and will never change throughout the lifetime of the app. The JWT though, could get silently refreshed in the background at any point during the session, so I would just want that to pick up on the API calls without needing to show the user a loading state. If the hook is only going to create a couple of extra entries, and, presumably, only the first time they are called - as config & auth should be ready the next time a dependent query needs them - then that's fine. I think I'll probably end up prefetching those at the parent level, since a failure to load either would be fatal to the app. But I wanted to check that I'm not writing code in a way that's going to generate a bunch of useless objects every time a query runs, or setting up a bad pattern that's going to get repeated a lot in the codebase. I do like the idea of using an intermediate function to explicitly pass args to the actual query function instead of just relying on using query key values for everything. Some of my API calls may need to pass in a bunch of extra params to build routes / querystrings / headers / etc, so that could be a useful technique. So, thanks again! React-query is turning out to be a pretty awesome library, and seems to have a great community around it as well. Cheers! |
Beta Was this translation helpful? Give feedback.
This is a very comprehensive post / question. I thought a bit about it, but I couldn't really come up with a better way than you already have.
I don't think the extra entries in the cache are a problem.
Only thing you could do is not add baseUrl and token to your key. This depends on what you want to do when those keys change. If the baseUrl is static per deployment and getting a new jwt token should not trigger a hard loading state (which I would assume), then you can do: