reactive_graph_graphql_schema/mutation/instances/
entity_instance_definition.rs

1use async_graphql::InputObject;
2use serde::Deserialize;
3use serde::Serialize;
4use std::str::FromStr;
5use uuid::Uuid;
6
7use crate::mutation::GraphQLExtensionDefinition;
8use crate::query::GraphQLPropertyInstance;
9use reactive_graph_graph::EntityInstance;
10use reactive_graph_graph::EntityInstances;
11use reactive_graph_graph::EntityTypeId;
12use reactive_graph_graph::Extension;
13use reactive_graph_graph::Extensions;
14use reactive_graph_graph::NamespacedTypeParseError;
15use reactive_graph_graph::PropertyInstances;
16
17/// Entity instances represents a typed object which contains properties.
18///
19/// The entity type defines the properties (name, data type and socket type).
20///
21/// In contrast to the entity type the entity instance stores values in its
22/// properties.
23#[derive(Serialize, Deserialize, Clone, Debug, InputObject)]
24#[graphql(name = "EntityInstanceDefinition")]
25pub struct GraphQLEntityInstanceDefinition {
26    /// The fully qualified name of the entity type.
27    /// TODO: REGEX
28    #[graphql(name = "type")]
29    pub _type: String,
30
31    /// The unique identifier of the entity instance.
32    pub id: Uuid,
33
34    /// The description of the entity instance.
35    #[serde(default = "String::new")]
36    pub description: String,
37
38    /// The properties of then entity instance.
39    ///
40    /// Each property is represented by its name (String) and it's value. The value is
41    /// a representation of a JSON. Therefore, the value can be boolean, number, string,
42    /// array or an object. For more information about the data types please look at
43    /// https://docs.serde.rs/serde_json/value/enum.Value.html
44    pub properties: Vec<GraphQLPropertyInstance>,
45
46    // TODO: components
47    /// Entity instance specific extensions.
48    pub extensions: Vec<GraphQLExtensionDefinition>,
49}
50
51impl TryFrom<GraphQLEntityInstanceDefinition> for EntityInstance {
52    type Error = NamespacedTypeParseError;
53
54    fn try_from(entity_instance: GraphQLEntityInstanceDefinition) -> Result<Self, Self::Error> {
55        let entity_ty = EntityTypeId::from_str(&entity_instance._type)?;
56        let properties: PropertyInstances = entity_instance
57            .properties
58            .iter()
59            .map(|property_instance| (property_instance.name.clone(), property_instance.value.clone()))
60            .collect();
61        let extensions = Extensions::new();
62        for extension in entity_instance.extensions {
63            extensions.push(Extension::try_from(extension.clone())?);
64        }
65        // let components; relation_instance.components.iter().map(|e| ComponentTypeId::from(e.clone())).collect();
66        Ok(EntityInstance::builder()
67            .ty(entity_ty)
68            .id(entity_instance.id)
69            .description(&entity_instance.description)
70            .properties(properties)
71            // .components(components) ???
72            .extensions(extensions)
73            .build())
74    }
75}
76
77#[derive(Default)]
78pub struct GraphQLEntityInstanceDefinitions(pub Vec<GraphQLEntityInstanceDefinition>);
79
80impl GraphQLEntityInstanceDefinitions {
81    pub fn new(entity_instances: Vec<GraphQLEntityInstanceDefinition>) -> Self {
82        Self(entity_instances)
83    }
84}
85
86impl TryFrom<GraphQLEntityInstanceDefinitions> for EntityInstances {
87    type Error = NamespacedTypeParseError;
88
89    fn try_from(entity_instance_definitions: GraphQLEntityInstanceDefinitions) -> Result<Self, Self::Error> {
90        let entity_instances = EntityInstances::new();
91        for entity_instance_definition in entity_instance_definitions.0 {
92            entity_instances.push(EntityInstance::try_from(entity_instance_definition)?);
93        }
94        Ok(entity_instances)
95    }
96}