resolver.rs

Overview

This file defines a data loader for efficiently fetching blockchain block data from a SQLite database. It implements the Loader trait from the async_graphql::dataloader module to batch-load blocks by their IDs asynchronously. This approach optimizes data retrieval by reducing the number of database queries required when resolving GraphQL requests that need multiple blocks.

The primary functionality revolves around the BlockLoader struct, which holds a database connection pool and provides an asynchronous method to load blocks corresponding to a collection of block IDs. The blocks fetched are mapped by their IDs and returned in a hash map, enabling quick lookups for subsequent GraphQL resolvers.

Structs and Implementations

BlockLoader

pub struct BlockLoader {
    pub pool: SqlitePool,
}
let pool = SqlitePool::connect("sqlite://mydb.sqlite").await?;
let block_loader = BlockLoader { pool };

Loader<String> Implementation for BlockLoader

The BlockLoader implements the Loader trait for the key type String, where each key is a block ID represented as a String.

impl Loader<String> for BlockLoader {
    type Error = Error;
    type Value = super::Block;

    async fn load(
        &self,
        keys: &[String],
    ) -> anyhow::Result<HashMap<String, Self::Value>, Self::Error> {
        // Implementation here
    }
}

load

async fn load(
    &self,
    keys: &[String],
) -> anyhow::Result<HashMap<String, Self::Value>, Self::Error>
let block_ids = vec!["block1".to_string(), "block2".to_string()];
let loaded_blocks = block_loader.load(&block_ids).await?;
if let Some(block) = loaded_blocks.get("block1") {
    println!("Block found: {:?}", block);
}

Important Implementation Details and Algorithms

Interaction with Other System Components

Diagram: Structure and Workflow of BlockLoader

flowchart TD
A[BlockLoader] -->|has| B[SqlitePool]
A -->|implements| C[Loader<String>]
C -->|method| D(load)
D --> E[Receives block IDs]
E --> F[Constructs SQL query with IN clause]
F --> G[Executes async query via SqlitePool]
G --> H[Maps db::Block rows to super::Block]
H --> I[Collects results into HashMap<String, Block>]
I --> J[Returns Map of block ID -> Block]

This flowchart illustrates how the BlockLoader uses its SqlitePool to execute an asynchronous query that loads blocks by their IDs, converts database rows to domain objects, and returns the results in a hash map keyed by block ID.