Processing Queries

We don’t only want to modify data, but allow the clients to query the current state. Clients can call /abci_query to tendermint which will make a Query request on the weave application.

Note how it uses a QueryRouter to send queries to different QueryHandlers based on their Path? It just happens that Buckets implement the QueryHandler interface, and now that we understand how RegisterRoutes work, this should be quite simple.

When constructing the application, we register QueryHandlers from every extension we support onto a main QueryRouter that handles all requests. Each extension is responsible for registering it’s Bucket (or Buckets) under appropriate paths. Here we see how the escrow extension registers its bucket to handle all queries for the /escrows path:

// RegisterQuery will register this bucket as "/escrows"
func RegisterQuery(qr weave.QueryRouter) {
    NewBucket().Register("escrows", qr)
}
To summarize :
  • Because we are using buckets, we get queries for free
  • This is true for primary indexes but also for any secondary index registered
  • This is also true for prefix queries
  • We only need to setup our bucket properly and attach it to the QueryRouter

Back to our blog example, let us start by registering our bucket queries :

That’s pretty much it, we can now query blogs, posts and profiles by their primary keys, and posts by author as we have defined this index previously. Here is an example of querying a Blog from our tests :

Similarly for a Post :

In case no results are returned by a query, we’ll get back an empty slice :

Finally, here is an example of a query by secondary index. In this case, we want all the Posts authored by signer :