T
TanStack2y ago
metropolitan-bronze

checking my understanding: i cannot chain queries without writing them twice for hook and async fn ?

I'd like to be able to define the chain of queries once, and then useFooQuery and fetchFoo both work as intended. Instead, if I have something rather complicated, I have to do:
useDQuery = (x) => {
const a = useAQuery(x).data
const b = useBQuery(a).data
const c = useCQuery(b).data
return useQuery(blahblah(c))
}
useDQuery = (x) => {
const a = useAQuery(x).data
const b = useBQuery(a).data
const c = useCQuery(b).data
return useQuery(blahblah(c))
}
and THEN also, in order to simply fetch this stuff in an async function
fetchD = async (x) => fetchA.then(fetchB).then(fetchC).then(c => queryClient.fetchQuery(blahblah(c))
fetchD = async (x) => fetchA.then(fetchB).then(fetchC).then(c => queryClient.fetchQuery(blahblah(c))
if I made a query that simply had fetchD as query fn, this would not work because it would not update if A got invalidated. I suppose I could make its query keys a composite of all the dependent queries' keys? is there a better way, or am I correctly understanding this?
3 Replies
metropolitan-bronze
metropolitan-bronzeOP2y ago
I suppose I could make its query keys a composite of all the dependent queries' keys?
though sometimes the intermediate query keys are dependent on intermediate results! (in fact this is like always true for me)
rival-black
rival-black2y ago
If you use fetchD you are not using the cache. So on subsequent requests this is slower than having four useQuery
metropolitan-bronze
metropolitan-bronzeOP2y ago
they both use cache (though i left out the specification of querykeys) fetchD (and implicitly, fetchA, fetchB, fetchC) uses queryClient.fetchQuery

Did you find this page helpful?