Skip to content

Is it possible to map the result of one query to another query? #228

Open
@RyanPayso13

Description

@RyanPayso13

I have an initial usePaginatedQuery which gives me an array of ids:

  const {
    resolvedData: policies,
    isLoading,
    isError,
  } = usePaginatedQuery(
    [`policies`],
    () => {
      return axios.get(`/v1/policies?page=${page}`);
    },
    {
      enabled: isEnabled,
      retry: false,
    }
  );

I need to iterate over the id in the resolved data array to subsequently fetch data associated with that id:

usePaginatedQuery(
    [`policy.${id}.documents`],
    () => {
      return axios.get(
        `/v1/policies/${id}/documents?page=${page}`
      );
    },
    {
      enabled: isEnabled,
      retry: false,
    }
  );

I then need an array of documents that are associated with the appropriate id to build my react-table e.g.

[{id: '123', documents: ['foo.txt', 'bar.txt']}, {id: '456', documents: ['xxx.txt', 'yyy.txt']}]

Is there a way to do this in react-query?

I looked at documentation for dependant queries but it is not quite what i need.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions