Chromia
Start Building

07 / 20Module 1: A Rell backend with FT4 accounts

Write basic queries

Here you query game data stored on the Chromia blockchain: you fetch games by their status and look up an individual game by its ID.

Queries

Get games by status

Use this query to fetch games in a particular state, such as created, active, or completed:

src/main/queries.rell
query get_games_by_status(status: enums.game_status) {    return functions.get_games_by_status(status);}

How it works:

  • get_games_by_status(status) returns every game whose state equals the status argument.
  • Filter games with it according to where they stand, for example to list games waiting for a second player or games still being played.

Example:

Call get_games_by_status with game_status.created to get every game that has the created status.

Get a game by ID

Use this query to look up one game by its unique ID:

src/main/queries.rell
query get_game_by_id(id: integer) {    return functions.get_game_by_id(id);}

How it works:

  • get_game_by_id(id) returns the game whose unique identifier matches id.
  • It gives you the full record for that game, covering the players, the moves made, and its current state.

Example:

Call get_game_by_id with the game's unique integer ID to get its details.

Manually test the dapp

To try the queries by hand, start a local test node and send requests to it through the Chromia CLI.

Starting a test node

From your project folder, start a local test node with:

bash
chr node start

The node prints logs as it builds blocks.

With the node running, you can call your queries from the Chromia CLI.

Test the get games by status query manually

Run the following to call get_games_by_status:

bash
chr query get_games_by_status status='active'

Test the get game by ID query manually

Run the following to call get_game_by_id:

bash
chr query get_game_by_id id='GAME_ID'

Example output:

A call to get_game_by_id returns something like this:

json
{  "id": 5,  "created_by": "bob_account_id",  "created_at": "TIMESTAMP",  "player1_account_id": "bob_account_id",  "player2_account_id": "eve_account_id",  "status": "game_status.active",  "moves": [    { "player": "bob", "position": 4 },    { "player": "eve", "position": 5 }  ],  "winner": null}

The result shows the participating players, when the game was created, the moves played, and the winner once one exists.

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