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:
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 thestatusargument.- 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:
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 matchesid.- 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:
chr node startThe 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:
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:
chr query get_game_by_id id='GAME_ID'Example output:
A call to get_game_by_id returns something like this:
{ "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.