reactive_graph_plugin_graphql_schema/query/mod.rs
1use std::sync::Arc;
2
3use async_graphql::Context;
4use async_graphql::Object;
5use uuid::Uuid;
6
7use async_graphql::Result;
8use reactive_graph_plugin_api::PluginState;
9use reactive_graph_plugin_service_api::PluginContainerManager;
10
11pub use plugin::*;
12
13pub mod plugin;
14
15pub struct PluginQuery;
16
17/// Search queries for the type system, the instances and the flows.
18#[Object(name = "Query")]
19impl PluginQuery {
20 #[allow(clippy::too_many_arguments)]
21 async fn plugins(
22 &self,
23 context: &Context<'_>,
24 id: Option<Uuid>,
25 stem: Option<String>,
26 name: Option<String>,
27 state: Option<String>,
28 has_dependencies: Option<bool>,
29 has_unsatisfied_dependencies: Option<bool>,
30 ) -> Result<Vec<GraphQLPlugin>> {
31 let plugin_container_manager = context.data::<Arc<dyn PluginContainerManager + Send + Sync>>()?;
32 let plugins = plugin_container_manager
33 .get_plugins()
34 .into_iter()
35 .filter(|plugin_id| match &id {
36 Some(id) => plugin_id == id,
37 None => true,
38 })
39 .filter(|plugin_id| match &stem {
40 Some(stem) => match plugin_container_manager.get_id(stem.as_ref()) {
41 Some(id) => plugin_id == &id,
42 None => false,
43 },
44 None => true,
45 })
46 .filter(|plugin_id| match &name {
47 Some(name) => match plugin_container_manager.name(plugin_id) {
48 Some(plugin_name) => &plugin_name == name,
49 None => false,
50 },
51 None => true,
52 })
53 .filter(|plugin_id| match &state {
54 Some(state) => match plugin_container_manager.get_plugin_state(plugin_id) {
55 Some(PluginState::Installed) => state == "Installed",
56 Some(PluginState::Resolving(_)) => state == "Resolving",
57 Some(PluginState::Resolved) => state == "Resolved",
58 Some(PluginState::Starting(_)) => state == "Starting",
59 Some(PluginState::Active) => state == "Active",
60 Some(PluginState::Stopping(_)) => state == "Stopping",
61 Some(PluginState::Refreshing(_)) => state == "Refreshing",
62 Some(PluginState::Uninstalling(_)) => state == "Uninstalling",
63 Some(PluginState::Uninstalled) => state == "Uninstalled",
64 Some(PluginState::Disabled) => state == "Disabled",
65 None => false,
66 },
67 None => true,
68 })
69 .filter(|plugin_id| match &has_dependencies {
70 Some(true) => plugin_container_manager.has_dependencies(plugin_id),
71 Some(false) => !plugin_container_manager.has_dependencies(plugin_id),
72 None => true,
73 })
74 .filter(|plugin_id| match &has_unsatisfied_dependencies {
75 Some(true) => plugin_container_manager.has_unsatisfied_dependencies(plugin_id),
76 Some(false) => !plugin_container_manager.has_unsatisfied_dependencies(plugin_id),
77 None => true,
78 })
79 .map(|id| GraphQLPlugin { id })
80 .collect();
81 Ok(plugins)
82 }
83}
84
85// #[cfg(test)]
86// mod tests {
87// use std::sync::Arc;
88//
89// use serde::Deserialize;
90// use uuid::Uuid;
91//
92// use reactive_graph_graphql_api::GraphQLQueryService;
93// use reactive_graph_plugin_api::serde_json;
94// use reactive_graph_runtime_api::Runtime;
95// use reactive_graph_runtime_impl::RuntimeBuilder;
96//
97// #[derive(Deserialize, Debug)]
98// #[serde(rename_all = "camelCase")]
99// struct Plugin {
100// id: Uuid,
101// }
102//
103// #[derive(Deserialize, Debug)]
104// #[serde(rename_all = "camelCase")]
105// struct System {
106// plugins: Vec<Plugin>,
107// }
108//
109// #[derive(Deserialize, Debug)]
110// struct Data {
111// system: System,
112// }
113//
114// #[tokio::test(flavor = "multi_thread")]
115// async fn test_get_all_plugins() {
116// RuntimeBuilder::new()
117// .ignore_config_files()
118// .disable_all_plugins(true)
119// .pick_free_port()
120// .init()
121// .await
122// .post_init()
123// .await
124// .spawn()
125// .await
126// .with_runtime(|runtime: Arc<dyn Runtime + Send + Sync>| async move {
127// let query_service = runtime.get_graphql_query_service();
128// let plugin_container_manager = runtime.get_plugin_container_manager();
129// let rt_plugins = plugin_container_manager.get_plugins();
130// let gql_plugins: Vec<Uuid> = query_get_all_plugins(&query_service).await.iter().map(|plugin| plugin.id).collect();
131// assert_eq!(rt_plugins, gql_plugins);
132// })
133// .await
134// .stop()
135// .await
136// .pre_shutdown()
137// .await
138// .shutdown()
139// .await;
140// }
141//
142// const QUERY_GET_ALL_PLUGIN_IDS: &str = include_str!("../../graphql/get_all_ids.graphql");
143//
144// async fn query_get_all_plugins(query_service: &Arc<dyn GraphQLQueryService + Send + Sync>) -> Vec<Plugin> {
145// let response = query_service.query_response(QUERY_GET_ALL_PLUGIN_IDS).await;
146// assert!(response.errors.is_empty());
147// let data = response.data.into_json().expect("Failed to get json data from graphql response");
148// let data: Data = serde_json::from_value(data).expect("Failed to deserialize json into target data model");
149// data.system.plugins
150// }
151// }