sui_gql_client/queries/
objects_content.rs

1use std::collections::HashMap;
2
3use af_sui_types::ObjectId;
4use sui_gql_schema::schema;
5
6use super::fragments::{MoveValueRaw, ObjectFilter};
7use super::objects_flat::Variables;
8use super::outputs::RawMoveStruct;
9use super::{objects_flat, Error};
10use crate::{missing_data, GraphQlClient};
11
12pub async fn query<C: GraphQlClient>(
13    client: &C,
14    object_ids: Vec<ObjectId>,
15) -> Result<HashMap<ObjectId, RawMoveStruct>, Error<C::Error>> {
16    let vars = Variables {
17        after: None,
18        first: None,
19        filter: Some(ObjectFilter {
20            object_ids: Some(object_ids),
21            ..Default::default()
22        }),
23    };
24    let (init, mut pages) = client
25        .query_paged::<Query>(vars)
26        .await
27        .map_err(Error::Client)?
28        .try_into_data()?
29        .ok_or(missing_data!("Empty response data"))?;
30    pages.insert(0, init);
31
32    let mut raw_objs = HashMap::new();
33    for object in pages.into_iter().flat_map(|q| q.objects.nodes) {
34        let object_id = object.object_id;
35        let struct_ = object
36            .as_move_object
37            .ok_or(missing_data!("Not a Move object"))?
38            .contents
39            .ok_or(missing_data!("Object contents"))?
40            .try_into()
41            .expect("Only structs can be top-level objects");
42        raw_objs.insert(object_id, struct_);
43    }
44    Ok(raw_objs)
45}
46
47type Query = objects_flat::Query<Object>;
48
49#[cfg(test)]
50#[allow(clippy::unwrap_used)]
51#[test]
52fn gql_output() {
53    use cynic::QueryBuilder as _;
54
55    let vars = Variables {
56        filter: None,
57        first: None,
58        after: None,
59    };
60    let operation = Query::build(vars);
61    insta::assert_snapshot!(operation.query, @r###"
62    query Query($filter: ObjectFilter, $after: String, $first: Int) {
63      objects(filter: $filter, first: $first, after: $after) {
64        nodes {
65          address
66          asMoveObject {
67            contents {
68              type {
69                repr
70              }
71              bcs
72            }
73          }
74        }
75        pageInfo {
76          hasNextPage
77          endCursor
78        }
79      }
80    }
81    "###);
82}
83
84#[derive(cynic::QueryFragment, Clone, Debug)]
85struct Object {
86    #[cynic(rename = "address")]
87    object_id: ObjectId,
88    as_move_object: Option<super::fragments::MoveObjectContent<MoveValueRaw>>,
89}