reactive_graph_dynamic_graph_impl/object/relation/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::instance_component_id_field;
5use crate::field::relation_inbound_field;
6use crate::field::relation_instance_id_field;
7use crate::field::relation_key_field;
8use crate::field::relation_outbound_field;
9use crate::field::relation_property_field;
10use crate::interface::relation::INTERFACE_RELATION;
11use crate::object::types::DynamicGraphTypeDefinition;
12use async_graphql::dynamic::Object;
13use async_graphql::dynamic::SchemaBuilder;
14use itertools::Itertools;
15use reactive_graph_dynamic_graph_api::SchemaBuilderContext;
16use reactive_graph_graph::RelationType;
17use reactive_graph_graph::RelationTypeId;
18
19pub fn register_relation_type_query_objects(mut schema: SchemaBuilder, context: &SchemaBuilderContext) -> SchemaBuilder {
20    for relation_type in context.relation_type_manager.get_all().iter() {
21        schema = schema.register(create_relation_type_query_object(relation_type.key(), relation_type.value(), context));
22    }
23    schema
24}
25
26pub fn create_relation_type_query_object(relation_ty: &RelationTypeId, relation_type: &RelationType, context: &SchemaBuilderContext) -> Object {
27    let dy_ty = DynamicGraphTypeDefinition::from(relation_ty);
28    let mut object = Object::new(dy_ty.to_string())
29        .description(&relation_type.description)
30        .implement(INTERFACE_RELATION);
31    // Components
32    for component_ty in relation_type.components.iter() {
33        object = object.field(instance_component_id_field(&component_ty));
34        let component_dy_ty = DynamicGraphTypeDefinition::from(component_ty.key());
35        if !is_divergent(relation_type, component_ty.key()) {
36            object = object.implement(component_dy_ty.to_string());
37        }
38    }
39    // Relation Instance ID field
40    object = object.field(relation_key_field());
41    object = object.field(relation_instance_id_field());
42    for property_type in relation_type.properties.iter().sorted_by(|a, b| Ord::cmp(&a.key(), &b.key())) {
43        object = object.field(relation_property_field(property_type.value()));
44    }
45    // Look up field names and descriptions in extensions
46    let field_names = get_dynamic_graph_field_names(relation_type);
47    let field_descriptions = get_dynamic_graph_field_descriptions(relation_type);
48    // Outbound fields
49    for field in relation_outbound_field(
50        &relation_type.outbound_type,
51        field_names.from_relation_to_outbound_entity,
52        field_descriptions.from_relation_to_outbound_entity,
53        context,
54    ) {
55        object = object.field(field);
56    }
57    // Inbound fields
58    for field in relation_inbound_field(
59        &relation_type.inbound_type,
60        field_names.from_relation_to_inbound_entity,
61        field_descriptions.from_relation_to_inbound_entity,
62        context,
63    ) {
64        object = object.field(field);
65    }
66    object
67}