Write a query to retrieve all reviews of a book
Now that we have a mechanism to add reviews connected to books, we need a way to retrieve them. We'll define a query for that.
Define the query
Open the file src/main/queries.rell
and add the following Rell code:
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
- Input Parameter: The query takes an
isbn
parameter to specify which book's reviews you want to retrieve. - Join Operation:
val reviews = book_review @* { .book.isbn == isbn };
retrieves allbook_review
entities where thebook.isbn
matches the givenisbn
. - Attributes: We specify which attributes to include in the result by adding:
(
.reviewer_name,
.review,
.rating
);
This query retrieves all reviews for a specified book, including the reviewer's name, the review text, and the rating.
Test it on a local Chromia node
Testing on a local Chromia node is done using the Chromia CLI. Here are the steps:
-
Update or start the local Chromia node:
-
If the node is already running, use the following command to update it:
chr node update
-
If the node is not already running, start it with:
chr node start
noteAfter running
chr node update
, the query might take a moment to become available. You may need to wait for the following block to be processed before executing the query. If you get a "400 Bad Request" error, please wait a few moments and try again.
-
-
Execute the query: In a new terminal window or tab, run the following command to fetch all reviews for a book:
chr query get_all_reviews_for_book "isbn=ISBN1234"
The expected result should be:
[
{
"rating": 5,
"review": "It was a great book",
"reviewer_name": "Alice"
}
]
Summary
In this lesson, we:
- Defined a
book_review
entity in Chromia's Rell. - Implemented a query to retrieve all reviews for a specific book.
In upcoming lessons, we'll explore Rell's powerful capabilities further, including handling validation, structuring results from queries and executing transactions with signatures.