sui_gql_client/queries/
epoch_final_checkpoint_num.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
75
76
use af_sui_types::Version;
use cynic::QueryFragment;
use graphql_extract::extract;

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

pub async fn query<C: GraphQlClient>(client: &C, epoch_id: u64) -> Result<u64, Error<C::Error>> {
    let data = client
        .query::<Query, _>(Variables { id: Some(epoch_id) })
        .await
        .map_err(Error::Client)?
        .try_into_data()?;
    Ok(extract(data)?)
}

fn extract(data: Option<Query>) -> Result<Version, &'static str> {
    extract!(data => {
        epoch? {
            checkpoints {
                nodes[] {
                    sequence_number
                }
            }
        }
    });
    let mut nodes = nodes;
    nodes.next().ok_or("Empty epoch checkpoints")?
}

#[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) {
        checkpoints(last: 1) {
          nodes {
            sequenceNumber
          }
        }
      }
    }
    "###);
}

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

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

#[derive(QueryFragment, Clone, Debug)]
struct Epoch {
    #[arguments(last: 1)]
    checkpoints: CheckpointConnection,
}

#[derive(QueryFragment, Clone, Debug)]
struct CheckpointConnection {
    nodes: Vec<Checkpoint>,
}

#[derive(QueryFragment, Clone, Debug)]
struct Checkpoint {
    sequence_number: Version,
}