mod.rs

Overview

This file defines the Event data structure, which represents a message event within the system. The Event struct is designed to be used with GraphQL APIs, enabling the exposure of message event data in a standardized and accessible format. The file also implements a conversion from the underlying database message representation (db::Message) into the Event struct, facilitating the transformation of raw persistence data into API-ready objects.


Detailed Description

Struct: Event

The Event struct encapsulates the essential information related to a message event. It is annotated with #[derive(SimpleObject, Clone, Debug)] from the async_graphql crate, making it compatible for GraphQL query responses.

Fields

Field

Type

Description

msg_id

String

Public identifier of the message event.

body

Option<String>

Optional message body, base64 encoded if present.

created_at

Option<u64>

Optional creation timestamp in Unix time, automatically set by the block containing the transaction generating this event.

dst

Option<String>

Optional destination address string.

msg_chain_order

Option<String>

Optional field indicating the order of the message in a chain.

GraphQL Behavior

Usage Example

// Assuming `db_msg` is an instance of `db::Message` retrieved from the database
let event: Event = db_msg.into(); // Converts db::Message to Event

// Accessing fields
println!("Message ID: {}", event.msg_id);
if let Some(body) = &event.body {
    println!("Message Body (base64): {}", body);
}
if let Some(timestamp) = event.created_at {
    println!("Created At (Unix time): {}", timestamp);
}

Implementation Details

Conversion: From<db::Message> for Event

The From trait is implemented to convert a db::Message instance (presumably the database representation of a message) into an Event struct. This conversion involves:

This ensures that the data conforms to the API's expected format and that optional fields are handled gracefully.


Interaction with Other Components

This file acts as a bridge between the persistence layer and the GraphQL API layer, translating internal database objects into API-exposed types.


Mermaid Diagram: Structure of mod.rs

classDiagram
class Event {
+msg_id: String
-body: Option<String>
-created_at: Option<u64>
-dst: Option<String>
+msg_chain_order: Option<String>
}
Event ..> db::Message : from()