# Return structured results from queries · Your first Rell app on Chromia · Learn Chromia

> Queries often need to return data assembled from more than one entity, such as a book together with its reviews. You can shape that result with a struct, so…

Source: https://chromia.com/learn/book-review/input-verification/structure/

Your first Rell app on ChromiaLesson 14 of 26

1. [01Your first Rell app on Chromia](https://chromia.com/learn/book-review/introduction/)
2. [02Set up your project](https://chromia.com/learn/book-review/setup/)
3. [03Rell project structure](https://chromia.com/learn/book-review/rell-structure/)

Part 1: Define your first entity

1. [04Overview](https://chromia.com/learn/book-review/book-entity/)
2. [05Create your first entity](https://chromia.com/learn/book-review/book-entity/tables/)
3. [06Add your first operation](https://chromia.com/learn/book-review/book-entity/basic-operations/)
4. [07Query every book](https://chromia.com/learn/book-review/book-entity/write-queries/)

Part 2: Add a related entity

1. [08Overview](https://chromia.com/learn/book-review/book-review-entity/)
2. [09Define the book review entity](https://chromia.com/learn/book-review/book-review-entity/tables/)
3. [10Add an operation for creating book reviews](https://chromia.com/learn/book-review/book-review-entity/basic-operations/)
4. [11Query all reviews for a book](https://chromia.com/learn/book-review/book-review-entity/write-queries/)

Part 3: Checking and validating inputs

1. [12Overview](https://chromia.com/learn/book-review/input-verification/)
2. [13Verify inputs](https://chromia.com/learn/book-review/input-verification/input-verification/)
3. [14Return structured results from queries](https://chromia.com/learn/book-review/input-verification/structure/)

Part 4: Signing transactions and filtering queries

1. [15Overview](https://chromia.com/learn/book-review/sign-transaction/)
2. [16Sign a transaction](https://chromia.com/learn/book-review/sign-transaction/sign-transaction/)
3. [17Filter and sort query results](https://chromia.com/learn/book-review/sign-transaction/structure/)

Part 5: Blockchain state and transactions explained

1. [18Overview](https://chromia.com/learn/book-review/blockchain-transactions/)
2. [19How blockchain state and transactions work](https://chromia.com/learn/book-review/blockchain-transactions/query-transaction/)
3. [20Walking through an example](https://chromia.com/learn/book-review/blockchain-transactions/example/)

Part 6: Create the client

1. [21Overview](https://chromia.com/learn/book-review/build-client/)
2. [22Prerequisites](https://chromia.com/learn/book-review/build-client/prerequisites/)
3. [23Establishing a connection to Chromia](https://chromia.com/learn/book-review/build-client/sign-transaction/)
4. [24Query the blockchain using postchain-client](https://chromia.com/learn/book-review/build-client/query-blockchain/)
5. [25Complete the example](https://chromia.com/learn/book-review/build-client/complete-example/)

1. [26What’s next?](https://chromia.com/learn/book-review/what-next/)

[Learn](https://chromia.com/learn/)[Your first Rell app on Chromia](https://chromia.com/learn/book-review/)

14 / 26Part 3: Checking and validating inputsBeginner

# Return structured results from queries

Queries often need to return data assembled from more than one entity, such as a book together with its reviews. You can shape that result with a struct, so the client receives a single, well-defined object instead of loose values.

You will define a `book_review_dto` struct that holds both the book details and the review details, and then change the query so it returns that struct.

### Define the struct

Data structures like this belong in `src/main/entities.rell`. The new `book_review_dto` struct carries the book information alongside the review fields.

Add the following code to `src/main/entities.rell`:

src/main/entities.rell

```
struct book_review_dto {
  book: struct<book>;
  reviewer_name: text;
  review: text;
  rating: integer;
}
```

A field of type `struct<book>` holds every field of the referenced `book` entity. Using it in an expression causes an implicit join that loads those fields.

### Update the query

Now change `get_all_reviews_for_book` so it returns `book_review_dto` values. Each result then contains the book and the review together.

Replace the query in `src/main/queries.rell` with this version:

src/main/queries.rell

```
query get_all_reviews_for_book(isbn: text) {
  require(book @? { .isbn == isbn }, "Book with isbn %s not found".format(isbn));
  val reviews = book_review @* { .book.isbn == isbn } (
    book_review_dto(
      book = .book.to_struct(),
      .reviewer_name,
      .review,
      .rating
    )
  );
  return reviews;
}
```

### Explanation

1. **Struct definition:** `book_review_dto` groups the book and review details into one value, so related data travels as a unit.
2. **Updated query:**

   * `require` verifies that a book with the given ISBN exists.
   * `book_review @* { .book.isbn == isbn }` selects every review for that book.
   * The struct's `book` attribute is filled with `book_review.book.to_struct()`, which turns the entity reference into a struct value. This is what causes the implicit join that loads all fields of the referenced `book` entity.

### Testing the update

Verify the change with the following steps:

1. **Update the node:**

   With a Chromia node already running, push the latest version of your dapp to it:

   shell

   ```nolines
   chr node update
   ```

   Otherwise, start the node with:

   shell

   ```nolines
   chr node start
   ```

   note

   Updating a running node does not take effect instantly. The new code may only become active once the next block is produced.
2. **Run the query:**

   Call the query from the Chromia CLI:

   shell

   ```nolines
   chr query get_all_reviews_for_book "isbn=ISBN1234"
   ```

   You should see output similar to this:

   json

   ```
   [
     {
       "book": {
         "author": "George Orwell",
         "isbn": "ISBN1234",
         "title": "1984"
       },
       "rating": 5,
       "review": "It was a great book",
       "reviewer_name": "Alice"
     }
   ]
   ```

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

[← Previous**Verify inputs**](https://chromia.com/learn/book-review/input-verification/input-verification/)[Next →**Part 4: Signing transactions and filtering queries**](https://chromia.com/learn/book-review/sign-transaction/)
