Skip to Content
Effetch Query
Queries

Queries that read other queries

Build a query from the value of another query.

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

The examples import api.ts and todos.ts from Getting started.

  • it waits while the other query has no value yet;
  • it fails as the other query does, with its typed error;
  • when the other query's value changes, this one refetches.

That makes dependent queries, where one request needs what another returned. Here the todos load once 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 refetches when it changes.
      const {  } = yield* .(());
      return yield* .(() =>
        .("/todos", {
          : { :  },
          : .(),
        }),
      );
    }),
});

A few 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 nothing uses doesn't count.
  • Clearing one clears its readers. Query.clear on session() clears myTodos too, since its value came from it. When myTodos is asked for again, it asks for the session (Sign out and switch accounts).
  • It waits for real values. A placeholder isn't a value: a query reading one through ctx.result waits for the fetch (Show a placeholder).

Endpoint queries have no run to write, so a query that reads one is a Query.make or Query.family of your own, as here. To derive a value in one component without a query of its own, use select (Select part of a value).