Skip to content

Query data across slices

Goal: a screen needs data that doesn’t live in a single slice — authors with their books, an order with its line items, anything where one thing owns a list of another.

In a database-backed Arc slice, model the relationship the same way you would in the domain: the child carries the parent’s id, and the query filters by it. Arc’s part is making that query a typed, live endpoint and generating the React proxy that calls it.

For an owns-a-list relationship, keep the child keyed to its parent. A book belongs to an author, so the Book read model carries AuthorId:

[ReadModel]
public record Book([property: Key] BookId Id, AuthorId AuthorId, BookTitle Title)
{
public static ISubject<IEnumerable<Book>> BooksForAuthor([Key] AuthorId authorId, IMongoCollection<Book> books) =>
books.Observe(book => book.AuthorId == authorId);
}

The generated proxy takes the same parameter:

const [books] = BooksForAuthor.use(authorId);

Because the query returns Observe(...), it stays live. Add a book for that author and the row updates without polling.

For a list-with-details screen, let the author list call AllAuthors.use() and each row call BooksForAuthor.use(author.id). That keeps each slice small: the author slice owns authors, the book slice owns books, and the screen composes their generated proxies.