reactive_graph_dynamic_graph_impl/object/namespace/
query.rs

1use crate::field::component_query_field;
2use crate::field::flow::flow_query_field;
3use crate::field::namespace_type_name;
4use crate::field::query::entity_query_field;
5use crate::field::relation_query_field;
6use crate::object::namespace::metrics::metrics_field;
7use crate::object::namespace::schema::json_schema_field;
8use crate::object::namespace::sort::sort_by_key;
9use async_graphql::dynamic::Object;
10use convert_case::Case::Pascal;
11use convert_case::Casing;
12use itertools::Itertools;
13use log::warn;
14use reactive_graph_dynamic_graph_api::SchemaBuilderContext;
15use reactive_graph_graph::TypeDefinitionJsonSchemaGetter;
16
17pub fn namespace_query(context: SchemaBuilderContext, namespace: &String) -> Option<Object> {
18    let components = context.component_manager.get_by_namespace(namespace);
19    let entity_types = context.entity_type_manager.get_by_namespace(namespace);
20    let relation_types = context.relation_type_manager.get_by_namespace(namespace);
21    let flow_types = context.flow_type_manager.get_by_namespace(namespace);
22    if components.is_empty() && entity_types.is_empty() && relation_types.is_empty() && flow_types.is_empty() {
23        warn!("Skip empty query namespace {}", &namespace);
24        return None;
25    }
26    let type_name = namespace_type_name(namespace);
27    let namespace_field_value = namespace.clone();
28
29    let mut namespace =
30        Object::new(&type_name).description(format!("Queries for components, entities and relations on the namespace {}", &namespace.to_case(Pascal)));
31
32    for component in components.iter().sorted_by(sort_by_key) {
33        namespace = namespace.field(component_query_field(component.value()));
34        namespace = namespace.field(json_schema_field(&component.ty, component.json_schema()));
35    }
36
37    for entity_type in entity_types.iter().sorted_by(sort_by_key) {
38        let entity_type = entity_type.value();
39        namespace = namespace.field(entity_query_field(entity_type));
40        namespace = namespace.field(json_schema_field(&entity_type.ty, entity_type.json_schema()));
41    }
42    for relation_type in relation_types.iter().sorted_by(sort_by_key) {
43        namespace = namespace.field(relation_query_field(relation_type.value()));
44        namespace = namespace.field(json_schema_field(&relation_type.ty, relation_type.json_schema()));
45    }
46
47    for flow_type in flow_types.iter().sorted_by(sort_by_key) {
48        namespace = namespace.field(flow_query_field(flow_type.value()));
49        if let Some(entity_type) = entity_types.get(&flow_type.wrapper_type()) {
50            if let Ok(json_schema) = flow_type.json_schema(entity_type.value()) {
51                namespace = namespace.field(json_schema_field(&flow_type.ty, json_schema));
52            }
53        }
54    }
55
56    namespace = namespace.field(metrics_field(Some(namespace_field_value)));
57    Some(namespace)
58}