Query
Querying is a process used for extracting data from a database or accessing state information. Typically, the available query messages can be found in either msg.rs or query.rs, depending on the organization of the code by the contract author.
You can perform queries using an external client (such as an API or through the CLI) or within a contract. For more information on how this works, see Querying contract state.
Queries access the data store of a contract in read-only mode. They can retrieve data and perform additional processing as needed. Consequently, a gas limit is imposed.
Queries consist of an entry in the QueryMsg enum and are managed within the contract's Query function.
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
#[serde(rename_all = "snake_case")]
pub enum QueryMsg {
// ResolveAddress returns the current address that the name resolves to
ResolveRecord { name: String },
Config {},
}
The contract then processes this in the query function:
#[cfg_attr(not(feature = "library"), entry_point)]
pub fn query(deps: Deps, env: Env, msg: QueryMsg) -> StdResult<Binary> {
match msg {
QueryMsg::ResolveRecord { name } => query_resolver(deps, env, name),
QueryMsg::Config {} => to_binary(&config_read(deps.storage).load()?),
}
}
Here, query_resolver is merely another function, and config_read is a helper that encapsulates access to the data store.