reactive_graph_graphql_schema/mutation/instances/
flow_instance_definition.rs

1use async_graphql::InputObject;
2use serde::Deserialize;
3use serde::Serialize;
4use std::str::FromStr;
5use thiserror::Error;
6use uuid::Uuid;
7
8use reactive_graph_graph::EntityInstance;
9use reactive_graph_graph::EntityInstances;
10use reactive_graph_graph::EntityTypeId;
11use reactive_graph_graph::FlowInstance;
12use reactive_graph_graph::NamespacedTypeParseError;
13use reactive_graph_graph::RelationInstance;
14use reactive_graph_graph::RelationInstanceTypeIdError;
15use reactive_graph_graph::RelationInstances;
16
17use crate::mutation::GraphQLEntityInstanceDefinition;
18use crate::mutation::GraphQLRelationInstanceDefinition;
19
20/// Represents a flow with entity instances and relation instances.
21///
22/// The entity type of the flow and the entity types of each provided entity instance must exist.
23/// The relation types of each provided relation instance must exist.
24#[derive(Serialize, Deserialize, Clone, Debug, InputObject)]
25#[graphql(name = "FlowInstanceDefinition")]
26pub struct GraphQLFlowInstanceDefinition {
27    /// The id of the flow corresponds to the id of the wrapper entity instance
28    ///
29    /// This means the vector of entity instances must contain an instance with
30    /// the id of the flow.
31    pub id: Uuid,
32
33    /// The fully qualified namespace of the entity type of the wrapper entity.
34    #[graphql(name = "entityType")]
35    pub _type: String,
36
37    /// The name of the flow.
38    #[serde(default = "String::new")]
39    pub name: String,
40
41    /// Textual description of the flow.
42    #[serde(default = "String::new")]
43    pub description: String,
44
45    /// The entity instances which are contained in this flow.
46    ///
47    /// It can't have a default because the wrapper entity instance must be
48    /// present in the list of entities.
49    pub entity_instances: Vec<GraphQLEntityInstanceDefinition>,
50
51    /// The relation instances which are contained in this flow.
52    #[serde(default = "Vec::new")]
53    pub relation_instances: Vec<GraphQLRelationInstanceDefinition>,
54}
55
56impl GraphQLFlowInstanceDefinition {
57    fn entity_instances(&self) -> Result<EntityInstances, NamespacedTypeParseError> {
58        let entity_instances = EntityInstances::new();
59        for entity_instance in self.entity_instances.iter() {
60            entity_instances.push(EntityInstance::try_from(entity_instance.clone())?);
61        }
62        Ok(entity_instances)
63    }
64
65    fn relation_instances(&self) -> Result<RelationInstances, RelationInstanceTypeIdError> {
66        let relation_instances = RelationInstances::new();
67        for relation_instance in self.relation_instances.iter() {
68            relation_instances.push(RelationInstance::try_from(relation_instance.clone())?);
69        }
70        Ok(relation_instances)
71    }
72}
73
74#[derive(Debug, Error)]
75pub enum GraphQLFlowInstanceDefinitionError {
76    #[error("The namespaced type is not a valid namespace: {0}")]
77    NamespacedTypeParseError(#[from] NamespacedTypeParseError),
78    #[error("The relation instance type id is not valid: {0}")]
79    RelationInstanceTypeIdError(#[from] RelationInstanceTypeIdError),
80}
81
82impl TryFrom<GraphQLFlowInstanceDefinition> for FlowInstance {
83    type Error = GraphQLFlowInstanceDefinitionError;
84
85    fn try_from(flow: GraphQLFlowInstanceDefinition) -> Result<Self, Self::Error> {
86        let entity_ty = EntityTypeId::from_str(&flow._type)?;
87        let entity_instances = flow.entity_instances()?;
88        let relation_instances = flow.relation_instances()?;
89        Ok(FlowInstance {
90            id: flow.id,
91            ty: entity_ty,
92            name: flow.name.clone(),
93            description: flow.description.clone(),
94            entity_instances,
95            relation_instances,
96        })
97    }
98}