reactive_graph_runtime_graphql_schema/
properties.rs

1use std::collections::HashMap;
2
3use async_graphql::InputObject;
4use async_graphql::Object;
5use serde::Deserialize;
6use serde::Serialize;
7use serde_json::Value;
8
9use reactive_graph_graph::PropertyInstances;
10use reactive_graph_graph::PropertyTypes;
11
12#[derive(Serialize, Deserialize, Clone, Debug, InputObject)]
13#[graphql(name = "CommandResultDefinition")]
14pub struct GraphQLCommandResult {
15    /// The name of the property.
16    pub name: String,
17
18    /// The value of the property as JSON representation.
19    pub value: Value,
20}
21
22/// The named property stores a value/document as JSON representation.
23///
24/// Each property is represented by it's name (String) and it's value. The value is
25/// a representation of a JSON value/document. Therefore the value can be boolean,
26/// number, string, array or an object. For more information about the data types
27/// please look at https://docs.serde.rs/serde_json/value/enum.Value.html
28#[Object(name = "CommandResult")]
29impl GraphQLCommandResult {
30    /// The name of the property.
31    async fn name(&self) -> String {
32        self.name.clone()
33    }
34
35    /// The value of the property as JSON representation.
36    async fn value(&self) -> Value {
37        self.value.clone()
38    }
39}
40
41impl GraphQLCommandResult {
42    pub fn new(name: String, value: Value) -> Self {
43        GraphQLCommandResult { name, value }
44    }
45
46    pub fn to_map(properties: Option<Vec<GraphQLCommandResult>>) -> HashMap<String, Value> {
47        match properties {
48            Some(properties) => {
49                let mut props = HashMap::new();
50                for property in properties {
51                    props.insert(property.name.clone(), property.value.clone());
52                }
53                props
54            }
55            None => HashMap::new(),
56        }
57    }
58
59    pub fn to_map_with_defaults(properties: Option<Vec<GraphQLCommandResult>>, property_types: PropertyTypes) -> HashMap<String, Value> {
60        let mut props = HashMap::new();
61        for property_type in property_types.iter() {
62            props.insert(property_type.name.clone(), property_type.data_type.default_value());
63        }
64        if let Some(properties) = properties {
65            for property in properties {
66                props.insert(property.name.clone(), property.value.clone());
67            }
68        }
69        props
70    }
71
72    pub fn to_property_instances_with_defaults(properties: Option<Vec<GraphQLCommandResult>>, property_types: PropertyTypes) -> PropertyInstances {
73        let property_instances = PropertyInstances::new();
74        for property_type in property_types.iter() {
75            property_instances.insert(property_type.name.clone(), property_type.data_type.default_value());
76        }
77        if let Some(properties) = properties {
78            for property in properties {
79                property_instances.insert(property.name.clone(), property.value.clone());
80            }
81        }
82        property_instances
83    }
84}