Skip to Content
Effetch Query
Queries

Queries that read other queries

Build a query from the value of another query.

Some requests need what another one returned: the todos of whoever is signed in need the session first. A query's run can read another query, and waits for it. This page shows how, and what the dependency changes.

The example imports api.ts and todos.ts from Getting started.

Read another query in run

Here the todos load after the session says whose they are:

import * as  from "effect/Effect";
import * as  from "effect/Schema";

import {  } from "@monarcode/effetch";
import {  } from "@monarcode/effetch-query";

import { ,  } from "./api.js";
import {  } from "./todos.js";

export const  = .(
  .("GET /session", {
    : .({ : . }),
  }),
);

export const  = .({
  : ["todos", "mine"],
  : () =>
    .(function* () {
      // Waits for the session, and runs again when its value changes.
      const {  } = yield* .(());
      return yield* .(() =>
        .("/todos", {
          : { :  },
          : .(),
        }),
      );
    }),
});

Query.make can take run as a function of a Query.RunContext, as here, and Query.family passes one as the second argument of its run. ctx.result(query) returns the other query's value, and makes this query depend on it:

  • it waits while the other query has no value yet;
  • it fails as the other query does, with its typed error;
  • it re-runs when the other query's value or failure changes.

Endpoint queries have no run to write, so a query that reads one is a Query.make or Query.family of your own, as myTodos is.

When it runs again

A dependent query re-runs when the other query's value or failure changes: when a refetch of it settles, when it's written, and when it's reset. So each refetch of session() runs myTodos once, after the refetch settles, and sends GET /todos?owner=… once, even when the session didn't change.

While the other query refetches, the dependent query keeps its value, and ctx.result returns the value the other query still shows. So when one event refetches both, such as an invalidation that reaches both keys, the dependent query runs with that value, then again when the other query's refetch settles. If that refetch fails, the dependent query fails too, with the same error.

To send fewer requests, give the query you read a longer staleTime, so fewer events refetch it. See Freshness and refetching.

What the dependency changes

A few more things follow from the dependency:

  • It keeps the other query in use. While something uses myTodos, session() counts as in use too, so focus and invalidation refetch it as they would a query on screen. A dependent query that's only cached doesn't count.
  • Clearing one clears its readers. Query.clear on session() clears myTodos too, since its value came from it. When myTodos is read again, it reads the session again, as Sign out and switch accounts shows.
  • It waits for real values. A placeholder isn't a value: a query reading one through ctx.result waits for the fetch. See Show a placeholder.

To derive a value in one component without a query of its own, pass useQuery a select, as Select part of a value shows.

What's next

On this page