reactive_graph_dynamic_graph_impl/field/entity/inbound/
outbound_field.rs

1use crate::extension::field_description::DynamicGraphFieldDescriptionExtension;
2use crate::extension::field_name::DynamicGraphFieldNameExtension;
3use crate::field::entity::optional_field_to_vec;
4use crate::field::inbound::outbound_entities_field::inbound_entity_to_outbound_components_field;
5use crate::field::inbound::outbound_entities_field::inbound_entity_to_outbound_entities_field;
6use crate::field::inbound::outbound_entities_field::inbound_entity_to_outbound_entities_union_field;
7use crate::union::entity::UNION_ALL_ENTITIES;
8use crate::union::entity::namespace_entities_union_type_name;
9use async_graphql::dynamic::Field;
10use log::trace;
11use reactive_graph_dynamic_graph_api::SchemaBuilderContext;
12use reactive_graph_graph::ComponentOrEntityTypeId;
13use reactive_graph_graph::NamespacedTypeGetter;
14use reactive_graph_graph::RelationType;
15
16pub fn inbound_entity_to_outbound_field(
17    inbound_relation_type: &RelationType,
18    field_names: &DynamicGraphFieldNameExtension,
19    field_descriptions: &DynamicGraphFieldDescriptionExtension,
20    context: &SchemaBuilderContext,
21) -> Vec<Field> {
22    let Some(relation_type) = context.relation_type_manager.get(&inbound_relation_type.ty) else {
23        return Vec::new();
24    };
25    let field_name = field_names.from_inbound_entity_to_outbound_entity.clone();
26    trace!("from inbound {} to outbound {:?} {}", &inbound_relation_type.ty, field_name, &relation_type.outbound_type);
27    let field_description = field_descriptions.from_inbound_entity_to_outbound_entity.clone();
28
29    match &relation_type.outbound_type {
30        ComponentOrEntityTypeId::EntityType(ty) => {
31            if ty.namespace() == "*" {
32                optional_field_to_vec(inbound_entity_to_outbound_entities_union_field(
33                    &relation_type.ty,
34                    UNION_ALL_ENTITIES,
35                    field_name,
36                    field_description,
37                ))
38            } else if ty.type_name() == "*" {
39                optional_field_to_vec(inbound_entity_to_outbound_entities_union_field(
40                    &relation_type.ty,
41                    &namespace_entities_union_type_name(&ty.namespace()),
42                    field_name,
43                    field_description,
44                ))
45            } else {
46                optional_field_to_vec(inbound_entity_to_outbound_entities_field(&relation_type.ty, ty, field_name, field_description))
47            }
48        }
49        ComponentOrEntityTypeId::Component(ty) => {
50            if ty.namespace() == "*" {
51                context
52                    .component_manager
53                    .get_type_ids()
54                    .into_iter()
55                    .filter_map(|ty| inbound_entity_to_outbound_components_field(&relation_type.ty, &ty, None, None))
56                    .collect()
57            } else if ty.type_name() == "*" {
58                context
59                    .component_manager
60                    .get_types_by_namespace(&ty.namespace())
61                    .into_iter()
62                    .filter_map(|ty| inbound_entity_to_outbound_components_field(&relation_type.ty, &ty, None, None))
63                    .collect()
64            } else {
65                optional_field_to_vec(inbound_entity_to_outbound_components_field(&relation_type.ty, ty, field_name, field_description))
66            }
67        }
68    }
69}