reactive_graph_graphql_schema/query/behaviours/
component_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::ComponentBehaviourTypeId;
9use reactive_graph_graph::NamespacedTypeGetter;
10use reactive_graph_type_system_api::ComponentManager;
11
12use crate::query::GraphQLBehaviour;
13use crate::query::GraphQLComponent;
14
15pub struct GraphQLComponentBehaviour {
16    component_behaviour_ty: ComponentBehaviourTypeId,
17}
18
19/// A component behaviour.
20#[Object(name = "ComponentBehaviour")]
21impl GraphQLComponentBehaviour {
22    /// The component.
23    async fn component(&self, context: &Context<'_>) -> Result<GraphQLComponent> {
24        let component_manager = context.data::<Arc<dyn ComponentManager + Send + Sync>>()?;
25        let component = component_manager
26            .get(&self.component_behaviour_ty.component_ty)
27            .ok_or(Error::new(format!("Component {} does not exist!", &self.component_behaviour_ty.component_ty)))?;
28        Ok(component.into())
29    }
30
31    /// The namespace the behaviour type belongs to.
32    async fn namespace(&self) -> String {
33        self.component_behaviour_ty.behaviour_ty.namespace()
34    }
35
36    /// The name of the behaviour type.
37    async fn name(&self) -> String {
38        self.component_behaviour_ty.behaviour_ty.type_name()
39    }
40
41    /// The behaviour type.
42    async fn behaviour(&self) -> GraphQLBehaviour {
43        GraphQLBehaviour::from(self.component_behaviour_ty.behaviour_ty.clone())
44    }
45}
46
47impl From<ComponentBehaviourTypeId> for GraphQLComponentBehaviour {
48    fn from(component_behaviour_ty: ComponentBehaviourTypeId) -> Self {
49        GraphQLComponentBehaviour { component_behaviour_ty }
50    }
51}