Skip to content

useQueries hook: Support for dynamically executing multiple queries at runtimeΒ #463

@ItaiYosephi

Description

@ItaiYosephi

We're implementing a feature in our application that requires executing multiple GraphQL queries that are determined at runtime. Currently, Apollo Client doesn't provide a clean way to handle this use case.

We'd like to propose adding a useQueries hook that would allow for elegant handling of multiple dynamic queries, similar to:

const visibleSteps = STEPS.filter(isStepVisible);
const results = useQueries(
  visibleSteps.map(step => ({ 
    query: step.query, 
    variables: step.variables 
  }))
);

While there is a similar feature request (#178) the solution proposed there only addresses running the same query multiple times with different variables.

Our use case requires running completely different queries that are determined at runtime. TanStack Query (formerly React Query) already offers this functionality through their useQueries hook.

It's worth noting that while we could potentially use Apollo Client directly for this purpose, there's currently no way to utilize the cache-and-network fetch policy in such an implementation, which is a critical requirement for our application's data fetching strategy.

Adding this capability to Apollo Client would significantly improve developer experience for complex applications that need to fetch data from multiple dynamic sources simultaneously while maintaining proper caching behavior.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions