reactive_graph_graphql_schema/mutation/instances/
property_connector_id.rs

1use async_graphql::InputObject;
2use std::str::FromStr;
3use uuid::Uuid;
4
5use reactive_graph_graph::InstanceId;
6use reactive_graph_graph::InstanceIdError;
7use reactive_graph_graph::NamespaceSegment;
8use reactive_graph_graph::RelationInstanceTypeId;
9use reactive_graph_graph::RelationInstanceTypeIdError;
10use reactive_graph_graph::RelationTypeId;
11
12/// The
13#[derive(Debug, Clone, InputObject)]
14#[graphql(name = "PropertyConnectorOutboundInbound")]
15pub struct GraphQLPropertyConnectorOutboundInbound {
16    /// The id of the outbound/inbound entity instance.
17    pub id: Uuid,
18
19    /// The property name of the outbound/inbound entity instance.
20    pub property_name: String,
21}
22
23/// Constructs the instance_id.
24///
25/// Between two nodes only one edge with the same type can exist. Therefore, we construct a unique
26/// type which contains the names of the outbound property and the inbound property. This allows
27/// *exactly one* connector (of the given connector type) between the two properties.
28#[derive(Debug, Clone, InputObject)]
29#[graphql(name = "PropertyConnectorId")]
30pub struct GraphQLPropertyConnectorId {
31    /// The outbound entity instance and property.
32    pub outbound: GraphQLPropertyConnectorOutboundInbound,
33
34    /// The fully qualified namespace of the relation type.
35    /// TODO: REGEX
36    #[graphql(name = "name")]
37    pub namespace: String,
38
39    /// The inbound entity instance and property.
40    pub inbound: GraphQLPropertyConnectorOutboundInbound,
41}
42
43impl GraphQLPropertyConnectorId {
44    pub fn instance_id(&self) -> Result<InstanceId, RelationInstanceTypeIdError> {
45        InstanceId::new_segmented(vec![
46            NamespaceSegment::from_str(&self.outbound.property_name)
47                .map_err(InstanceIdError::NamespaceSegmentError)
48                .map_err(RelationInstanceTypeIdError::InstanceIdError)?,
49            NamespaceSegment::from_str(&self.inbound.property_name)
50                .map_err(InstanceIdError::NamespaceSegmentError)
51                .map_err(RelationInstanceTypeIdError::InstanceIdError)?,
52        ])
53        .map_err(RelationInstanceTypeIdError::InstanceIdError)
54    }
55
56    pub fn parse(&self) -> Result<RelationInstanceTypeId, RelationInstanceTypeIdError> {
57        Ok(RelationInstanceTypeId::new(
58            RelationTypeId::from_str(&self.namespace).map_err(RelationInstanceTypeIdError::NamespacedTypeParseError)?,
59            self.instance_id()?,
60        ))
61    }
62}