reactive_graph_graphql_schema/query/behaviours/
entity_behaviour.rs1use 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::EntityBehaviourTypeId;
9use reactive_graph_behaviour_service_api::EntityBehaviourManager;
10use reactive_graph_type_system_api::EntityTypeManager;
11
12use crate::query::GraphQLBehaviour;
13use crate::query::GraphQLEntityInstance;
14use crate::query::GraphQLEntityType;
15
16pub struct GraphQLEntityBehaviour {
17 entity_behaviour_ty: EntityBehaviourTypeId,
18}
19
20#[Object(name = "EntityBehaviour")]
22impl GraphQLEntityBehaviour {
23 async fn entity_type(&self, context: &Context<'_>) -> Result<GraphQLEntityType> {
25 let entity_type_manager = context.data::<Arc<dyn EntityTypeManager + Send + Sync>>()?;
26 let entity_type = entity_type_manager
27 .get(&self.entity_behaviour_ty.entity_ty)
28 .ok_or(Error::new(format!("Entity type {} does not exist!", &self.entity_behaviour_ty.entity_ty)))?;
29 Ok(entity_type.into())
30 }
31
32 async fn behaviour(&self) -> GraphQLBehaviour {
34 GraphQLBehaviour::from(self.entity_behaviour_ty.behaviour_ty.clone())
35 }
36
37 async fn instances(&self, context: &Context<'_>) -> Result<Vec<GraphQLEntityInstance>> {
39 let entity_behaviour_manager = context.data::<Arc<dyn EntityBehaviourManager + Send + Sync>>()?;
40 Ok(entity_behaviour_manager
41 .get_instances_by_behaviour(&self.entity_behaviour_ty.behaviour_ty)
42 .into_iter()
43 .map(GraphQLEntityInstance::from)
44 .collect())
45 }
46}
47
48impl From<EntityBehaviourTypeId> for GraphQLEntityBehaviour {
49 fn from(entity_behaviour_ty: EntityBehaviourTypeId) -> Self {
50 GraphQLEntityBehaviour { entity_behaviour_ty }
51 }
52}