reactive_graph_graphql_schema/mutation/instances/
relation_instance_definition.rs

1use async_graphql::*;
2use serde::Deserialize;
3use serde::Serialize;
4use uuid::Uuid;
5
6use reactive_graph_graph::Extension;
7use reactive_graph_graph::Extensions;
8use reactive_graph_graph::PropertyInstances;
9use reactive_graph_graph::RelationInstance;
10use reactive_graph_graph::RelationInstanceTypeId;
11use reactive_graph_graph::RelationInstances;
12
13use crate::query::GraphQLExtension;
14use crate::query::GraphQLPropertyInstance;
15
16/// Relation instances are edges from an outbound entity instance to an
17/// inbound entity instance.
18///
19/// The relation instance is of a relation type. The relation type defines
20/// the entity types of the outbound entity instance and the inbound entity
21/// instance. Furthermore, the relation type defines which properties
22/// (name, data type, socket type) a relation instance have to have.
23///
24/// In contrast to the relation type, the relation instance stores values/
25/// documents in its properties.
26#[derive(Serialize, Deserialize, Clone, Debug, InputObject)]
27#[graphql(name = "RelationInstanceDefinition")]
28pub struct GraphQLRelationInstanceDefinition {
29    /// The id of the outbound vertex.
30    pub outbound_id: Uuid,
31
32    /// The namespace the relation type belongs to.
33    pub namespace: String,
34
35    /// The name of the relation type.
36    pub type_name: String,
37
38    /// The instance id of the relation instance type.
39    pub instance_id: String,
40
41    /// The id of the inbound vertex.
42    pub inbound_id: Uuid,
43
44    /// Textual description of the relation instance.
45    pub description: String,
46
47    /// The properties of then relation instance.
48    ///
49    /// Each property is represented by its name (String) and it's value. The value is
50    /// a representation of a JSON. Therefore, the value can be boolean, number, string,
51    /// array or an object. For more information about the data types please look at
52    /// https://docs.serde.rs/serde_json/value/enum.Value.html
53    pub properties: Vec<GraphQLPropertyInstance>,
54
55    /// Relation instance specific extensions.
56    pub extensions: Vec<GraphQLExtension>,
57}
58
59impl From<GraphQLRelationInstanceDefinition> for RelationInstance {
60    fn from(relation_instance: GraphQLRelationInstanceDefinition) -> Self {
61        let ty = RelationInstanceTypeId::new_from_type_unique_for_instance_id(
62            relation_instance.namespace,
63            relation_instance.type_name,
64            relation_instance.instance_id,
65        );
66        let properties: PropertyInstances = relation_instance
67            .properties
68            .iter()
69            .map(|property_instance| (property_instance.name.clone(), property_instance.value.clone()))
70            .collect();
71        // let components; relation_instance.components.iter().map(|e| ComponentTypeId::from(e.clone())).collect();
72        let extensions: Extensions = relation_instance.extensions.iter().map(|e| Extension::from(e.clone())).collect();
73        RelationInstance::builder()
74            .outbound_id(relation_instance.outbound_id)
75            .ty(ty)
76            .inbound_id(relation_instance.inbound_id)
77            .description(relation_instance.description)
78            .properties(properties)
79            // .components(components) ???
80            .extensions(extensions)
81            .build()
82    }
83}
84
85#[derive(Default)]
86pub struct GraphQLRelationInstanceDefinitions(pub Vec<GraphQLRelationInstanceDefinition>);
87
88impl GraphQLRelationInstanceDefinitions {
89    pub fn new(relation_instances: Vec<GraphQLRelationInstanceDefinition>) -> Self {
90        Self(relation_instances)
91    }
92}
93
94impl From<GraphQLRelationInstanceDefinitions> for RelationInstances {
95    fn from(relation_instances: GraphQLRelationInstanceDefinitions) -> Self {
96        relation_instances.0.into_iter().map(|entity_instance| entity_instance.into()).collect()
97    }
98}