reactive_graph_dynamic_graph_impl/object/namespace/
mutation.rs1use crate::field::creation::entity_creation_field;
2use crate::field::flow::flow_creation_field;
3use crate::field::flow::flow_mutation_field;
4use crate::field::mutation::entity_mutation_field;
5use crate::field::namespace_mutation_type_name;
6use crate::field::relation_creation_field;
7use crate::field::relation_mutation_field;
8use crate::object::namespace::metrics::metrics_field;
9use crate::object::namespace::sort::sort_by_key;
10use async_graphql::dynamic::Object;
11use convert_case::Case::Pascal;
12use convert_case::Casing;
13use itertools::Itertools;
14use log::warn;
15use reactive_graph_dynamic_graph_api::SchemaBuilderContext;
16
17pub fn namespace_mutation(context: SchemaBuilderContext, namespace: &String) -> Option<Object> {
18 let entity_types = context.entity_type_manager.get_by_namespace(namespace);
19 let relation_types = context.relation_type_manager.get_by_namespace(namespace);
20 let flow_types = context.flow_type_manager.get_by_namespace(namespace);
21 if entity_types.is_empty() && relation_types.is_empty() && flow_types.is_empty() {
22 warn!("Skip empty mutation namespace {}", &namespace);
23 return None;
24 }
25 let type_name = namespace_mutation_type_name(namespace);
26 let namespace_field_value = namespace.clone();
27
28 let mut namespace = Object::new(&type_name).description(format!("Mutations for entities and relations on the namespace {}", &namespace.to_case(Pascal)));
29
30 let mut contains_field = false;
31
32 for entity_type in entity_types.iter().sorted_by(sort_by_key) {
33 if let Some(field) = entity_creation_field(entity_type.value()) {
34 namespace = namespace.field(field);
35 contains_field = true;
36 }
37 if let Some(field) = entity_mutation_field(entity_type.value()) {
38 namespace = namespace.field(field);
39 contains_field = true;
40 }
41 }
42
43 for relation_type in relation_types.iter().sorted_by(sort_by_key) {
44 if let Some(field) = relation_creation_field(relation_type.value()) {
45 namespace = namespace.field(field);
46 contains_field = true;
47 }
48 if let Some(field) = relation_mutation_field(relation_type.value()) {
49 namespace = namespace.field(field);
50 contains_field = true;
51 }
52 }
53
54 for flow_type in flow_types.iter().sorted_by(sort_by_key) {
55 if let Some(field) = flow_creation_field(flow_type.value()) {
56 namespace = namespace.field(field);
57 contains_field = true;
58 }
59 if let Some(field) = flow_mutation_field(flow_type.value()) {
60 namespace = namespace.field(field);
61 contains_field = true;
62 }
63 }
64
65 namespace = namespace.field(metrics_field(Some(namespace_field_value)));
66
67 if contains_field { Some(namespace) } else { None }
68}