Chromia
Start Building

11 / 26Part 2: Add a related entityBeginner

Query all reviews for a book

Your app can now attach reviews to books, but it has no way to read them back yet. You'll add a query for that.

Define the query

Add the following Rell code to src/main/queries.rell:

src/main/queries.rell
query get_all_reviews_for_book(isbn: text) {  val reviews = book_review @* { .book.isbn == isbn } (    .reviewer_name,    .review,    .rating  );  return reviews;}

Breakdown of the query

  1. Input parameter: You pass an isbn to choose the book whose reviews are returned.
  2. Join operation: The expression val reviews = book_review @* { .book.isbn == isbn }; selects every book_review entity whose book.isbn equals the isbn you supplied.
  3. Attributes: The following part controls which attributes appear in the result:
    rell
    (  .reviewer_name,  .review,  .rating);

Given a book, the query returns each of its reviews with the reviewer's name, the review text, and the rating.

Test it on a local Chromia node

You test against a local node with the Chromia CLI:

  1. Update or start the local Chromia node:

    • If your node is running, apply the changes with:

      shell
      chr node update
    • If your node is not running, launch it with:

      shell
      chr node start
  2. Execute the query: Open another terminal window or tab and fetch the reviews for a book with:

    shell
    chr query get_all_reviews_for_book "isbn=ISBN1234"

    You should see output like this:

    json
    [  {    "rating": 5,    "review": "It was a great book",    "reviewer_name": "Alice"  }]

Later lessons cover more of Rell, including validation, shaping query results, and running transactions that require signatures.

Written for this site from Chromia’s public course. Code samples are the course’s own.