# Query every book · Your first Rell app on Chromia · Learn Chromia

> Queries let you read data from the blockchain. This lesson walks you through writing a query and then testing it.

Source: https://chromia.com/learn/book-review/book-entity/write-queries/

Your first Rell app on ChromiaLesson 7 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/)

07 / 26Part 1: Define your first entityBeginner

# Query every book

Queries let you read data from the blockchain. This lesson walks you through writing a query and then testing it.

## Define the query

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

src/main/queries.rell

```
query get_all_books() {
  return book @* { } (
    .isbn,
    .title,
    .author
  );
}
```

### Breakdown of the query

* **`query` keyword:** Declares a new query. It plays a role comparable to a `SELECT` statement in SQL and returns data from the blockchain.
* **`@*` operand:** Writing `book @*` tells Rell the query can return any number of `book` objects, including none.
* **Filter criteria:** You place query conditions inside the curly braces `{}`. They are empty here, so every entry is returned.
* **Attributes:** The part after the curly braces lists the attributes to return: `.isbn`, `.title`, and `.author`.

The result is a collection of every book, where each item carries its `isbn`, `title`, and `author`.

## Update the unit tests

Next, extend the tests so they verify the query's behavior.

### Step 1: Update imports

In `src/test/book_review_test.rell`, change the import statement to the following:

src/test/book\_review\_test.rell

```nolines
import main.{ book, create_book, get_all_books };
```

### Step 2: Add the new test function

Add this test function to the same file:

src/test/book\_review\_test.rell

```
function test_get_books() {
    rell.test.tx()
        .op(create_book("123", "Book1", "Author1"))
        .op(create_book("124", "Book2", "Author2"))
        .run();

    val books = get_all_books();
    assert_equals(books.size(), 2);
}
```

### Breakdown of the test

* **Transaction creation:** `rell.test.tx()` builds a transaction for testing.
* **Adding operations:** The transaction receives two `create_book` operations.
* **Execution:** Calling `run()` executes the transaction.
* **Query and assertions:** `get_all_books()` fetches the books, and the test asserts that exactly 2 were returned.

From your project folder, run the tests with this terminal command:

shell

```nolines
chr test
```

## Test on a local Chromia node

To check the query against a local Chromia node:

1. **Update or start the local Chromia node**:

   * **If your node is running**, update it with:

     shell

     ```nolines
     chr node update
     ```
   * **If your node is not running**, start it with:

     shell

     ```nolines
     chr node start
     ```

   note

   The effect of `chr node update` is not always immediate. The query might only become available once the next block has been processed.
2. **Run the query**: Open another terminal window or tab and run:

   shell

   ```nolines
   chr query get_all_books
   ```

   The output lists the books stored so far, such as any you added earlier with the `create_book` operation. If you completed the previous steps, the result should resemble this:

   json

   ```
   [
     {
       "author": "George Orwell",
       "isbn": "ISBN1234",
       "title": "1984"
     }
   ]
   ```

   This output shows that the query returns the stored book from the blockchain as expected.

You have now stored entities on your test node's blockchain and written a query that returns a collection of all books.

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

[← Previous**Add your first operation**](https://chromia.com/learn/book-review/book-entity/basic-operations/)[Next →**Part 2: Add a related entity**](https://chromia.com/learn/book-review/book-review-entity/)

On this page

* [Define the query](#define-the-query)
* [Update the unit tests](#update-the-unit-tests)
* [Test on a local Chromia node](#test-on-a-local-chromia-node)
