sui_gql_client/queries/
genesis_tx.rs

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
use af_sui_types::TransactionData;
use graphql_extract::extract;

use super::Error;
use crate::{scalars, schema, GraphQlClient, GraphQlResponseExt as _};

pub async fn query<C: GraphQlClient>(client: &C) -> Result<TransactionData, Error<C::Error>> {
    let data = client
        .query::<Query, _>(Variables { id: Some(0) })
        .await
        .map_err(Error::Client)?
        .try_into_data()?;
    extract!(data => {
        epoch? {
            transaction_blocks {
                nodes[] {
                    bcs?
                }
            }
        }
    });
    Ok(nodes
        .into_iter()
        .next()
        .ok_or("No transactions in epoch")??
        .into_inner())
}

#[derive(cynic::QueryVariables, Clone, Debug)]
struct Variables {
    id: Option<af_sui_types::Version>,
}

#[derive(cynic::QueryFragment, Clone, Debug)]
#[cynic(graphql_type = "Query", variables = "Variables")]
struct Query {
    #[arguments(id: $id)]
    epoch: Option<Epoch>,
}

#[derive(cynic::QueryFragment, Clone, Debug)]
struct Epoch {
    #[arguments(first: 1)]
    transaction_blocks: TransactionBlockConnection,
}

#[derive(cynic::QueryFragment, Clone, Debug)]
struct TransactionBlockConnection {
    nodes: Vec<TransactionBlock>,
}

#[derive(cynic::QueryFragment, Clone, Debug)]
struct TransactionBlock {
    bcs: Option<scalars::Base64Bcs<TransactionData>>,
}

#[cfg(test)]
#[allow(clippy::unwrap_used)]
#[test]
fn init_gql_output() {
    use cynic::QueryBuilder as _;
    let operation = Query::build(Variables { id: None });
    insta::assert_snapshot!(operation.query, @r###"
    query Query($id: UInt53) {
      epoch(id: $id) {
        transactionBlocks(first: 1) {
          nodes {
            bcs
          }
        }
      }
    }
    "###);
}