Open
Description
An issue that comes up regularly is building queries that want to pull data from other queries: #4828 #1171 to name a few
These queries would need to:
- be provided the data from the dependent queries
- rerun if any of those dependent queries refetch
- act as a subscription for those queries (so their cache sticks around), unsubscribing when its own cache entry is removed
API still TBC, but some ideas raised in maintainers' chat:
build.query({
query(arg, dependencies) {
return `foo/${dependencies.first.id}`
},
dependencies(arg){
return {
first: { endpoint: "ep1", args: arg.foo }
}
}
})
build.withDependencies(
(arg)=> {
return {
first: { endpoint: "ep1", args: arg.foo }
}
}
).query({
query(arg, dependencies) {
return `foo/${dependencies.first.id}`
}
})
build.withDependencies(
(arg)=> {
return {
first: api.endpoints.ep1.createDep(arg.foo)
}
}
).query({
query(arg, dependencies) {
return `foo/${dependencies.first.id}`
}
})
async queryFn(postId, { dependentQuery }) {
const comments = await dependentQuery("getComments", postId);
const commentMetadata = await Promise.all(
comments.map((comment) => dependentQuery("getCommentMetadata", comment.id)),
);
return { data: commentMetadata };
}
async queryFn(arg, api, extraOptions, fetchWithBq) {
const first = await api.dependentQuery("ep1", arg.foo);
if (first.error) return first;
return fetchWithBq(`foo/${first.data.id}`);
}