Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

API for Dependent Queries #4829

Open
EskiMojo14 opened this issue Jan 23, 2025 · 1 comment
Open

API for Dependent Queries #4829

EskiMojo14 opened this issue Jan 23, 2025 · 1 comment
Labels
enhancement New feature or request rtk-query

Comments

@EskiMojo14
Copy link
Collaborator

EskiMojo14 commented Jan 23, 2025

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}`);
}
@EskiMojo14 EskiMojo14 added enhancement New feature or request rtk-query labels Jan 23, 2025
@markerikson
Copy link
Collaborator

Thoughts:

  • dependentQuery would be auto-dispatched because this is happening from within our thunk in the first place, so we've got dispatch available
  • we'd have to nest subscription behavior, ie "this subscription was triggered by a dependent query - when the dep query cache entry goes away, remove that subscription", and "if the dependent query changes, rerun this query too just in case"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request rtk-query
Projects
None yet
Development

No branches or pull requests

2 participants