A Protobuf Query service processes queries. Query services are specific to the module in which they are defined, and only process queries defined within said module. They are called from BaseApp's Query method.
The querier type defined in the Cosmos SDK will be deprecated in favor of gRPC Services. It specifies the typical structure of a querier function:
Copy
package types
import(
abci "github.com/tendermint/tendermint/abci/types")// Querier defines a function type that a module querier must implement to handle// custom client queries.type Querier =func(ctx Context, path []string, req abci.RequestQuery)([]byte,error)
Let us break it down:
The path is an array of strings that contains the type of the query, and that can also contain query arguments. See queries for more information.
The req itself is primarily used to retrieve arguments if they are too large to fit in the path. This is done using the Data field of req.
The Context contains all the necessary information needed to process the query, as well as a branch of the latest state. It is primarily used by the keeper to access the state.
The result res returned to BaseApp, marshalled using the application's codec.
When defining a Protobuf Query service, a QueryServer interface is generated for each module with all the service methods:
Copy
type QueryServer interface{QueryBalance(context.Context,*QueryBalanceParams)(*types.Coin,error)QueryAllBalances(context.Context,*QueryAllBalancesParams)(*QueryAllBalancesResponse,error)}
These custom queries methods should be implemented by a module's keeper, typically in ./keeper/grpc_query.go. The first parameter of these methods is a generic context.Context, whereas querier methods generally need an instance of sdk.Context to read
from the store. Therefore, the SDK provides a function sdk.UnwrapSDKContext to retrieve the sdk.Context from the provided
context.Context.
Here's an example implementation for the bank module:
Module legacy queriers are typically implemented in a ./keeper/querier.go file inside the module's folder. The module manager is used to add the module's queriers to the application's queryRouter via the NewQuerier() method. Typically, the manager's NewQuerier() method simply calls a NewQuerier() method defined in keeper/querier.go, which looks like the following:
This simple switch returns a querier function specific to the type of the received query. At this point of the query lifecycle, the first element of the path (path[0]) contains the type of the query. The following elements are either empty or contain arguments needed to process the query.
The querier functions themselves are pretty straighforward. They generally fetch a value or values from the state using the keeper. Then, they marshall the value(s) using the codec and return the []byte obtained as result.