github.com/fibonacci-chain/fbc@v0.0.0-20231124064014-c7636198c1e9/libs/cosmos-sdk/docs/building-modules/querier.md (about)

     1  <!--
     2  order: 5
     3  synopsis: "A `Querier` designates a function that processes [`queries`](./messages-and-queries.md#queries). `querier`s 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](../core/baseapp.md#query)."
     4  -->
     5  
     6  # Queriers
     7  
     8  ## Pre-requisite Readings {hide}
     9  
    10  - [Module Manager](./module-manager.md) {prereq}
    11  - [Messages and Queries](./messages-and-queries.md) {prereq}
    12  
    13  ## `Querier` type
    14  
    15  The `querier` type defined in the Cosmos SDK specifies the typical structure of a `querier` function:
    16  
    17  +++ https://github.com/cosmos/cosmos-sdk/blob/7d7821b9af132b0f6131640195326aa02b6751db/types/queryable.go#L6
    18  
    19  Let us break it down:
    20  
    21  - The `path` is an array of `string`s that contains the type of the query, and that can also contain `query` arguments. See [`queries`](./messages-and-queries.md#queries) for more information.
    22  - 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`. 
    23  - The [`Context`](../core/context.md) contains all the necessary information needed to process the `query`, as well as a cache-wrapped copy of the latest state. It is primarily used by the [`keeper`](./keeper.md) to access the state. 
    24  - The result `res` returned to `baseapp`, marhsalled using the application's [`codec`](../core/encoding.md). 
    25  
    26  ## Implementation of a module `querier`s
    27  
    28  Module `querier`s are typically implemented in a `./internal/keeper/querier.go` file inside the module's folder. The [module manager](./module-manager.md) is used to add the module's `querier`s to the [application's `queryRouter`](../core/baseapp.md#query-routing) 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:
    29  
    30  ```go
    31  func NewQuerier(keeper Keeper) sdk.Querier {
    32  	return func(ctx sdk.Context, path []string, req abci.RequestQuery) ([]byte, error) {
    33  		switch path[0] {
    34  		case QueryType1:
    35  			return queryType1(ctx, path[1:], req, keeper)
    36  
    37  		case QueryType2:
    38  			return queryType2(ctx, path[1:], req, keeper)
    39  
    40  		default:
    41  			return nil, sdkerrors.Wrapf(sdkerrors.ErrUnknownRequest, "unknown %s query endpoint: %s", types.ModuleName, path[0])
    42  		}
    43  	}
    44  }
    45  ```
    46  
    47  This simple switch returns a `querier` function specific to the type of the received `query`. At this point of the [query lifecycle](../interfaces/query-lifecycle.md), 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. 
    48  
    49  The `querier` functions themselves are pretty straighforward. They generally fetch a value or values from the state using the [`keeper`](./keeper.md). Then, they marshall the value(s) using the [`codec`](../core/encoding.md) and return the `[]byte` obtained as result. 
    50  
    51  For a deeper look at `querier`s, see this [example implementation of a `querier` function](https://github.com/cosmos/sdk-application-tutorial/blob/c6754a1e313eb1ed973c5c91dcc606f2fd288811/x/nameservice/internal/keeper/querier.go) from the nameservice tutorial. 
    52  
    53  ## Next {hide}
    54  
    55  Learn about [`BeginBlocker` and `EndBlocker`](./beginblock-endblock.md) {hide}