fuel_gql_client/client/schema/
balance.rs

1use crate::client::{
2    schema::{
3        schema,
4        Address,
5        AssetId,
6        PageInfo,
7        U64,
8    },
9    PageDirection,
10    PaginatedResult,
11    PaginationRequest,
12};
13
14#[derive(cynic::QueryVariables, Debug)]
15pub struct BalanceArgs {
16    pub owner: Address,
17    pub asset_id: AssetId,
18}
19
20#[derive(cynic::QueryFragment, Debug)]
21#[cynic(
22    schema_path = "./assets/schema.sdl",
23    graphql_type = "Query",
24    variables = "BalanceArgs"
25)]
26pub struct BalanceQuery {
27    #[arguments(owner: $owner, assetId: $asset_id)]
28    pub balance: Balance,
29}
30
31#[derive(cynic::InputObject, Clone, Debug)]
32#[cynic(schema_path = "./assets/schema.sdl")]
33pub struct BalanceFilterInput {
34    /// Filter coins based on the `owner` field
35    pub owner: Address,
36}
37
38#[derive(cynic::QueryVariables, Debug)]
39pub struct BalancesConnectionArgs {
40    /// Filter coins based on a filter
41    filter: BalanceFilterInput,
42    /// Skip until coin id (forward pagination)
43    pub after: Option<String>,
44    /// Skip until coin id (backward pagination)
45    pub before: Option<String>,
46    /// Retrieve the first n coins in order (forward pagination)
47    pub first: Option<i32>,
48    /// Retrieve the last n coins in order (backward pagination).
49    /// Can't be used at the same time as `first`.
50    pub last: Option<i32>,
51}
52
53impl From<(Address, PaginationRequest<String>)> for BalancesConnectionArgs {
54    fn from(r: (Address, PaginationRequest<String>)) -> Self {
55        match r.1.direction {
56            PageDirection::Forward => BalancesConnectionArgs {
57                filter: BalanceFilterInput { owner: r.0 },
58                after: r.1.cursor,
59                before: None,
60                first: Some(r.1.results as i32),
61                last: None,
62            },
63            PageDirection::Backward => BalancesConnectionArgs {
64                filter: BalanceFilterInput { owner: r.0 },
65                after: None,
66                before: r.1.cursor,
67                first: None,
68                last: Some(r.1.results as i32),
69            },
70        }
71    }
72}
73
74#[derive(cynic::QueryFragment, Debug)]
75#[cynic(
76    schema_path = "./assets/schema.sdl",
77    graphql_type = "Query",
78    variables = "BalancesConnectionArgs"
79)]
80pub struct BalancesQuery {
81    #[arguments(filter: $filter, after: $after, before: $before, first: $first, last: $last)]
82    pub balances: BalanceConnection,
83}
84
85#[derive(cynic::QueryFragment, Debug)]
86#[cynic(schema_path = "./assets/schema.sdl")]
87pub struct BalanceConnection {
88    pub edges: Vec<BalanceEdge>,
89    pub page_info: PageInfo,
90}
91
92impl From<BalanceConnection> for PaginatedResult<Balance, String> {
93    fn from(conn: BalanceConnection) -> Self {
94        PaginatedResult {
95            has_next_page: conn.page_info.has_next_page,
96            has_previous_page: conn.page_info.has_previous_page,
97            cursor: conn.page_info.end_cursor,
98            results: conn.edges.into_iter().map(|e| e.node).collect(),
99        }
100    }
101}
102
103#[derive(cynic::QueryFragment, Debug)]
104#[cynic(schema_path = "./assets/schema.sdl")]
105pub struct BalanceEdge {
106    pub cursor: String,
107    pub node: Balance,
108}
109
110#[derive(cynic::QueryFragment, Debug)]
111#[cynic(schema_path = "./assets/schema.sdl")]
112pub struct Balance {
113    pub owner: Address,
114    pub amount: U64,
115    pub asset_id: AssetId,
116}
117
118#[cfg(test)]
119mod tests {
120    use super::*;
121
122    #[test]
123    fn balance_query_gql_output() {
124        use cynic::QueryBuilder;
125        let operation = BalanceQuery::build(BalanceArgs {
126            owner: Address::default(),
127            asset_id: AssetId::default(),
128        });
129        insta::assert_snapshot!(operation.query)
130    }
131
132    #[test]
133    fn balances_connection_query_gql_output() {
134        use cynic::QueryBuilder;
135        let operation = BalancesQuery::build(BalancesConnectionArgs {
136            filter: BalanceFilterInput {
137                owner: Address::default(),
138            },
139            after: None,
140            before: None,
141            first: None,
142            last: None,
143        });
144        insta::assert_snapshot!(operation.query)
145    }
146}