# List posts
To list created posts we will be using
blogd query blog list-post and
blogd query blog get-post command.
get-post subcommand hasn’t been defined yet, so let’s do it now. Query commands (opens new window) from the CLI are handled by
First we define our proto files, in
In our proto file we define the structure of the API endpoint, as well as our request and response structure of the post.
GetQueryCmd is used for creating a list of
query subcommands, it should already be defined. Edit the function to add
CmdShowPost as a subcommand:
Now let’s define
CmdListPost in a new
CmdShowPost run an ABCI (opens new window) query to fetch the data, unmarshals it back form binary to JSON and returns it to the console. ABCI is an interface between your app and Tendermint (a program responsible for replicating the state across machines). ABCI queries look like paths on a hierarchical filesystem. In our case, the query is
custom/blog/list-post. Before we continue, we need to define
QueryListPost that will be used later on to dispatch query requests:
NewQuerier acts as a dispatcher for query functions, it should already be defined. Modify the switch statement to include
Now let’s define
Create a new file
query_post.go in the
Make sure to add the codec to the previous import and add the
getPost function in a new file called
query_post.go in our keeper.
In the keeper we define also the grpc of our queryPost function.
We add the grpc query handler to our module on
Let's make sure to import
context and add the
This function uses a prefix iterator to loop through all the keys with a given prefix (in our case
"post-"). We’re getting values by key with
store.Get and appending them to
postList. Finally, we unmarshal bytes back to JSON and return the result to the console.
Now let’s see how it works. Run the following command to recompile your app, clear the data and relaunch the chain:
After the app has launched, open a different terminal window and create a post:
Now run the query to see the post:
That’s a newly created post along with your address and a unique ID. Try creating more posts and see the output.
We can also make ABCI (opens new window) queries from the browser:
The result of this query is a base64 encoded string inside
This is actually not an error, but may be a bit confusing. If you've added a post and immediately issued
list-post subcommand, you may get a
null. This happens, because it takes several seconds to process the block. After a couple of seconds you should be able to see output of