reactive_graph_dynamic_graph_impl/object/flow/mutation/
trigger.rs

1use crate::object::types::DynamicGraphTypeDefinition;
2use async_graphql::dynamic::Field;
3use async_graphql::dynamic::FieldFuture;
4use async_graphql::dynamic::FieldValue;
5use async_graphql::dynamic::TypeRef;
6use reactive_graph_graph::ComponentTypeIdContainer;
7use reactive_graph_graph::FlowType;
8use reactive_graph_graph::PropertyInstanceSetter;
9use reactive_graph_graph::PropertyTypeDefinition;
10use reactive_graph_reactive_model_impl::ReactiveFlow;
11use reactive_graph_runtime_model::ActionProperties::TRIGGER;
12use reactive_graph_runtime_model::COMPONENT_ACTION;
13use serde_json::json;
14
15pub fn get_flow_type_trigger_field(flow_type: &FlowType) -> Option<Field> {
16    if !flow_type.wrapper_entity_instance.is_a(&COMPONENT_ACTION) {
17        return None;
18    }
19    let dy_ty = DynamicGraphTypeDefinition::from(&flow_type.ty);
20    let trigger_field = Field::new(TRIGGER.property_name(), TypeRef::named_nn_list_nn(dy_ty.to_string()), move |ctx| {
21        FieldFuture::new(async move {
22            let flow_instances = ctx.parent_value.try_downcast_ref::<Vec<ReactiveFlow>>()?;
23            for flow_instance in flow_instances {
24                flow_instance.set(TRIGGER.property_name(), json!(true));
25            }
26            Ok(Some(FieldValue::list(
27                flow_instances.iter().map(|flow_instance| FieldValue::owned_any(flow_instance.clone())),
28            )))
29        })
30    });
31    Some(trigger_field)
32}