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;
14use crate::query::GraphQLNamespacedType;
15
16pub struct GraphQLComponentBehaviour {
17    component_behaviour_ty: ComponentBehaviourTypeId,
18}
19
20/// A component behaviour.
21#[Object(name = "ComponentBehaviour")]
22impl GraphQLComponentBehaviour {
23    /// The component.
24    async fn component(&self, context: &Context<'_>) -> Result<GraphQLComponent> {
25        let component_manager = context.data::<Arc<dyn ComponentManager + Send + Sync>>()?;
26        let component = component_manager
27            .get(&self.component_behaviour_ty.component_ty)
28            .ok_or(Error::new(format!("Component {} does not exist!", &self.component_behaviour_ty.component_ty)))?;
29        Ok(component.into())
30    }
31
32    // TODO: this is already available via the behaviour resolver below
33    /// The namespace and type name.
34    #[graphql(name = "type")]
35    async fn ty(&self) -> GraphQLNamespacedType {
36        self.component_behaviour_ty.behaviour_ty.namespaced_type().into()
37    }
38
39    /// The behaviour type.
40    async fn behaviour(&self) -> GraphQLBehaviour {
41        GraphQLBehaviour::from(self.component_behaviour_ty.behaviour_ty.clone())
42    }
43}
44
45impl From<ComponentBehaviourTypeId> for GraphQLComponentBehaviour {
46    fn from(component_behaviour_ty: ComponentBehaviourTypeId) -> Self {
47        GraphQLComponentBehaviour { component_behaviour_ty }
48    }
49}