cala_server/graphql/
loader.rs

1use async_graphql::dataloader::Loader;
2
3use std::{collections::HashMap, sync::Arc};
4
5use super::{
6    account::Account,
7    account_set::AccountSet,
8    journal::Journal,
9    transaction::Transaction,
10    tx_template::TxTemplate,
11    velocity::{VelocityControl, VelocityLimit},
12};
13use cala_ledger::{
14    account::{error::AccountError, *},
15    account_set::{error::AccountSetError, *},
16    balance::{error::BalanceError, *},
17    journal::{error::JournalError, *},
18    primitives::*,
19    transaction::error::TransactionError,
20    tx_template::error::TxTemplateError,
21    velocity::error::VelocityError,
22    CalaLedger,
23};
24
25pub struct LedgerDataLoader {
26    pub ledger: CalaLedger,
27}
28
29impl Loader<BalanceId> for LedgerDataLoader {
30    type Value = AccountBalance;
31    type Error = Arc<BalanceError>;
32
33    async fn load(
34        &self,
35        keys: &[BalanceId],
36    ) -> Result<HashMap<BalanceId, AccountBalance>, Self::Error> {
37        self.ledger
38            .balances()
39            .find_all(keys)
40            .await
41            .map_err(Arc::new)
42    }
43}
44
45impl Loader<AccountId> for LedgerDataLoader {
46    type Value = Account;
47    type Error = Arc<AccountError>;
48
49    async fn load(&self, keys: &[AccountId]) -> Result<HashMap<AccountId, Account>, Self::Error> {
50        self.ledger
51            .accounts()
52            .find_all(keys)
53            .await
54            .map_err(Arc::new)
55    }
56}
57
58impl Loader<AccountSetId> for LedgerDataLoader {
59    type Value = AccountSet;
60    type Error = Arc<AccountSetError>;
61
62    async fn load(
63        &self,
64        keys: &[AccountSetId],
65    ) -> Result<HashMap<AccountSetId, AccountSet>, Self::Error> {
66        self.ledger
67            .account_sets()
68            .find_all(keys)
69            .await
70            .map_err(Arc::new)
71    }
72}
73
74impl Loader<JournalId> for LedgerDataLoader {
75    type Value = Journal;
76    type Error = Arc<JournalError>;
77
78    async fn load(&self, keys: &[JournalId]) -> Result<HashMap<JournalId, Journal>, Self::Error> {
79        self.ledger
80            .journals()
81            .find_all(keys)
82            .await
83            .map_err(Arc::new)
84    }
85}
86
87impl Loader<TransactionId> for LedgerDataLoader {
88    type Value = Transaction;
89    type Error = Arc<TransactionError>;
90
91    async fn load(
92        &self,
93        keys: &[TransactionId],
94    ) -> Result<HashMap<TransactionId, Transaction>, Self::Error> {
95        self.ledger
96            .transactions()
97            .find_all(keys)
98            .await
99            .map_err(Arc::new)
100    }
101}
102
103impl Loader<TxTemplateId> for LedgerDataLoader {
104    type Value = TxTemplate;
105    type Error = Arc<TxTemplateError>;
106
107    async fn load(
108        &self,
109        keys: &[TxTemplateId],
110    ) -> Result<HashMap<TxTemplateId, TxTemplate>, Self::Error> {
111        self.ledger
112            .tx_templates()
113            .find_all(keys)
114            .await
115            .map_err(Arc::new)
116    }
117}
118
119impl Loader<VelocityLimitId> for LedgerDataLoader {
120    type Value = VelocityLimit;
121    type Error = Arc<VelocityError>;
122
123    async fn load(
124        &self,
125        keys: &[VelocityLimitId],
126    ) -> Result<HashMap<VelocityLimitId, VelocityLimit>, Self::Error> {
127        self.ledger
128            .velocities()
129            .find_all_limits(keys)
130            .await
131            .map_err(Arc::new)
132    }
133}
134
135impl Loader<VelocityControlId> for LedgerDataLoader {
136    type Value = VelocityControl;
137    type Error = Arc<VelocityError>;
138
139    async fn load(
140        &self,
141        keys: &[VelocityControlId],
142    ) -> Result<HashMap<VelocityControlId, VelocityControl>, Self::Error> {
143        self.ledger
144            .velocities()
145            .find_all_controls(keys)
146            .await
147            .map_err(Arc::new)
148    }
149}