sui_gql_client/queries/
packages_from_original.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
use af_sui_types::{Address, ObjectId, Version};

use super::fragments::PageInfoForward;
use super::Error;
use crate::{missing_data, schema, GraphQlClient, Paged, PagedResponse};

pub async fn query<C: GraphQlClient>(
    client: &C,
    package_id: ObjectId,
) -> Result<impl Iterator<Item = (ObjectId, u64)>, Error<C::Error>> {
    let vars = Variables {
        address: package_id,
        first: None,
        after: None,
    };

    let response: PagedResponse<Query> = client.query_paged(vars).await.map_err(Error::Client)?;
    let (init, pages) = response
        .try_into_data()?
        .ok_or_else(|| missing_data!("No data"))?;

    Ok(init
        .package_versions
        .nodes
        .into_iter()
        .chain(pages.into_iter().flat_map(|p| p.package_versions.nodes))
        .map(|o| (o.address.into(), o.version)))
}

#[derive(cynic::QueryVariables, Clone, Debug)]
pub struct Variables {
    address: ObjectId,
    after: Option<String>,
    first: Option<i32>,
}

#[derive(cynic::QueryFragment, Debug)]
#[cynic(variables = "Variables")]
pub struct Query {
    #[arguments(address: $address, first: $first, after: $after)]
    pub package_versions: MovePackageConnection,
}

impl Paged for Query {
    type Input = Variables;
    type NextPage = Self;
    type NextInput = Variables;

    fn next_variables(&self, mut prev_vars: Self::Input) -> Option<Self::NextInput> {
        let PageInfoForward {
            has_next_page,
            end_cursor,
        } = &self.package_versions.page_info;
        if *has_next_page {
            prev_vars.after.clone_from(end_cursor);
            Some(prev_vars)
        } else {
            None
        }
    }
}

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

#[derive(cynic::QueryFragment, Debug)]
pub struct MovePackage {
    pub address: Address,
    pub version: Version,
}

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

    let vars = Variables {
        address: Address::new(rand::random()).into(),
        first: None,
        after: None,
    };
    let operation = Query::build(vars);
    insta::assert_snapshot!(operation.query);
}