To list created posts we will be using blogd query blog list-post and blogd query blog get-post command. list-post and 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 query.go.
Copy
syntax ="proto3";package example.blog.blog;import"google/api/annotations.proto";import"cosmos/base/query/v1beta1/pagination.proto";// this line is used by starport scaffolding # 1import"blog/post.proto";
option go_package ="github.com/example/blog/x/blog/types";// Query defines the gRPC querier service.
service Query {// this line is used by starport scaffolding # 2
rpc Post(QueryGetPostRequest) returns (QueryGetPostResponse){
option (google.api.http).get ="/example/blog/blog/post/{id}";}
rpc PostAll(QueryAllPostRequest) returns (QueryAllPostResponse){
option (google.api.http).get ="/example/blog/blog/post";}}// this line is used by starport scaffolding # 3
message QueryGetPostRequest {string id =1;}
message QueryGetPostResponse {
Post Post =1;}
message QueryAllPostRequest {
cosmos.base.query.v1beta1.PageRequest pagination =1;}
message QueryAllPostResponse {
repeated Post Post =1;
cosmos.base.query.v1beta1.PageResponse pagination =2;}
In our proto file we define the structure of the API endpoint, as well as our request and response structure of the post.
Function GetQueryCmd is used for creating a list of query subcommands, it should already be defined. Edit the function to add CmdListPost and CmdShowPost as a subcommand:
CmdListPost and 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.
This function uses a prefix iterator to loop through all the keys with a given prefix (in our case PostPrefix is "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:
Copy
starport serve
After the app has launched, open a different terminal window and create a post:
Copy
blogd tx blog create-post 'Hello!' 'This is my first blog post.' --from=user1
Now run the query to see the post:
Copy
blogd query blog list-post
Copy
[{"creator":"cosmos1mc6leyjdwd9ygxeqdnvtsh7ks3knptjf3s5lf9","title":"Hello!","body":"This is my first blog post.","id":"30808a80-799d-475c-9f5d-b382ea24d79c"}]
That’s a newly created post along with your address and a unique ID. Try creating more posts and see the output.
The result of this query is a base64 encoded string inside result.response.value. You can decode it using a browser’s built in JavaScript console: atob("WwogIHsKICAgICJjcmV...").
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 list-post subcommand.