resolver.rs

Overview

This file defines the MessageLoader struct and its implementation as a data loader for asynchronously fetching Message entities from a SQLite database. The loader facilitates batching and caching of database queries to efficiently resolve GraphQL requests involving messages by their IDs. It leverages the async-graphql crate's Loader trait and uses SQLx for database interaction.

Components

MessageLoader Struct

pub struct MessageLoader {
    pub pool: SqlitePool,
}

impl Loader<String> for MessageLoader

This implementation makes MessageLoader conform to the Loader trait from async_graphql::dataloader. This enables it to be used as a data loader in GraphQL resolvers, providing efficient batching and caching mechanisms.

Associated Types

Method: load

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

Example Usage

let loader = MessageLoader { pool: sqlite_pool.clone() };
let message_ids = vec!["msg1".to_string(), "msg2".to_string()];
let messages_map = loader.load(&message_ids).await?;
if let Some(message) = messages_map.get("msg1") {
    // Use the message instance
}

Important Implementation Notes

Interaction with Other Components

Mermaid Diagram: Class Structure of MessageLoader

classDiagram
class MessageLoader {
+pool: SqlitePool
+load(keys: &[String]) async -> Result<HashMap<String, Message>, Error>
}
MessageLoader ..|> Loader

This diagram shows the MessageLoader struct containing a pool field and implementing the asynchronous load method as part of the Loader trait specialization for String keys and Message values.