cosmos-sdk/docs/building-modules/query-services.md

78 lines
4.2 KiB
Markdown
Raw Normal View History

Update Building Modules documentation (#7473) * Update module-manager.md * Update modules #messages doc * Fix typo * Update message implementation example link * Update messages-and-queries.md#queries doc * Update querier.md * Update handler.md * Update links in beginblock-endblock.md * Update keeper.md * Update invariants.md * Update genesis.md * Update module-interfaces.md#transaction-commands * Update module-interfaces.md#query-commands * Update module-interfaces.md#flags * Update module-interfaces.md#rest * Update structure.md * Update module-interfaces.md#grpc * Update errors.md * Update module-interfaces.md#grpc-gateway-rest * Add more info on swagger * Address comments * Fix go.sum * Fix app-anatomy.md * Update docs/building-modules/module-interfaces.md Co-authored-by: Federico Kunze <31522760+fedekunze@users.noreply.github.com> * Update docs/building-modules/querier.md Co-authored-by: Federico Kunze <31522760+fedekunze@users.noreply.github.com> * Update docs/building-modules/querier.md Co-authored-by: Federico Kunze <31522760+fedekunze@users.noreply.github.com> * Update docs/building-modules/module-interfaces.md Co-authored-by: Federico Kunze <31522760+fedekunze@users.noreply.github.com> * Update docs/building-modules/module-interfaces.md Co-authored-by: Federico Kunze <31522760+fedekunze@users.noreply.github.com> * Address part of review comments * Update old ref of RegisterQueryService * Add example code for Manager * Update queriers.md to query-services.md and refs * Add new query-services.md * Revert "Update old ref of RegisterQueryService" This reverts commit 1ea1ea8559604d5c0aa13951b3016967438da2a5. * Update keeper.md * Update handler.md * Update handler.md * Update messages-and-queries.md * Update docs/basics/app-anatomy.md Co-authored-by: Amaury Martiny <amaury.martiny@protonmail.com> * Update docs/building-modules/intro.md Co-authored-by: Amaury Martiny <amaury.martiny@protonmail.com> * Fix typo Co-authored-by: Federico Kunze <31522760+fedekunze@users.noreply.github.com> Co-authored-by: Amaury Martiny <amaury.martiny@protonmail.com>
2020-10-14 05:38:14 -07:00
<!--
order: 5
-->
# Query Services
[docs]: update building modules section to reflect ADR31 (#7702) * Revert "Revert "Update old ref of RegisterQueryService"" This reverts commit 03e4c56de53938ccbf025a441e54b9842f7c4544. * Update intro, module-manager and messages-and-queries * Update messages-and-queries * Update handler * Update structure * Add doc related to RegisterMsgServiceDesc * Update docs/building-modules/handler.md Co-authored-by: Robert Zaremba <robert@zaremba.ch> * Update docs/building-modules/handler.md Co-authored-by: Robert Zaremba <robert@zaremba.ch> * Update docs/building-modules/handler.md Co-authored-by: Robert Zaremba <robert@zaremba.ch> * Update docs/building-modules/messages-and-queries.md * Update handler.md * Rename handler.md to msg-services.md * Update legacy msgs wording * Update messages-and-queries.md * Update docs/building-modules/msg-services.md Co-authored-by: Amaury Martiny <amaury.martiny@protonmail.com> * Update docs/building-modules/intro.md Co-authored-by: Amaury Martiny <amaury.martiny@protonmail.com> * Remove handler mention from intro.md * Update docs/building-modules/messages-and-queries.md Co-authored-by: Amaury Martiny <amaury.martiny@protonmail.com> * Update docs/building-modules/messages-and-queries.md Co-authored-by: Amaury Martiny <amaury.martiny@protonmail.com> * Update docs/building-modules/msg-services.md Co-authored-by: Amaury Martiny <amaury.martiny@protonmail.com> * Update docs/building-modules/keeper.md Co-authored-by: Amaury Martiny <amaury.martiny@protonmail.com> * Update docs/building-modules/msg-services.md Co-authored-by: Amaury Martiny <amaury.martiny@protonmail.com> * Address review comments * Use tag * Update docs/building-modules/intro.md Co-authored-by: Cory <cjlevinson@gmail.com> * Update docs/building-modules/intro.md Co-authored-by: Aaron Craelius <aaron@regen.network> * Update docs/building-modules/messages-and-queries.md Co-authored-by: Aaron Craelius <aaron@regen.network> * Update docs/building-modules/messages-and-queries.md Co-authored-by: Aaron Craelius <aaron@regen.network> * Update docs/building-modules/messages-and-queries.md Co-authored-by: Aaron Craelius <aaron@regen.network> * Update docs/core/transactions.md Co-authored-by: Aaron Craelius <aaron@regen.network> * Update docs/building-modules/messages-and-queries.md Co-authored-by: Aaron Craelius <aaron@regen.network> * Update docs/building-modules/messages-and-queries.md Co-authored-by: Aaron Craelius <aaron@regen.network> * Update docs/building-modules/messages-and-queries.md Co-authored-by: Aaron Craelius <aaron@regen.network> * Update docs/building-modules/messages-and-queries.md Co-authored-by: Aaron Craelius <aaron@regen.network> * Remove framework related explanation from docs * Update docs/building-modules/messages-and-queries.md Co-authored-by: Aaron Craelius <aaron@regen.network> * Update docs/building-modules/module-manager.md Co-authored-by: Aaron Craelius <aaron@regen.network> * Update docs/building-modules/module-manager.md Co-authored-by: Aaron Craelius <aaron@regen.network> * Update docs/building-modules/msg-services.md Co-authored-by: Aaron Craelius <aaron@regen.network> * Update docs/building-modules/msg-services.md Co-authored-by: Aaron Craelius <aaron@regen.network> * Update docs/building-modules/msg-services.md Co-authored-by: Aaron Craelius <aaron@regen.network> * Update docs/building-modules/msg-services.md Co-authored-by: Aaron Craelius <aaron@regen.network> * Update docs/building-modules/msg-services.md Co-authored-by: Aaron Craelius <aaron@regen.network> * Update docs/building-modules/msg-services.md Co-authored-by: Aaron Craelius <aaron@regen.network> * Update docs/building-modules/msg-services.md Co-authored-by: Aaron Craelius <aaron@regen.network> * Update docs/core/baseapp.md Co-authored-by: Aaron Craelius <aaron@regen.network> * Update docs/core/baseapp.md Co-authored-by: Aaron Craelius <aaron@regen.network> * Update beginblock-endblock.md * Update docs/core/baseapp.md Co-authored-by: Aaron Craelius <aaron@regen.network> * Update docs/core/transactions.md Co-authored-by: Aaron Craelius <aaron@regen.network> * Add deprecated notice * Update tx-lifecycle.md Co-authored-by: Robert Zaremba <robert@zaremba.ch> Co-authored-by: Amaury Martiny <amaury.martiny@protonmail.com> Co-authored-by: Aaron Craelius <aaron@regen.network> Co-authored-by: Cory <cjlevinson@gmail.com> Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
2020-11-17 12:41:43 -08:00
A query service processes [`queries`](./messages-and-queries.md#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](../core/baseapp.md#query). {synopsis}
Update Building Modules documentation (#7473) * Update module-manager.md * Update modules #messages doc * Fix typo * Update message implementation example link * Update messages-and-queries.md#queries doc * Update querier.md * Update handler.md * Update links in beginblock-endblock.md * Update keeper.md * Update invariants.md * Update genesis.md * Update module-interfaces.md#transaction-commands * Update module-interfaces.md#query-commands * Update module-interfaces.md#flags * Update module-interfaces.md#rest * Update structure.md * Update module-interfaces.md#grpc * Update errors.md * Update module-interfaces.md#grpc-gateway-rest * Add more info on swagger * Address comments * Fix go.sum * Fix app-anatomy.md * Update docs/building-modules/module-interfaces.md Co-authored-by: Federico Kunze <31522760+fedekunze@users.noreply.github.com> * Update docs/building-modules/querier.md Co-authored-by: Federico Kunze <31522760+fedekunze@users.noreply.github.com> * Update docs/building-modules/querier.md Co-authored-by: Federico Kunze <31522760+fedekunze@users.noreply.github.com> * Update docs/building-modules/module-interfaces.md Co-authored-by: Federico Kunze <31522760+fedekunze@users.noreply.github.com> * Update docs/building-modules/module-interfaces.md Co-authored-by: Federico Kunze <31522760+fedekunze@users.noreply.github.com> * Address part of review comments * Update old ref of RegisterQueryService * Add example code for Manager * Update queriers.md to query-services.md and refs * Add new query-services.md * Revert "Update old ref of RegisterQueryService" This reverts commit 1ea1ea8559604d5c0aa13951b3016967438da2a5. * Update keeper.md * Update handler.md * Update handler.md * Update messages-and-queries.md * Update docs/basics/app-anatomy.md Co-authored-by: Amaury Martiny <amaury.martiny@protonmail.com> * Update docs/building-modules/intro.md Co-authored-by: Amaury Martiny <amaury.martiny@protonmail.com> * Fix typo Co-authored-by: Federico Kunze <31522760+fedekunze@users.noreply.github.com> Co-authored-by: Amaury Martiny <amaury.martiny@protonmail.com>
2020-10-14 05:38:14 -07:00
## Pre-requisite Readings
- [Module Manager](./module-manager.md) {prereq}
- [Messages and Queries](./messages-and-queries.md) {prereq}
## `Querier` type
The `querier` type defined in the Cosmos SDK will be deprecated in favor of [gRPC Services](#grpc-service). It specifies the typical structure of a `querier` function:
+++ https://github.com/cosmos/cosmos-sdk/blob/9a183ffbcc0163c8deb71c7fd5f8089a83e58f05/types/queryable.go#L9
Let us break it down:
- 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.
- 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`](../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.
[docs]: update building modules section to reflect ADR31 (#7702) * Revert "Revert "Update old ref of RegisterQueryService"" This reverts commit 03e4c56de53938ccbf025a441e54b9842f7c4544. * Update intro, module-manager and messages-and-queries * Update messages-and-queries * Update handler * Update structure * Add doc related to RegisterMsgServiceDesc * Update docs/building-modules/handler.md Co-authored-by: Robert Zaremba <robert@zaremba.ch> * Update docs/building-modules/handler.md Co-authored-by: Robert Zaremba <robert@zaremba.ch> * Update docs/building-modules/handler.md Co-authored-by: Robert Zaremba <robert@zaremba.ch> * Update docs/building-modules/messages-and-queries.md * Update handler.md * Rename handler.md to msg-services.md * Update legacy msgs wording * Update messages-and-queries.md * Update docs/building-modules/msg-services.md Co-authored-by: Amaury Martiny <amaury.martiny@protonmail.com> * Update docs/building-modules/intro.md Co-authored-by: Amaury Martiny <amaury.martiny@protonmail.com> * Remove handler mention from intro.md * Update docs/building-modules/messages-and-queries.md Co-authored-by: Amaury Martiny <amaury.martiny@protonmail.com> * Update docs/building-modules/messages-and-queries.md Co-authored-by: Amaury Martiny <amaury.martiny@protonmail.com> * Update docs/building-modules/msg-services.md Co-authored-by: Amaury Martiny <amaury.martiny@protonmail.com> * Update docs/building-modules/keeper.md Co-authored-by: Amaury Martiny <amaury.martiny@protonmail.com> * Update docs/building-modules/msg-services.md Co-authored-by: Amaury Martiny <amaury.martiny@protonmail.com> * Address review comments * Use tag * Update docs/building-modules/intro.md Co-authored-by: Cory <cjlevinson@gmail.com> * Update docs/building-modules/intro.md Co-authored-by: Aaron Craelius <aaron@regen.network> * Update docs/building-modules/messages-and-queries.md Co-authored-by: Aaron Craelius <aaron@regen.network> * Update docs/building-modules/messages-and-queries.md Co-authored-by: Aaron Craelius <aaron@regen.network> * Update docs/building-modules/messages-and-queries.md Co-authored-by: Aaron Craelius <aaron@regen.network> * Update docs/core/transactions.md Co-authored-by: Aaron Craelius <aaron@regen.network> * Update docs/building-modules/messages-and-queries.md Co-authored-by: Aaron Craelius <aaron@regen.network> * Update docs/building-modules/messages-and-queries.md Co-authored-by: Aaron Craelius <aaron@regen.network> * Update docs/building-modules/messages-and-queries.md Co-authored-by: Aaron Craelius <aaron@regen.network> * Update docs/building-modules/messages-and-queries.md Co-authored-by: Aaron Craelius <aaron@regen.network> * Remove framework related explanation from docs * Update docs/building-modules/messages-and-queries.md Co-authored-by: Aaron Craelius <aaron@regen.network> * Update docs/building-modules/module-manager.md Co-authored-by: Aaron Craelius <aaron@regen.network> * Update docs/building-modules/module-manager.md Co-authored-by: Aaron Craelius <aaron@regen.network> * Update docs/building-modules/msg-services.md Co-authored-by: Aaron Craelius <aaron@regen.network> * Update docs/building-modules/msg-services.md Co-authored-by: Aaron Craelius <aaron@regen.network> * Update docs/building-modules/msg-services.md Co-authored-by: Aaron Craelius <aaron@regen.network> * Update docs/building-modules/msg-services.md Co-authored-by: Aaron Craelius <aaron@regen.network> * Update docs/building-modules/msg-services.md Co-authored-by: Aaron Craelius <aaron@regen.network> * Update docs/building-modules/msg-services.md Co-authored-by: Aaron Craelius <aaron@regen.network> * Update docs/building-modules/msg-services.md Co-authored-by: Aaron Craelius <aaron@regen.network> * Update docs/core/baseapp.md Co-authored-by: Aaron Craelius <aaron@regen.network> * Update docs/core/baseapp.md Co-authored-by: Aaron Craelius <aaron@regen.network> * Update beginblock-endblock.md * Update docs/core/baseapp.md Co-authored-by: Aaron Craelius <aaron@regen.network> * Update docs/core/transactions.md Co-authored-by: Aaron Craelius <aaron@regen.network> * Add deprecated notice * Update tx-lifecycle.md Co-authored-by: Robert Zaremba <robert@zaremba.ch> Co-authored-by: Amaury Martiny <amaury.martiny@protonmail.com> Co-authored-by: Aaron Craelius <aaron@regen.network> Co-authored-by: Cory <cjlevinson@gmail.com> Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
2020-11-17 12:41:43 -08:00
- The result `res` returned to `BaseApp`, marshalled using the application's [`codec`](../core/encoding.md).
Update Building Modules documentation (#7473) * Update module-manager.md * Update modules #messages doc * Fix typo * Update message implementation example link * Update messages-and-queries.md#queries doc * Update querier.md * Update handler.md * Update links in beginblock-endblock.md * Update keeper.md * Update invariants.md * Update genesis.md * Update module-interfaces.md#transaction-commands * Update module-interfaces.md#query-commands * Update module-interfaces.md#flags * Update module-interfaces.md#rest * Update structure.md * Update module-interfaces.md#grpc * Update errors.md * Update module-interfaces.md#grpc-gateway-rest * Add more info on swagger * Address comments * Fix go.sum * Fix app-anatomy.md * Update docs/building-modules/module-interfaces.md Co-authored-by: Federico Kunze <31522760+fedekunze@users.noreply.github.com> * Update docs/building-modules/querier.md Co-authored-by: Federico Kunze <31522760+fedekunze@users.noreply.github.com> * Update docs/building-modules/querier.md Co-authored-by: Federico Kunze <31522760+fedekunze@users.noreply.github.com> * Update docs/building-modules/module-interfaces.md Co-authored-by: Federico Kunze <31522760+fedekunze@users.noreply.github.com> * Update docs/building-modules/module-interfaces.md Co-authored-by: Federico Kunze <31522760+fedekunze@users.noreply.github.com> * Address part of review comments * Update old ref of RegisterQueryService * Add example code for Manager * Update queriers.md to query-services.md and refs * Add new query-services.md * Revert "Update old ref of RegisterQueryService" This reverts commit 1ea1ea8559604d5c0aa13951b3016967438da2a5. * Update keeper.md * Update handler.md * Update handler.md * Update messages-and-queries.md * Update docs/basics/app-anatomy.md Co-authored-by: Amaury Martiny <amaury.martiny@protonmail.com> * Update docs/building-modules/intro.md Co-authored-by: Amaury Martiny <amaury.martiny@protonmail.com> * Fix typo Co-authored-by: Federico Kunze <31522760+fedekunze@users.noreply.github.com> Co-authored-by: Amaury Martiny <amaury.martiny@protonmail.com>
2020-10-14 05:38:14 -07:00
## Implementation of a module query service
### gRPC Service
When defining a Protobuf `Query` service, a `QueryServer` interface is generated for each module with all the service methods:
```go
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:
+++ https://github.com/cosmos/cosmos-sdk/blob/d55c1a26657a0af937fa2273b38dcfa1bb3cff9f/x/bank/keeper/grpc_query.go
### Legacy Queriers
Module legacy `querier`s are typically implemented in a `./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:
```go
func NewQuerier(keeper Keeper) sdk.Querier {
return func(ctx sdk.Context, path []string, req abci.RequestQuery) ([]byte, error) {
switch path[0] {
case QueryType1:
return queryType1(ctx, path[1:], req, keeper)
case QueryType2:
return queryType2(ctx, path[1:], req, keeper)
default:
return nil, sdkerrors.Wrapf(sdkerrors.ErrUnknownRequest, "unknown %s query endpoint: %s", types.ModuleName, path[0])
}
}
}
```
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.
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.
For a deeper look at `querier`s, see this [example implementation of a `querier` function](https://github.com/cosmos/cosmos-sdk/blob/7f59723d889b69ca19966167f0b3a7fec7a39e53/x/gov/keeper/querier.go) from the bank module.
## Next {hide}
Learn about [`BeginBlocker` and `EndBlocker`](./beginblock-endblock.md) {hide}