sui_gql_client/queries/
objects_content.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
use std::collections::HashMap;

use af_sui_types::ObjectId;
use sui_gql_schema::schema;

use super::fragments::{MoveValueRaw, ObjectFilter};
use super::objects_flat::Variables;
use super::outputs::RawMoveStruct;
use super::{objects_flat, Error};
use crate::{missing_data, GraphQlClient};

pub async fn query<C: GraphQlClient>(
    client: &C,
    object_ids: Vec<ObjectId>,
) -> Result<HashMap<ObjectId, RawMoveStruct>, Error<C::Error>> {
    let vars = Variables {
        after: None,
        first: None,
        filter: Some(ObjectFilter {
            object_ids: Some(object_ids),
            ..Default::default()
        }),
    };
    let (init, mut pages) = client
        .query_paged::<Query>(vars)
        .await
        .map_err(Error::Client)?
        .try_into_data()?
        .ok_or(missing_data!("Empty response data"))?;
    pages.insert(0, init);

    let mut raw_objs = HashMap::new();
    for object in pages.into_iter().flat_map(|q| q.objects.nodes) {
        let object_id = object.object_id;
        let struct_ = object
            .as_move_object
            .ok_or(missing_data!("Not a Move object"))?
            .contents
            .ok_or(missing_data!("Object contents"))?
            .try_into()
            .expect("Only structs can be top-level objects");
        raw_objs.insert(object_id, struct_);
    }
    Ok(raw_objs)
}

type Query = objects_flat::Query<Object>;

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

    let vars = Variables {
        filter: None,
        first: None,
        after: None,
    };
    let operation = Query::build(vars);
    insta::assert_snapshot!(operation.query);
}

#[derive(cynic::QueryFragment, Clone, Debug)]
struct Object {
    #[cynic(rename = "address")]
    object_id: ObjectId,
    as_move_object: Option<super::fragments::MoveObjectContent<MoveValueRaw>>,
}