reactive_graph_graphql_schema/query/behaviours/
relation_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::RelationBehaviourTypeId;
9use reactive_graph_behaviour_service_api::RelationBehaviourManager;
10use reactive_graph_graph::NamespacedTypeGetter;
11use reactive_graph_type_system_api::RelationTypeManager;
12
13use crate::query::GraphQLBehaviour;
14use crate::query::GraphQLRelationInstance;
15use crate::query::GraphQLRelationType;
16
17pub struct GraphQLRelationBehaviour {
18 relation_behaviour_ty: RelationBehaviourTypeId,
19}
20
21#[Object(name = "RelationBehaviour")]
23impl GraphQLRelationBehaviour {
24 async fn relation_type(&self, context: &Context<'_>) -> Result<GraphQLRelationType> {
26 let relation_type_manager = context.data::<Arc<dyn RelationTypeManager + Send + Sync>>()?;
27 let relation_type = relation_type_manager
28 .get(&self.relation_behaviour_ty.relation_ty)
29 .ok_or(Error::new(format!("Relation type {} does not exist!", &self.relation_behaviour_ty.relation_ty)))?;
30 Ok(relation_type.into())
31 }
32
33 async fn namespace(&self) -> String {
35 self.relation_behaviour_ty.behaviour_ty.namespace()
36 }
37
38 async fn name(&self) -> String {
40 self.relation_behaviour_ty.behaviour_ty.type_name()
41 }
42
43 async fn behaviour(&self) -> GraphQLBehaviour {
45 GraphQLBehaviour::from(self.relation_behaviour_ty.behaviour_ty.clone())
46 }
47
48 async fn instances(&self, context: &Context<'_>) -> Result<Vec<GraphQLRelationInstance>> {
50 let relation_behaviour_manager = context.data::<Arc<dyn RelationBehaviourManager + Send + Sync>>()?;
51 Ok(relation_behaviour_manager
52 .get_instances_by_behaviour(&self.relation_behaviour_ty.behaviour_ty)
53 .into_iter()
54 .map(GraphQLRelationInstance::from)
55 .collect())
56 }
57}
58
59impl From<RelationBehaviourTypeId> for GraphQLRelationBehaviour {
60 fn from(relation_behaviour_ty: RelationBehaviourTypeId) -> Self {
61 GraphQLRelationBehaviour { relation_behaviour_ty }
62 }
63}