# Queries that read other queries (https://effetch-query.monarcode.dev/docs/queries/dependent)

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](https://effetch-query.monarcode.dev/docs/getting-started).

## Read another query in run

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

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

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

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

export const session = api.query(
  Fetch.endpoint("GET /session", {
    output: Schema.Struct({ userId: Schema.Finite }),
  }),
);

export const myTodos = Query.make({
  key: ["todos", "mine"],
  run: (ctx) =>
    Effect.gen(function* () {
      // Waits for the session, and runs again when its value changes.
      const { userId } = yield* ctx.result(session());
      return yield* ApiClient.use((client) =>
        client.get("/todos", {
          query: { owner: userId },
          output: Schema.Array(Todo),
        }),
      );
    }),
});
```

`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](https://effetch-query.monarcode.dev/docs/queries/freshness).

## 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](https://effetch-query.monarcode.dev/docs/queries/freshness#queries-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](https://effetch-query.monarcode.dev/docs/cache/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](https://effetch-query.monarcode.dev/docs/queries/placeholders).

To derive a value in one component without a query of its own, pass
`useQuery` a `select`, as
[Select part of a value](https://effetch-query.monarcode.dev/docs/react#select-part-of-a-value) shows.

## What's next

- [Freshness and refetching](https://effetch-query.monarcode.dev/docs/queries/freshness): when the query you
  read refetches, and so when yours runs again.
- [Sign out and switch accounts](https://effetch-query.monarcode.dev/docs/cache/accounts): clear the session
  and every query that read it.
- [Select part of a value](https://effetch-query.monarcode.dev/docs/react#select-part-of-a-value): derive a
  value in a component instead.