reactive_graph_graphql_schema/query/behaviours/
relation_behaviour.rs

1use std::sync::Arc;
2
3use async_graphql::Context;
4use async_graphql::Error;
5use async_graphql::Object;
6use async_graphql::Result;
7
8use reactive_graph_behaviour_model_api::RelationBehaviourTypeId;
9use reactive_graph_behaviour_service_api::RelationBehaviourManager;
10use reactive_graph_type_system_api::RelationTypeManager;
11
12use crate::query::GraphQLBehaviour;
13use crate::query::GraphQLRelationInstance;
14use crate::query::GraphQLRelationType;
15
16pub struct GraphQLRelationBehaviour {
17    relation_behaviour_ty: RelationBehaviourTypeId,
18}
19
20/// A relation behaviour.
21#[Object(name = "RelationBehaviour")]
22impl GraphQLRelationBehaviour {
23    /// The relation type.
24    async fn relation_type(&self, context: &Context<'_>) -> Result<GraphQLRelationType> {
25        let relation_type_manager = context.data::<Arc<dyn RelationTypeManager + Send + Sync>>()?;
26        let relation_type = relation_type_manager
27            .get(&self.relation_behaviour_ty.relation_ty)
28            .ok_or(Error::new(format!("Relation type {} does not exist!", &self.relation_behaviour_ty.relation_ty)))?;
29        Ok(relation_type.into())
30    }
31
32    /// The behaviour type.
33    async fn behaviour(&self) -> GraphQLBehaviour {
34        GraphQLBehaviour::from(self.relation_behaviour_ty.behaviour_ty.clone())
35    }
36
37    /// The instances with the behaviour.
38    async fn instances(&self, context: &Context<'_>) -> Result<Vec<GraphQLRelationInstance>> {
39        let relation_behaviour_manager = context.data::<Arc<dyn RelationBehaviourManager + Send + Sync>>()?;
40        Ok(relation_behaviour_manager
41            .get_instances_by_behaviour(&self.relation_behaviour_ty.behaviour_ty)
42            .into_iter()
43            .map(GraphQLRelationInstance::from)
44            .collect())
45    }
46}
47
48impl From<RelationBehaviourTypeId> for GraphQLRelationBehaviour {
49    fn from(relation_behaviour_ty: RelationBehaviourTypeId) -> Self {
50        GraphQLRelationBehaviour { relation_behaviour_ty }
51    }
52}