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

use af_sui_types::{Address as SuiAddress, ObjectId};

use super::fragments::MoveValueRaw;
use super::outputs::{DynamicField as OutputDf, ObjectKey, RawMoveValue};
use super::Error;
use crate::{missing_data, schema, GraphQlClient, GraphQlResponseExt as _};

pub async fn query<C: GraphQlClient>(
    client: &C,
    address: SuiAddress,
    root_version: Option<u64>,
    first: Option<i32>,
    after: Option<String>,
) -> Result<(HashMap<RawMoveValue, OutputDf>, Option<String>), Error<C::Error>> {
    let vars = QueryVariables {
        address,
        root_version,
        first,
        after,
    };
    let data = client
        .query::<Query, QueryVariables>(vars)
        .await
        .map_err(Error::Client)?
        .try_into_data()?
        .ok_or(missing_data!("Response empty"))?;

    let DynamicFieldConnection { nodes, page_info } = data
        .owner
        .ok_or(missing_data!("No owner found"))?
        .dynamic_fields;

    let next_cursor = if page_info.has_next_page {
        page_info.end_cursor
    } else {
        None
    };

    let mut df_map = HashMap::new();
    for DynamicField { name, value } in nodes {
        let name = name
            .ok_or(missing_data!("Dynamic field found but with no name"))?
            .into();
        let instance = value.ok_or(missing_data!("Dynamic field found but with no value"))?;
        let out = match instance {
            DynamicFieldValue::MoveObject(MoveObject {
                object_id,
                version,
                contents,
            }) => {
                let struct_ = contents
                    .ok_or(missing_data!("No contents for DOF"))?
                    .try_into()
                    .expect("Only Move structs can be top-level objects");
                OutputDf::Object(ObjectKey { object_id, version }, struct_)
            }
            DynamicFieldValue::MoveValue(value) => OutputDf::Field(value.into()),
            DynamicFieldValue::Unknown => return Err(missing_data!("Unknown dynamic field type")),
        };
        df_map.insert(name, out);
    }

    Ok((df_map, next_cursor))
}

#[cfg(test)]
#[allow(clippy::unwrap_used)]
#[test]
fn gql_output() {
    use cynic::QueryBuilder as _;
    let vars = QueryVariables {
        address: SuiAddress::new(rand::random()),
        root_version: None,
        first: None,
        after: None,
    };
    let operation = Query::build(vars);
    insta::assert_snapshot!(operation.query);
}

// ================================================================================
//  Mostly autogenerated by: https://generator.cynic-rs.dev/
// ================================================================================

#[derive(cynic::QueryVariables, Debug)]
struct QueryVariables {
    address: SuiAddress,
    root_version: Option<af_sui_types::Version>,
    after: Option<String>,
    first: Option<i32>,
}

#[derive(cynic::QueryFragment, Debug)]
#[cynic(variables = "QueryVariables")]
struct Query {
    #[arguments(address: $address, rootVersion: $root_version)]
    owner: Option<Owner>,
}

#[derive(cynic::QueryFragment, Debug)]
#[cynic(variables = "QueryVariables")]
struct Owner {
    #[arguments(first: $first, after: $after)]
    dynamic_fields: DynamicFieldConnection,
}

#[derive(cynic::QueryFragment, Debug)]
struct MoveObject {
    #[cynic(rename = "address")]
    object_id: ObjectId,
    version: af_sui_types::Version,
    contents: Option<MoveValueRaw>,
}

#[derive(cynic::QueryFragment, Debug)]
struct DynamicFieldConnection {
    nodes: Vec<DynamicField>,
    page_info: PageInfo,
}

#[derive(cynic::QueryFragment, Debug)]
struct PageInfo {
    has_next_page: bool,
    end_cursor: Option<String>,
}

#[derive(cynic::QueryFragment, Debug)]
struct DynamicField {
    name: Option<MoveValueRaw>,
    value: Option<DynamicFieldValue>,
}

#[derive(cynic::InlineFragments, Debug)]
enum DynamicFieldValue {
    MoveObject(MoveObject),
    MoveValue(MoveValueRaw),
    #[cynic(fallback)]
    Unknown,
}