reactive_graph_dynamic_graph_impl/
json_schema_field_factory_impl.rs

1use crate::field::json::to_field_value;
2use crate::type_ref::TYPE_REF_JSON;
3use async_graphql::dynamic::Field;
4use async_graphql::dynamic::FieldFuture;
5use async_trait::async_trait;
6use convert_case::Case::Pascal;
7use convert_case::Casing;
8use reactive_graph_dynamic_graph_api::FIELD_JSON_SCHEMA_APPENDIX;
9use reactive_graph_dynamic_graph_api::JsonSchemaFieldFactory;
10use reactive_graph_graph::ComponentTypeId;
11use reactive_graph_graph::EntityTypeId;
12use reactive_graph_graph::FlowTypeId;
13use reactive_graph_graph::NamespacedType;
14use reactive_graph_graph::NamespacedTypeGetter;
15use reactive_graph_graph::RelationTypeId;
16use reactive_graph_graph::TypeDefinition;
17use reactive_graph_graph::TypeDefinitionJsonSchemaGetter;
18use reactive_graph_graph::TypeIdType;
19use reactive_graph_lifecycle::Lifecycle;
20use reactive_graph_type_system_api::ComponentManager;
21use reactive_graph_type_system_api::EntityTypeManager;
22use reactive_graph_type_system_api::FlowTypeManager;
23use reactive_graph_type_system_api::RelationTypeManager;
24use springtime_di::Component;
25use springtime_di::component_alias;
26use std::sync::Arc;
27
28#[derive(Component)]
29pub struct JsonSchemaFieldFactoryImpl {
30    component_manager: Arc<dyn ComponentManager + Send + Sync>,
31    entity_type_manager: Arc<dyn EntityTypeManager + Send + Sync>,
32    relation_type_manager: Arc<dyn RelationTypeManager + Send + Sync>,
33    flow_type_manager: Arc<dyn FlowTypeManager + Send + Sync>,
34}
35
36impl JsonSchemaFieldFactoryImpl {}
37
38#[async_trait]
39#[component_alias]
40impl JsonSchemaFieldFactory for JsonSchemaFieldFactoryImpl {
41    fn get_json_schema_field(&self, field_name: &str, type_definition: &TypeDefinition) -> Option<Field> {
42        let description = format!("JSON schema of {} {}", type_definition.type_id_type.full_name(), type_definition.namespace());
43        let schema = match type_definition.type_id_type {
44            TypeIdType::Component => {
45                let ty = ComponentTypeId::try_from(type_definition).ok()?;
46                self.component_manager.get(&ty)?.json_schema()
47            }
48            TypeIdType::EntityType => {
49                let ty = EntityTypeId::try_from(type_definition).ok()?;
50                self.entity_type_manager.get(&ty)?.json_schema()
51            }
52            TypeIdType::RelationType => {
53                let ty = RelationTypeId::try_from(type_definition).ok()?;
54                self.relation_type_manager.get(&ty)?.json_schema()
55            }
56            TypeIdType::FlowType => {
57                let ty = FlowTypeId::try_from(type_definition).ok()?;
58                let flow_type = self.flow_type_manager.get(&ty)?;
59                let wrapper_type = self.entity_type_manager.get(&flow_type.wrapper_type())?;
60                flow_type.json_schema(&wrapper_type).ok()?
61            }
62            _ => {
63                return None;
64            }
65        };
66        Some(
67            Field::new(field_name, TYPE_REF_JSON.clone(), move |_ctx| {
68                let schema = schema.clone();
69                FieldFuture::new(async move { Ok(to_field_value(schema.as_value().clone())) })
70            })
71            .description(description),
72        )
73    }
74}
75
76#[async_trait]
77impl Lifecycle for JsonSchemaFieldFactoryImpl {}
78
79pub fn json_schema_field_name<T: Into<NamespacedType>>(ty: T) -> String {
80    format!("_{}{}", ty.into().type_name.to_string().to_case(Pascal), FIELD_JSON_SCHEMA_APPENDIX)
81}