Skip to content

Query related data with Cratis Arc

To show one author’s books in Cratis Arc, store the author’s identifier on each book and filter a live query by that identifier. The screen can combine the author list with each author’s catalog without making the author feature call the book feature’s handler.

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.

Use the application setup from the standalone MongoDB book lesson, including its domain declarations, imports, and replica-set configuration for change streams. You need stored authors and books to check the filter, plus generated query proxies and a configured Arc React client to show the results. Neither the relationship nor the live query requires Cratis Chronicle.

Keep the book’s own Id and add AuthorId to record which author it belongs to. The query receives that author identifier and uses it as its filter:

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

View C# snippet source on GitHub

The read model’s identity is its Id property by convention; AuthorId is the relationship, not the book’s key. The caller supplies authorId. Arc supplies the storage dependency, so the caller does not choose a collection or repository.

C# distinguishes data and service parameters by type. The JVM example marks MongoObservableQuery with @FromServices and puts the query on the companion object as a @JvmStatic function. TypeScript declares the argument and service with argument(...) and service(...); its application-owned BookRepository must implement observeForAuthor.

After generating the query proxy, pass the author identifier to its React hook:

// authorId is the generated Guid value, not String(author.id).
const [books] = BooksForAuthor.use({ authorId });

The query returns a stream of matching books. With MongoDB change streams configured and the observation source connected, changes publish updated query data without polling. The TypeScript repository is responsible for providing that observation source; returning an ordinary list would not make it live.

The filter does not enforce parent existence. If every book must refer to an existing author, add server validation and database constraints where supported.

Query an author who has books and confirm that another author’s books are absent. Keep the query subscribed, then add a book for the selected author: the next result should include it. Adding a book for a different author should not add it to this catalog.

If the initial list is correct but does not update, check the MongoDB change-stream setup and the repository’s observation source. If the wrong books appear, check the stored AuthorId and filter before debugging React.

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