24 / 26Part 6: Create the clientBeginner
Query the blockchain using postchain-client
This lesson shows you how to query the Chromia blockchain for information about transactions and about the current state of your dapp's table.
postchain-client keeps queries simple. The example below fetches every book and walks through how the call works.
Start by declaring a Book type to describe each book's data:
type Book = { isbn: string; title: string; author: string;};Then add a function that fetches all books and prints them:
const getAllBooks = async () => { const bookList = await client.query<Book[]>("get_all_books", {}); console.log("Book list:", bookList);};The function runs the query through the client and its query command. You pass the expected return type as a generic, here Book[].
Function arguments
First argument: queryFunctionName
The name of the query function on the blockchain. It matches a function in your Rell script that returns every book record.
Second argument: queryArguments
Passing an empty object ({}) means the query runs without arguments. When a blockchain function needs input to filter or process its results, you put those parameters in this object.
Handling the response
client.query returns a promise. Once it resolves, you get an array of books (Book[]) that you can display, process, or use however your application requires. This example logs them to the console.
Call getAllBooks(); from your main function as shown below, then run the client again to see the book you added. To restart and test the example from a clean state, run chr node start --wipe, which starts a new node with no existing data.
async function main() { client = await createClient({ nodeUrlPool: "http://localhost:7740", blockchainRid: blockchainRID, }); console.log("Creating a new book transaction"); await client.signAndSendUniqueTransaction( { name: "create_book", args: ["ISBN1", "Chromia 101", "John Doe"] }, bookKeeperSignatureProvider ); getAllBooks();}