reactive_graph_dynamic_graph_impl/
interface_manager_impl.rs1use crate::extension::divergent::is_divergent;
2use crate::field::NAMESPACE_FIELD_NAME;
3use crate::field::property::datatype::to_type_ref;
4use crate::object_type_name::object_type_name;
5use crate::type_ref::TYPE_REF_ID;
6use async_graphql::ID;
7use async_graphql::dynamic::Field;
8use async_graphql::dynamic::FieldFuture;
9use async_graphql::dynamic::FieldValue;
10use async_graphql::dynamic::Interface;
11use async_graphql::dynamic::InterfaceField;
12use async_graphql::dynamic::Object;
13use async_graphql::dynamic::TypeRef;
14use async_trait::async_trait;
15use log::info;
16use log::trace;
17use reactive_graph_dynamic_graph_api::INTERFACE_ENTITY;
18use reactive_graph_dynamic_graph_api::INTERFACE_ENTITY_FIELD_ID;
19use reactive_graph_dynamic_graph_api::INTERFACE_FLOW;
20use reactive_graph_dynamic_graph_api::INTERFACE_FLOW_FIELD_ID;
21use reactive_graph_dynamic_graph_api::INTERFACE_RELATION;
22use reactive_graph_dynamic_graph_api::INTERFACE_RELATION_FIELD_ID;
23use reactive_graph_dynamic_graph_api::INTERFACE_RELATION_FIELD_INSTANCE_ID;
24use reactive_graph_dynamic_graph_api::InterfaceManager;
25use reactive_graph_dynamic_graph_api::RootObjectType;
26use reactive_graph_graph::Component;
27use reactive_graph_graph::ComponentTypeId;
28use reactive_graph_graph::ComponentTypeIdContainer;
29use reactive_graph_graph::ExtensionContainer;
30use reactive_graph_graph::NamespacedTypeGetter;
31use reactive_graph_graph::PropertyType;
32use reactive_graph_graph::TypeDefinitionGetter;
33use reactive_graph_lifecycle::Lifecycle;
34use reactive_graph_type_system_api::ComponentManager;
35use springtime_di::Component;
36use springtime_di::component_alias;
37use std::sync::Arc;
38
39#[derive(Component)]
40pub struct InterfaceManagerImpl {
41 component_manager: Arc<dyn ComponentManager + Send + Sync>,
42}
43
44impl InterfaceManagerImpl {}
45
46#[async_trait]
47#[component_alias]
48impl InterfaceManager for InterfaceManagerImpl {
49 fn get_interfaces(&self) -> Vec<Interface> {
50 let mut interfaces = Vec::new();
51 for (_, component) in self.component_manager.get_all().into_iter() {
52 interfaces.push(self.get_component_interface(component));
53 }
54 interfaces.push(self.get_entity_interface());
55 interfaces.push(self.get_relation_interface());
56 interfaces.push(self.get_flow_interface());
57 interfaces
58 }
59
60 fn get_component_interface(&self, component: Component) -> Interface {
61 let interface_type_name = object_type_name(&component.ty, RootObjectType::Interface);
62 info!("Create interface {interface_type_name} for {}", &component.ty);
63 let mut interface = Interface::new(interface_type_name)
64 .description(&component.description)
65 .field(component_id_field(&component.ty));
66 for field in component.properties.iter() {
67 interface = interface.field(property_type_interface_field(field.value()));
68 }
69 interface
70 }
71
72 fn get_entity_interface(&self) -> Interface {
73 info!("Create interface {INTERFACE_ENTITY}");
74 Interface::new(INTERFACE_ENTITY)
75 .description("Entities have outbound and inbound relations as well as components and properties ")
76 .field(InterfaceField::new(NAMESPACE_FIELD_NAME, TYPE_REF_ID.clone()))
77 .field(InterfaceField::new(INTERFACE_ENTITY_FIELD_ID, TYPE_REF_ID.clone()))
78 }
79
80 fn get_relation_interface(&self) -> Interface {
81 info!("Create interface {INTERFACE_RELATION}");
82 Interface::new(INTERFACE_RELATION)
83 .description("Relations have a outbound entity and a inbound entity as well as components and properties,")
84 .field(InterfaceField::new(NAMESPACE_FIELD_NAME, TYPE_REF_ID.clone()))
85 .field(InterfaceField::new(INTERFACE_RELATION_FIELD_ID, TYPE_REF_ID.clone()))
86 .field(InterfaceField::new(INTERFACE_RELATION_FIELD_INSTANCE_ID, TYPE_REF_ID.clone()))
87 }
88
89 fn get_flow_interface(&self) -> Interface {
90 info!("Create interface {INTERFACE_FLOW}");
91 Interface::new(INTERFACE_FLOW)
92 .description("Flows have entities and relations.")
93 .field(InterfaceField::new(NAMESPACE_FIELD_NAME, TYPE_REF_ID.clone()))
94 .field(InterfaceField::new(INTERFACE_FLOW_FIELD_ID, TYPE_REF_ID.clone()))
95 }
96}
97
98#[async_trait]
99impl Lifecycle for InterfaceManagerImpl {}
100
101pub fn component_id_field_name(ty: &ComponentTypeId) -> String {
102 format!("_{}", ty.fully_qualified_type_name())
103}
104
105pub fn component_id_field(ty: &ComponentTypeId) -> InterfaceField {
108 InterfaceField::new(component_id_field_name(ty), TypeRef::named(TypeRef::ID))
109}
110
111pub fn instance_component_id_field(ty: &ComponentTypeId) -> Field {
112 let ty_inner = ty.clone();
113 Field::new(component_id_field_name(ty), TypeRef::named(TypeRef::ID), move |_ctx| {
114 let ty = ty_inner.clone();
115 FieldFuture::new(async move {
116 Ok(Some(FieldValue::value(ID(ty.namespace().to_string()))))
118 })
119 })
120}
121
122pub fn component_type_id_container_component_fields<T: ComponentTypeIdContainer + ExtensionContainer + TypeDefinitionGetter>(
125 component_type_id_container: &T,
126 mut component_type_id_container_object: Object,
127) -> Object {
128 for component_ty in component_type_id_container.get_components_cloned().iter() {
129 component_type_id_container_object = component_type_id_container_object.field(
131 instance_component_id_field(component_ty.key())
132 .description(format!("The fully qualified namespace to the component: {}", component_ty.namespace().to_string())),
133 );
134 if !is_divergent(component_type_id_container, component_ty.key()) {
135 let interface_type_name = object_type_name(component_ty.key(), RootObjectType::Interface);
137 trace!(
138 "{} is a {} implements interface {interface_type_name}",
139 component_type_id_container.type_definition(),
140 component_ty.key()
141 );
142 component_type_id_container_object = component_type_id_container_object.implement(interface_type_name);
143 }
144 }
145 component_type_id_container_object
146}
147
148pub fn property_type_interface_field(property_type: &PropertyType) -> InterfaceField {
150 InterfaceField::new(&property_type.name, to_type_ref(&property_type.data_type)).description(&property_type.description)
151}