# `query.other_domain`

A query in one domain reads the records of an entity that belongs to another domain.

Area 6, [Reads and writes](/docs/reads-and-writes.md); not declinable; the next step is usually the human's.

## Why Jadpo refuses it

A domain owns its entities, and other domains see its data only through
what it makes public. A query reads its own domain's entities. Here
`shelf`, in `lending`, reads `Book`, which belongs to `catalogue`. If
that were allowed, `catalogue` could no longer change how it stores books
without breaking `lending`, and it would have no say in what it shares.

Jadpo won't choose for you between moving the query and asking
`catalogue` for the data, because that decides which domain owns the
shelf and what `catalogue` offers to others.

## What it looks like

`Book` is a public entity in the `catalogue` domain, and `lending` uses
it:




domains/lending/queries/shelf.jadpo:

```jadpo
use catalogue/entities { Book }

/// Every copy.
query shelf() -> List<Book> {
    from: Book
    order: [Book.id asc]
    limit: 20
}
```

```text
error: `shelf` reads `Book`, an entity of domain `catalogue`
 --> domains/lending/queries/shelf.jadpo:5:11
  |
5 |     from: Book
  |           ^^^^ domain `catalogue`
  reason: A query reads its own domain's entities only; another domain's data comes from that domain's public queries, so `catalogue` decides what it shares.
  next: Call a public query of `catalogue`, or move `shelf` into `catalogue` (human)
  rule: query.other_domain
  help: https://jadpo.dev/docs/errors/query.other_domain

check failed: 1 error, 4 files
```

`(human)` at the end of the `next:` line tells your coding agent to stop
and ask you which fix you want, because the choice is yours to make: it
decides which domain owns the shelf, and what `catalogue` shares with
other domains.

## How to fix it

The question is whether the shelf belongs to `catalogue` or to `lending`.

**If it belongs to `catalogue`**, move the file to
`domains/catalogue/queries/shelf.jadpo`. Its contents stay the same:

```jadpo
use catalogue/entities { Book }

/// Every copy.
query shelf() -> List<Book> {
    from: Book
    order: [Book.id asc]
    limit: 20
}
```




`shelf` reads `Book` from inside its own domain. Only `catalogue`'s own
code can call it.

**If `lending` needs the list**, put the query in `catalogue`, mark it
`public`, and call it from `lending` through the domain's name. In
`domains/catalogue/queries/shelf.jadpo`:

```jadpo
public query shelf() -> List<Book> {
    ...
}
```


And in `domains/lending/actions/browse_shelf.jadpo`:

```jadpo
use catalogue/entities { Book }
use catalogue

/// The copies a borrower can choose from.
action browse_shelf() -> List<Book> {
    return catalogue.shelf()
}
```





`catalogue` now offers `shelf` to other domains, and `lending` gets the
books by calling `catalogue.shelf()`. `catalogue` can change how it
stores books as long as `shelf` gives the same result.

## Related

- [Reads across domains](/docs/reads-across-domains.md)
- [What can be public](/docs/ownership-and-calls-replace-modules.md#what-can-be-public)
- [Calls name their owner](/docs/ownership-and-calls-replace-modules.md#calls-name-their-owner)
