Skip to main content

Write basic queries

This section focuses on querying game data from the Chromia blockchain. We’ll cover how to retrieve games by their status and get specific game details by ID.

Queries

Get game by status

To retrieve games with a specific status (e.g., created, active, or completed), use:

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

Explanation:

  • get_games_by_status(status) is used to fetch all games that match the specified status parameter.
  • This query is valuable for filtering games based on their current state (e.g., to find games ready to join or currently in play).

Example usage:

To list all games with the status created, pass game_status.created to get_games_by_status.

Get game by ID

To retrieve details of a specific game using its unique ID:

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

Explanation:

  • get_game_by_id(id) returns the details of the game matching the provided unique id.
  • This query is useful for retrieving all data related to a particular game, including player details, moves, and the current state.

Example usage:

To retrieve game details for a game with a specific ID, pass the game’s unique integer ID to get_game_by_id.

Manually test the dapp

You can manually test your queries by running a local test node and interacting with it using the Chromia CLI.

Starting a test node

To start a local test node, run the following command from your project folder:

chr node start

You'll see logs as the node progresses to build blocks.

You can now test your queries manually using Chromia CLI.

Test get games by status query manually

To manually test the get_games_by_status query:

chr query get_games_by_status status='game_status.active'

Test get game by ID query manually

To manually test the get_game_by_id query:

chr query get_game_by_id id='GAME_ID'

Example output:

For get_game_by_id, you might see output similar to:

{
"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
}

This output includes key details like the players involved, the game’s creation time, moves, and the winner, if available.