Dependant and parallel queries #4333
Unanswered
pmarxbraun
asked this question in
Q&A
Replies: 2 comments 5 replies
-
just pass in an empty array to useQueries if it's not ready yet:
|
Beta Was this translation helpful? Give feedback.
2 replies
-
I have a similar use-case and can't figure out the TanStack/query idiomatic way. Basically, I fetch a resource that returns a set of IDs. In order for the data to have any meaning, I need to associate those IDs with actual resources. I want to maintain the cache so don't want to fetch any of this data outside of TanStack/query. So I started with @TkDodo's suggestion of just using an empty array for const useQueryFamily = (parentId: number) => {
const parent = useQuery({
queryKey: ['parent', parentId],
queryFn: fetchParent // returns an object containing { childrenIds: [1, 2, 3] }
});
// I can't return `parent` from this custom hook because it isn't complete yet
const childrenIds = parent.data?.childrenIds ?? [];
const children = useQueries({
queries: childrenIds.map(childId => {
return {
queryKey: ['children', childId],
queryFn: fetchChild,
// enabled: this isn't really necessary because it's only going to run once there are childrenIds because otherwise childrenIds is an empty array
}
})
});
// Transform the data: add the Child *object* to the Parent
let childrenCollection = {};
if (children.length > 0 && children.every(query => query.isSuccess) {
childrenCollection = children.reduce((collection, query) => {
collection[query.data.id] = query.data;
return collection;
}, {});
}
const transformed = {
...parent.data,
children: childrenCollection
}
// Not sure how to return from this custom query or *what* to return
// The isLoading property is now "wrong" because it is done but the children hasn't been populated yet
// It's not quite "done" in that sense
// I could do something like:
// isLoading: parent.isLoading || children.some(query => query.isLoading)
//
// but would I have to do that for all the different properties returned from query?
return { ...parent, data: transformed }
} |
Beta Was this translation helpful? Give feedback.
3 replies
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.
-
Hi,
I'd like to call
useQueries
dependent on a previous oneuseQuery
to finish before it can execute.I'm calling the 1 st one as such :
It returns the response with following shape
My goal is to then take
response.categories
and make parallel queries usinguseQueries
.Currently if fails as
categoriesArr
is called beforepostCats
is received, and if I conditionally calluseQueries
it breaks the rules of Hooks.fetchPost
andfetchPostCategories
are basic fetch calls.Is there a way to wait for
fetchPost
to respond before calling paralleluseQueries
?Right now i'm using this approach after
fetchPost
:fetchPostCategories
being afor...loop
iterating overpostCats
.It works but I don't think I'm using it the right way and profit from the library benefits.
Thanks for your help 🙏
Beta Was this translation helpful? Give feedback.
All reactions