1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
use crate::client::schema::{schema, HexString256, HexStringUtxoId, PageInfo, U64};
use crate::client::{PageDirection, PaginatedResult, PaginationRequest};
#[derive(cynic::FragmentArguments, Debug)]
pub struct CoinByIdArgs {
pub utxo_id: HexStringUtxoId,
}
#[derive(cynic::QueryFragment, Debug)]
#[cynic(
schema_path = "./assets/schema.sdl",
graphql_type = "Query",
argument_struct = "CoinByIdArgs"
)]
pub struct CoinByIdQuery {
#[arguments(utxo_id = &args.utxo_id)]
pub coin: Option<Coin>,
}
#[derive(cynic::InputObject, Clone, Debug)]
#[cynic(schema_path = "./assets/schema.sdl")]
pub struct CoinFilterInput {
pub owner: HexString256,
pub color: Option<HexString256>,
}
#[derive(cynic::FragmentArguments, Debug)]
pub struct CoinsConnectionArgs {
filter: CoinFilterInput,
pub after: Option<String>,
pub before: Option<String>,
pub first: Option<i32>,
pub last: Option<i32>,
}
impl From<(HexString256, HexString256, PaginationRequest<String>)> for CoinsConnectionArgs {
fn from(r: (HexString256, HexString256, PaginationRequest<String>)) -> Self {
match r.2.direction {
PageDirection::Forward => CoinsConnectionArgs {
filter: CoinFilterInput {
owner: r.0,
color: Some(r.1),
},
after: r.2.cursor,
before: None,
first: Some(r.2.results as i32),
last: None,
},
PageDirection::Backward => CoinsConnectionArgs {
filter: CoinFilterInput {
owner: r.0,
color: Some(r.1),
},
after: None,
before: r.2.cursor,
first: None,
last: Some(r.2.results as i32),
},
}
}
}
#[derive(cynic::QueryFragment, Debug)]
#[cynic(
schema_path = "./assets/schema.sdl",
graphql_type = "Query",
argument_struct = "CoinsConnectionArgs"
)]
pub struct CoinsQuery {
#[arguments(filter = &args.filter, after = &args.after, before = &args.before, first = &args.first, last = &args.last)]
pub coins: CoinConnection,
}
#[derive(cynic::QueryFragment, Debug)]
#[cynic(schema_path = "./assets/schema.sdl")]
pub struct CoinConnection {
pub edges: Option<Vec<Option<CoinEdge>>>,
pub page_info: PageInfo,
}
impl From<CoinConnection> for PaginatedResult<Coin, String> {
fn from(conn: CoinConnection) -> Self {
PaginatedResult {
cursor: conn.page_info.end_cursor,
results: conn
.edges
.unwrap_or_default()
.into_iter()
.filter_map(|e| e.map(|e| e.node))
.collect(),
}
}
}
#[derive(cynic::QueryFragment, Debug)]
#[cynic(schema_path = "./assets/schema.sdl")]
pub struct CoinEdge {
pub cursor: String,
pub node: Coin,
}
#[derive(cynic::QueryFragment, Debug)]
#[cynic(schema_path = "./assets/schema.sdl")]
pub struct Coin {
pub amount: U64,
pub block_created: U64,
pub color: HexString256,
pub utxo_id: HexStringUtxoId,
pub maturity: U64,
pub owner: HexString256,
pub status: CoinStatus,
}
#[derive(cynic::Enum, Clone, Copy, Debug, PartialEq)]
#[cynic(schema_path = "./assets/schema.sdl")]
pub enum CoinStatus {
Unspent,
Spent,
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn coin_by_id_query_gql_output() {
use cynic::QueryBuilder;
let operation = CoinByIdQuery::build(CoinByIdArgs {
utxo_id: HexStringUtxoId::default(),
});
insta::assert_snapshot!(operation.query)
}
#[test]
fn coins_connection_query_gql_output() {
use cynic::QueryBuilder;
let operation = CoinsQuery::build(CoinsConnectionArgs {
filter: CoinFilterInput {
owner: HexString256::default(),
color: HexString256::default().into(),
},
after: None,
before: None,
first: None,
last: None,
});
insta::assert_snapshot!(operation.query)
}
}