reactive_graph_client/schema_graphql/instances/relation_instance/
relation_instance.rs1use crate::schema_graphql::instances::entity_instance::EntityInstance;
2use crate::schema_graphql::instances::property_instance::PropertyInstance;
3use crate::schema_graphql::types::component::Component;
4use crate::schema_graphql::types::relation_type::RelationType;
5
6use crate::PropertyInstances;
7use reactive_graph_graph::RelationInstanceTypeId;
8use reactive_graph_graph::RelationTypeId;
9use std::ops::Deref;
10
11#[derive(cynic::QueryFragment, Clone, Debug)]
12#[cynic(
13 schema_path = "../../schema/graphql/reactive-graph-schema.graphql",
14 schema_module = "crate::schema_graphql::schema"
15)]
16pub struct RelationInstance {
17 inbound: EntityInstance,
18 #[cynic(rename = "type")]
19 ty: Option<RelationType>,
20 instance_id: String,
21 outbound: EntityInstance,
22 name: String,
23 description: String,
24 properties: Vec<PropertyInstance>,
25 components: Vec<Component>,
26}
27
28impl RelationInstance {
29 pub fn ty(&self) -> RelationTypeId {
30 self.ty
31 .clone()
32 .map(|relation_type| RelationTypeId::new_from_type(relation_type.namespace, relation_type.name))
33 .unwrap_or(RelationTypeId::new_from_type(String::new(), String::new()))
34 }
35
36 pub fn instance_ty(&self) -> RelationInstanceTypeId {
37 RelationInstanceTypeId::new_unique_for_instance_id(self.ty(), self.instance_id.clone())
38 }
39}
40
41impl From<RelationInstance> for reactive_graph_graph::RelationInstance {
42 fn from(relation_instance: RelationInstance) -> Self {
43 let ty = relation_instance.instance_ty();
44 reactive_graph_graph::RelationInstance {
45 outbound_id: relation_instance.outbound.id.into(),
46 ty,
47 inbound_id: relation_instance.inbound.id.into(),
48 name: relation_instance.name.clone(),
49 description: relation_instance.description.clone(),
50 properties: PropertyInstances(relation_instance.properties).into(),
51 components: reactive_graph_graph::ComponentTypeIds::from_iter(relation_instance.components),
52 extensions: Default::default(),
53 }
54 }
55}
56
57pub struct RelationInstances(pub Vec<RelationInstance>);
58
59impl Deref for RelationInstances {
60 type Target = Vec<RelationInstance>;
61
62 fn deref(&self) -> &Self::Target {
63 &self.0
64 }
65}
66
67impl From<RelationInstances> for Vec<reactive_graph_graph::RelationInstance> {
68 fn from(relations: RelationInstances) -> Self {
69 relations.0.into_iter().map(From::from).collect()
70 }
71}
72
73impl From<RelationInstances> for reactive_graph_graph::RelationInstances {
74 fn from(relations: RelationInstances) -> Self {
75 relations.0.into_iter().map(From::from).collect()
76 }
77}