reactive_graph_dynamic_graph_impl/object/entity/query/
mod.rs

1use crate::extension::divergent::is_divergent;
2use crate::extension::field_description::get_dynamic_graph_field_descriptions;
3use crate::extension::field_name::get_dynamic_graph_field_names;
4use crate::field::id::entity_id_field;
5use crate::field::inbound::outbound_field::inbound_entity_to_outbound_field;
6use crate::field::inbound::relation_field::entity_inbound_relation_field;
7use crate::field::instance_component_id_field;
8use crate::field::outbound::inbound_field::outbound_entity_to_inbound_field;
9use crate::field::outbound::relation_field::entity_outbound_relation_field;
10use crate::field::property::property_field::entity_property_field;
11use crate::interface::entity::INTERFACE_ENTITY;
12use crate::object::types::DynamicGraphTypeDefinition;
13use async_graphql::dynamic::Object;
14use async_graphql::dynamic::SchemaBuilder;
15use itertools::Itertools;
16use reactive_graph_dynamic_graph_api::SchemaBuilderContext;
17use reactive_graph_graph::ComponentOrEntityTypeId;
18use reactive_graph_graph::EntityType;
19use reactive_graph_graph::RelationTypes;
20
21pub fn register_entity_type_query_objects(mut schema: SchemaBuilder, context: &SchemaBuilderContext) -> SchemaBuilder {
22    for entity_type in context.entity_type_manager.get_all().iter() {
23        let ty = ComponentOrEntityTypeId::EntityType(entity_type.key().clone());
24        let outbound_types = context.relation_type_manager.get_outbound_relation_types(&ty, false);
25        let inbound_types = context.relation_type_manager.get_inbound_relation_types(&ty, false);
26        let entity_type = create_entity_type_query_object(entity_type.value(), outbound_types, inbound_types, context);
27        schema = schema.register(entity_type);
28    }
29    schema
30}
31
32pub fn create_entity_type_query_object(
33    entity_type: &EntityType,
34    outbound_types: RelationTypes,
35    inbound_types: RelationTypes,
36    context: &SchemaBuilderContext,
37) -> Object {
38    let dy_ty = DynamicGraphTypeDefinition::from(&entity_type.ty);
39    let mut object = Object::new(dy_ty.to_string()).description(&entity_type.description).implement(INTERFACE_ENTITY);
40    // ID field
41    object = object.field(entity_id_field());
42    // Components
43    for component_ty in entity_type.components.iter() {
44        object = object.field(instance_component_id_field(component_ty.key()));
45        let component_dy_ty = DynamicGraphTypeDefinition::from(component_ty.key());
46        if !is_divergent(entity_type, component_ty.key()) {
47            object = object.implement(component_dy_ty.to_string());
48        }
49    }
50    // Property Fields
51    for property_type in entity_type.properties.iter().sorted_by(|a, b| Ord::cmp(&a.key(), &b.key())) {
52        object = object.field(entity_property_field(&property_type));
53    }
54    // Outbound Relations
55    for outbound_relation_type in outbound_types.iter() {
56        // Look up field names and descriptions in extensions
57        let field_names = get_dynamic_graph_field_names(outbound_relation_type.value());
58        let field_descriptions = get_dynamic_graph_field_descriptions(outbound_relation_type.value());
59
60        if let Some(entity_outbound_relation_field) = entity_outbound_relation_field(outbound_relation_type.value(), &field_names, &field_descriptions) {
61            object = object.field(entity_outbound_relation_field);
62        }
63        for field in outbound_entity_to_inbound_field(&outbound_relation_type, &field_names, &field_descriptions, context) {
64            object = object.field(field);
65        }
66    }
67    // Inbound Relations
68    for inbound_relation_type in inbound_types.iter() {
69        // Look up field names and descriptions in extensions
70        let field_names = get_dynamic_graph_field_names(inbound_relation_type.value());
71        let field_descriptions = get_dynamic_graph_field_descriptions(inbound_relation_type.value());
72
73        if let Some(entity_inbound_relation_field) = entity_inbound_relation_field(&inbound_relation_type, &field_names, &field_descriptions) {
74            object = object.field(entity_inbound_relation_field);
75        }
76        for field in inbound_entity_to_outbound_field(&inbound_relation_type, &field_names, &field_descriptions, context) {
77            object = object.field(field);
78        }
79    }
80    object
81}