account.rs
Overview
The account.rs file defines GraphQL types and query resolvers related to user accounts within the application. It mainly handles querying account information and paginated account-related events (outgoing external messages). The file integrates with a SQLite database through the sqlx crate and uses async-graphql's connection-based pagination utilities for efficient event retrieval. It leverages external crates such as tvm_client for client context and tvm_types for base64 encoding.
Structs and Their Responsibilities
AccountEventEdge
Implements
EdgeNameTypefor GraphQL cursor pagination edge naming.Defines the GraphQL type name "AccountEventEdge" for edges in the connection of account events.
AccountEventsConnection
Implements
ConnectionNameTypefor GraphQL cursor pagination connection naming.Defines the GraphQL type name "AccountEventsConnection" for the connection wrapping account events.
Account
A GraphQL object representing an account.
Fields
id: String
The unique identifier (address) of the account.boc: Option<String>
An optional base64-encoded Bag of Cells (BOC) representing the account struct.dapp_id: Option<String>
An optional decentralized application identifier related to the account.
Traits and Conversions
Implements
SimpleObjectfromasync_graphqlwith snake_case field renaming for GraphQL schema.Implements Fromdb::Account to convert database account models into GraphQL
Accountobjects.
The BOC field is base64-encoded using tvm_types::base64_encode if present.
Usage Example
let db_account: db::Account = // obtain from DB
let gql_account: Account = db_account.into();
AccountQuery
Represents a GraphQL query root for fetching account data and related events.
Fields
address: String
The account address to query.preloaded: Option<db::Account>
Optionally holds a preloaded database account object to optimize repeated queries.
Methods
info
pub async fn info(&self, ctx: &Context<'_>) -> Option<Account>
Retrieves the account information for the specified address.
If a
preloadedaccount is present, it returns that directly.Otherwise, it queries the database for the account by address asynchronously.
Converts the database account result into the GraphQL
Accountobject.
Parameters
ctx: &Context<'_>: Provides access to shared data such as the database connection (SqlitePool) and client context (ClientContext).
Returns
Option<Account>: The account data if found, otherwiseNone.
Usage Example
let query = AccountQuery { address: "0x123...".to_string(), preloaded: None };
let account_info = query.info(&context).await;
events
pub async fn events(
&self,
ctx: &Context<'_>,
first: Option<i32>,
after: Option<String>,
last: Option<i32>,
before: Option<String>,
) -> Option<Connection<String, Event, EmptyFields, EmptyFields, AccountEventsConnection, AccountEventEdge>>
Fetches a paginated list of account-related events based on cursor pagination arguments.
Supports cursor-based pagination using
first,after,last, andbeforeparameters.Uses
db::Message::account_eventsto query outgoing external messages associated with the account address.Wraps the results in an
async_graphql::connection::Connectionobject, with edges containingEventobjects.Logs pagination state for debugging purposes.
Parameters
ctx: &Context<'_>: Provides access to shared data such as the SQLite connection pool.first: Option<i32>: Number of items to return starting from the cursor afterafter. Mutually exclusive withlast.after: Option<String>: Cursor to paginate after.last: Option<i32>: Number of items to return counting backwards starting from the cursor beforebefore. Mutually exclusive withfirst.before: Option<String>: Cursor to paginate before.
Returns
Option<Connection<String, Event, EmptyFields, EmptyFields, AccountEventsConnection, AccountEventEdge>>: A connection object containing edges for events;Noneif the query fails.
Implementation Details
Uses
async_graphql::connection::queryhelper to handle pagination logic.Converts database messages into GraphQL
Eventobjects.Cursors are derived from
msg_chain_orderfield of events.Uses
EmptyFieldsas placeholder for additional edge or connection fields, indicating no extra metadata.
Usage Example
let events_connection = query.events(&ctx, Some(10), None, None, None).await;
Important Implementation Details and Algorithms
Cursor-based Pagination:
Theeventsmethod relies on cursor-based pagination using theasync_graphql::connectionmodule. The pagination logic is encapsulated in thePaginationArgsstruct (imported from elsewhere), which determines the slicing and cursor boundaries for the event list.Database Abstraction:
The file depends on database module functionsdb::Account::by_addressanddb::Message::account_eventsto retrieve account and event data. These are asynchronous and return Rust futures, supporting efficient I/O-bound operations.Event Conversion:
Messages fetched from the database are converted into GraphQLEventobjects for API consumption, consistent with the GraphQL schema design.GraphQL Integration:
Usesasync_graphql's derive macros and traits extensively to define GraphQL types, queries, and connections, following best practices for schema design and pagination.
System Interaction
Database Layer:
Interacts with SQLite through a connection pool (SqlitePool) to fetch account and message data.GraphQL Schema:
Exposes GraphQL types and query resolvers for clients to request account information and paginated events.Client Context:
Usestvm_client::ClientContextfrom an Arc-wrapped shared reference, presumably for blockchain or VM context related to accounts.Event Module:
Converts messages toEventobjects, which are part of the GraphQL event schema (crate::schema::graphql_std::events).Pagination Utilities:
Usesasync_graphql::connectionmodule for standardizing cursor-based pagination mechanics.
Mermaid Class Diagram
classDiagram
class Account {
+id: String
-boc: Option<String>
-dapp_id: Option<String>
}
class AccountQuery {
+address: String
-preloaded: Option<db::Account>
+info()
+events()
}
class AccountEventEdge {
<<EdgeNameType>>
+type_name()
}
class AccountEventsConnection {
<<ConnectionNameType>>
+type_name()
}
AccountQuery ..> Account : returns
AccountQuery ..> AccountEventsConnection : uses
AccountQuery ..> AccountEventEdge : uses