reactive_graph_graphql_schema/mutation/instances/
entity_instance_definition.rs

1use async_graphql::*;
2use serde::Deserialize;
3use serde::Serialize;
4use uuid::Uuid;
5
6use reactive_graph_graph::EntityInstance;
7use reactive_graph_graph::EntityInstances;
8use reactive_graph_graph::EntityTypeId;
9use reactive_graph_graph::Extension;
10use reactive_graph_graph::Extensions;
11use reactive_graph_graph::PropertyInstances;
12
13use crate::query::GraphQLExtension;
14use crate::query::GraphQLPropertyInstance;
15
16/// Entity instances represents a typed object which contains properties.
17///
18/// The entity type defines the properties (name, data type and socket type).
19///
20/// In contrast to the entity type the entity instance stores values in its
21/// properties.
22#[derive(Serialize, Deserialize, Clone, Debug, InputObject)]
23#[graphql(name = "EntityInstanceDefinition")]
24pub struct GraphQLEntityInstanceDefinition {
25    /// The namespace the entity type belongs to.
26    pub namespace: String,
27
28    /// The name of the entity type.
29    pub type_name: 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    /// Entity instance specific extensions.
47    pub extensions: Vec<GraphQLExtension>,
48}
49
50impl From<GraphQLEntityInstanceDefinition> for EntityInstance {
51    fn from(entity_instance: GraphQLEntityInstanceDefinition) -> Self {
52        let properties: PropertyInstances = entity_instance
53            .properties
54            .iter()
55            .map(|property_instance| (property_instance.name.clone(), property_instance.value.clone()))
56            .collect();
57        let extensions: Extensions = entity_instance.extensions.iter().map(|e| Extension::from(e.clone())).collect();
58        EntityInstance::builder()
59            .ty(EntityTypeId::new_from_type(entity_instance.namespace, entity_instance.type_name))
60            .id(entity_instance.id)
61            .description(&entity_instance.description)
62            .properties(properties)
63            .extensions(extensions)
64            .build()
65    }
66}
67
68#[derive(Default)]
69pub struct GraphQLEntityInstanceDefinitions(pub Vec<GraphQLEntityInstanceDefinition>);
70
71impl GraphQLEntityInstanceDefinitions {
72    pub fn new(entity_instances: Vec<GraphQLEntityInstanceDefinition>) -> Self {
73        Self(entity_instances)
74    }
75}
76
77impl From<GraphQLEntityInstanceDefinitions> for EntityInstances {
78    fn from(entity_instances: GraphQLEntityInstanceDefinitions) -> Self {
79        entity_instances.0.into_iter().map(|entity_instance| entity_instance.into()).collect()
80    }
81}