reactive_graph_graphql_schema/subscription/
mod.rs1use std::sync::Arc;
2use std::time::Duration;
3
4use async_graphql::Context;
5use async_graphql::Result;
6use async_graphql::Subscription;
7use async_stream;
8use futures_util::Stream;
9use futures_util::StreamExt;
10use serde::Serialize;
11use serde_json::Value;
12use uuid::Uuid;
13
14pub use entity_instance::*;
15use reactive_graph_graph::RelationInstanceId;
16use reactive_graph_graph::SubscribeEntityInstanceError;
17use reactive_graph_graph::SubscribeRelationInstanceError;
18use reactive_graph_reactive_model_api::ReactiveInstance;
19use reactive_graph_reactive_model_api::ReactivePropertyContainer;
20pub use relation_instance::*;
21
22use crate::mutation::GraphQLRelationInstanceId;
23use crate::query::GraphQLPropertyInstance;
24use reactive_graph_reactive_service_api::ReactiveEntityManager;
25use reactive_graph_reactive_service_api::ReactiveRelationManager;
26
27pub mod entity_instance;
28pub mod relation_instance;
29
30pub struct ReactiveGraphSubscription;
31
32#[Subscription(name = "Subscription")]
34impl ReactiveGraphSubscription {
35 async fn entity(
36 &self,
37 context: &Context<'_>,
38 #[graphql(desc = "The uuid of the entity instance")] id: Option<Uuid>,
39 #[graphql(desc = "The label of the entity instance")] label: Option<String>,
40 #[graphql(desc = "The name of the property")] property_name: String,
41 ) -> Result<impl Stream<Item = GraphQLPropertyInstance>> {
42 let reactive_entity_manager = context.data::<Arc<dyn ReactiveEntityManager + Send + Sync>>()?;
43 let reactive_entity;
44 if let Some(id) = id {
45 reactive_entity = reactive_entity_manager
46 .get(id)
47 .ok_or(SubscribeEntityInstanceError::EntityInstanceDoesNotExist(id))?;
48 } else if let Some(label) = label {
49 reactive_entity = reactive_entity_manager
50 .get_by_label(label.as_str())
51 .ok_or(SubscribeEntityInstanceError::EntityInstanceWithLabelDoesNotExist(label))?;
52 } else {
53 return Err(SubscribeEntityInstanceError::EitherUuidOrLabelMustBeGiven.into());
54 }
55 if !reactive_entity.has_property(&property_name) {
56 return Err(SubscribeEntityInstanceError::PropertyNotFound(reactive_entity.id, property_name).into());
57 }
58 let entity_ty = reactive_entity.ty.clone();
59 let mut stream = EntityPropertyInstanceStream::new(reactive_entity, property_name.clone());
60
61 Ok(async_stream::stream! {
62 loop {
63 match stream.next().await {
64 Some(value) => {
65 futures_timer::Delay::new(Duration::from_millis(10)).await;
66 yield GraphQLPropertyInstance::new_entity_property(entity_ty.clone(), property_name.clone(), value.clone());
67 }
68 None => {
69 futures_timer::Delay::new(Duration::from_millis(100)).await;
70 }
71 };
72 }
73 })
74 }
75
76 async fn relation(
77 &self,
78 context: &Context<'_>,
79 relation_instance_id: GraphQLRelationInstanceId,
80 #[graphql(desc = "The name of the property")] property_name: String,
81 ) -> Result<impl Stream<Item = GraphQLPropertyInstance>> {
82 let reactive_relation_manager = context.data::<Arc<dyn ReactiveRelationManager + Send + Sync>>()?;
83 let relation_instance_id = RelationInstanceId::try_from(relation_instance_id)?;
84 let reactive_relation = reactive_relation_manager
85 .get(&relation_instance_id)
86 .ok_or(SubscribeRelationInstanceError::RelationInstanceDoesNotExist(relation_instance_id))?;
87 if !reactive_relation.has_property(&property_name) {
88 return Err(SubscribeRelationInstanceError::PropertyNotFound(reactive_relation.id(), property_name).into());
89 }
90 let relation_ty = reactive_relation.relation_type_id();
91 let mut stream = RelationPropertyInstanceStream::new(reactive_relation, property_name.clone());
92
93 Ok(async_stream::stream! {
94 loop {
95 match stream.next().await {
96 Some(value) => {
97 futures_timer::Delay::new(Duration::from_millis(10)).await;
98 yield GraphQLPropertyInstance::new_relation_property(relation_ty.clone(), property_name.clone(), value.clone());
99 }
100 None => {
101 futures_timer::Delay::new(Duration::from_millis(100)).await;
102 }
103 };
104 }
105 })
106 }
107}
108
109#[derive(Serialize)]
110pub struct GraphQLPropertyValueChanged {
111 property_name: String,
112 value: Value,
113}
114
115impl GraphQLPropertyValueChanged {
116 pub fn new(property_name: String, value: Value) -> Self {
117 GraphQLPropertyValueChanged { property_name, value }
118 }
119}
120
121impl From<GraphQLPropertyValueChanged> for Value {
122 fn from(value_changed: GraphQLPropertyValueChanged) -> Self {
123 serde_json::to_value(value_changed).unwrap()
124 }
125}