fuel_gql_client/client/schema/
message.rs

1use super::{
2    block::Header,
3    Bytes32,
4    HexString,
5    PageDirection,
6    PageInfo,
7    PaginatedResult,
8    PaginationRequest,
9    Signature,
10    TransactionId,
11};
12use crate::client::schema::{
13    schema,
14    Address,
15    MessageId,
16    U64,
17};
18
19#[derive(cynic::QueryFragment, Debug)]
20#[cynic(schema_path = "./assets/schema.sdl")]
21pub struct Message {
22    pub message_id: MessageId,
23    pub amount: U64,
24    pub sender: Address,
25    pub recipient: Address,
26    pub nonce: U64,
27    pub data: HexString,
28    pub da_height: U64,
29    pub fuel_block_spend: Option<U64>,
30}
31
32#[derive(cynic::QueryFragment, Debug)]
33#[cynic(
34    schema_path = "./assets/schema.sdl",
35    graphql_type = "Query",
36    variables = "OwnedMessagesConnectionArgs"
37)]
38pub struct OwnedMessageQuery {
39    #[arguments(owner: $owner, after: $after, before: $before, first: $first, last: $last)]
40    pub messages: MessageConnection,
41}
42
43#[derive(cynic::QueryFragment, Debug)]
44#[cynic(schema_path = "./assets/schema.sdl")]
45pub struct MessageConnection {
46    pub edges: Vec<MessageEdge>,
47    pub page_info: PageInfo,
48}
49
50#[derive(cynic::QueryFragment, Debug)]
51#[cynic(schema_path = "./assets/schema.sdl")]
52pub struct MessageEdge {
53    pub cursor: String,
54    pub node: Message,
55}
56
57#[derive(cynic::QueryVariables, Debug)]
58pub struct OwnedMessagesConnectionArgs {
59    /// Filter messages based on an owner
60    pub owner: Option<Address>,
61    /// Skip until coin id (forward pagination)
62    pub after: Option<String>,
63    /// Skip until coin id (backward pagination)
64    pub before: Option<String>,
65    /// Retrieve the first n coins in order (forward pagination)
66    pub first: Option<i32>,
67    /// Retrieve the last n coins in order (backward pagination).
68    /// Can't be used at the same time as `first`.
69    pub last: Option<i32>,
70}
71
72#[derive(cynic::QueryFragment, Debug)]
73#[cynic(
74    schema_path = "./assets/schema.sdl",
75    graphql_type = "Query",
76    variables = "MessageProofArgs"
77)]
78pub struct MessageProofQuery {
79    #[arguments(transactionId: $transaction_id, messageId: $message_id)]
80    pub message_proof: Option<MessageProof>,
81}
82
83#[derive(cynic::QueryFragment, Debug)]
84#[cynic(schema_path = "./assets/schema.sdl")]
85pub struct MessageProof {
86    /// The proof set of the message proof.
87    pub proof_set: Vec<Bytes32>,
88    /// The index that was used to produce this proof.
89    pub proof_index: U64,
90    /// The signature of the fuel block.
91    pub signature: Signature,
92    /// The fuel block that contains the message.
93    pub header: Header,
94    /// The messages sender address.
95    pub sender: Address,
96    /// The messages recipient address.
97    pub recipient: Address,
98    /// The nonce from the message.
99    pub nonce: Bytes32,
100    /// The amount from the message.
101    pub amount: U64,
102    /// The data from the message.
103    pub data: HexString,
104}
105
106#[derive(cynic::QueryVariables, Debug)]
107pub struct MessageProofArgs {
108    /// Transaction id that contains the output message.
109    pub transaction_id: TransactionId,
110    /// Message id of the output message that requires a proof.
111    pub message_id: MessageId,
112}
113
114impl From<(Option<Address>, PaginationRequest<String>)> for OwnedMessagesConnectionArgs {
115    fn from(r: (Option<Address>, PaginationRequest<String>)) -> Self {
116        match r.1.direction {
117            PageDirection::Forward => OwnedMessagesConnectionArgs {
118                owner: r.0,
119                after: r.1.cursor,
120                before: None,
121                first: Some(r.1.results as i32),
122                last: None,
123            },
124            PageDirection::Backward => OwnedMessagesConnectionArgs {
125                owner: r.0,
126                after: None,
127                before: r.1.cursor,
128                first: None,
129                last: Some(r.1.results as i32),
130            },
131        }
132    }
133}
134
135impl From<MessageConnection> for PaginatedResult<Message, String> {
136    fn from(conn: MessageConnection) -> Self {
137        PaginatedResult {
138            cursor: conn.page_info.end_cursor,
139            has_next_page: conn.page_info.has_next_page,
140            has_previous_page: conn.page_info.has_previous_page,
141            results: conn.edges.into_iter().map(|e| e.node).collect(),
142        }
143    }
144}
145
146#[cfg(test)]
147mod tests {
148    use super::*;
149
150    #[test]
151    fn owned_message_query_gql_output() {
152        use cynic::QueryBuilder;
153
154        let operation = OwnedMessageQuery::build(OwnedMessagesConnectionArgs {
155            owner: Some(Address::default()),
156            after: None,
157            before: None,
158            first: None,
159            last: None,
160        });
161
162        insta::assert_snapshot!(operation.query)
163    }
164}