reactive_graph_dynamic_graph_impl/object/entity/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::EntityType;
8use reactive_graph_graph::PropertyInstanceSetter;
9use reactive_graph_graph::PropertyTypeDefinition;
10use reactive_graph_reactive_model_impl::ReactiveEntity;
11use reactive_graph_runtime_model::ActionProperties::TRIGGER;
12use reactive_graph_runtime_model::COMPONENT_ACTION;
13use serde_json::json;
14
15pub fn entity_trigger_field(entity_type: &EntityType) -> Option<Field> {
16    if !entity_type.is_a(&COMPONENT_ACTION) {
17        return None;
18    }
19    let dy_ty = DynamicGraphTypeDefinition::from(&entity_type.ty);
20    Some(Field::new(TRIGGER.property_name(), TypeRef::named_nn_list_nn(dy_ty.to_string()), move |ctx| {
21        FieldFuture::new(async move {
22            let reactive_entities = ctx.parent_value.try_downcast_ref::<Vec<ReactiveEntity>>()?;
23            for reactive_entity in reactive_entities {
24                reactive_entity.set(TRIGGER.property_name(), json!(true));
25            }
26            Ok(Some(FieldValue::list(
27                reactive_entities.iter().map(|reactive_entity| FieldValue::owned_any(reactive_entity.clone())),
28            )))
29        })
30    }))
31}