sui_gql_client/queries/
packages_published_epoch.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
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
158
159
160
161
162
163
164
165
166
use af_sui_types::{ObjectId, Version};
use futures::TryStreamExt as _;

use super::fragments::{ObjectFilterV2, PageInfo, PageInfoForward};
use super::{stream, Error};
use crate::{schema, GraphQlClient, GraphQlResponseExt as _};

type Item = (ObjectId, u64, u64);

pub async fn query<C: GraphQlClient>(
    client: &C,
    package_ids: Vec<ObjectId>,
) -> Result<impl Iterator<Item = Item>, Error<C::Error>> {
    let vars = QueryVariables {
        filter: Some(ObjectFilterV2 {
            type_: None,
            owner: None,
            object_ids: Some(&package_ids),
        }),
        first: None,
        after: None,
    };

    let results: Vec<_> = stream::forward(client, vars, request).try_collect().await?;

    Ok(results.into_iter())
}

async fn request<C: GraphQlClient>(
    client: &C,
    vars: QueryVariables<'_>,
) -> super::Result<stream::Page<impl Iterator<Item = super::Result<Item, C>>>, C> {
    let data = client
        .query::<Query, _>(vars)
        .await
        .map_err(Error::Client)?
        .try_into_data()?;
    graphql_extract::extract!(data => {
        objects {
            page_info
            nodes[] {
                address
                as_move_package? {
                    previous_transaction_block? {
                        effects? {
                            epoch? {
                                epoch_id
                            }
                            checkpoint? {
                                sequence_number
                            }
                        }
                    }
                }
            }
        }
    });
    Ok(stream::Page::new(
        page_info,
        nodes.map(|r| -> super::Result<_, C> {
            let (address, (epoch_id, ckpt_seq)) = r?;
            Ok((address, epoch_id, ckpt_seq))
        }),
    ))
}

#[cfg(test)]
#[allow(clippy::unwrap_used)]
#[test]
fn gql_output() {
    use cynic::QueryBuilder as _;

    let vars = QueryVariables {
        filter: None,
        first: None,
        after: None,
    };
    let operation = Query::build(vars);
    insta::assert_snapshot!(operation.query, @r###"
    query Query($after: String, $filter: ObjectFilter, $first: Int) {
      objects(filter: $filter, first: $first, after: $after) {
        nodes {
          address
          asMovePackage {
            previousTransactionBlock {
              effects {
                epoch {
                  epochId
                }
                checkpoint {
                  sequenceNumber
                }
              }
            }
          }
        }
        pageInfo {
          hasNextPage
          endCursor
        }
      }
    }
    "###);
}

// ================================================================================

impl stream::UpdatePageInfo for QueryVariables<'_> {
    fn update_page_info(&mut self, info: &PageInfo) {
        self.after.clone_from(&info.end_cursor)
    }
}

// ================================================================================

#[derive(cynic::QueryVariables, Clone, Debug)]
struct QueryVariables<'a> {
    after: Option<String>,
    filter: Option<ObjectFilterV2<'a>>,
    first: Option<i32>,
}

#[derive(cynic::QueryFragment, Debug)]
#[cynic(variables = "QueryVariables")]
struct Query {
    #[arguments(filter: $filter, first: $first, after: $after)]
    objects: ObjectConnection,
}

#[derive(cynic::QueryFragment, Debug)]
struct ObjectConnection {
    nodes: Vec<Object>,
    page_info: PageInfoForward,
}

#[derive(cynic::QueryFragment, Debug)]
struct Object {
    address: ObjectId,
    as_move_package: Option<MovePackage>,
}

#[derive(cynic::QueryFragment, Debug)]
struct MovePackage {
    previous_transaction_block: Option<TransactionBlock>,
}

#[derive(cynic::QueryFragment, Debug)]
struct TransactionBlock {
    effects: Option<TransactionBlockEffects>,
}

#[derive(cynic::QueryFragment, Debug)]
struct TransactionBlockEffects {
    epoch: Option<Epoch>,
    checkpoint: Option<Checkpoint>,
}

#[derive(cynic::QueryFragment, Debug)]
struct Epoch {
    epoch_id: Version,
}

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