reactive_graph_plugin_graphql_schema/query/
plugin.rs

1use std::sync::Arc;
2
3use async_graphql::Context;
4use async_graphql::Error;
5use async_graphql::Object;
6use async_graphql::Result;
7use uuid::Uuid;
8
9use reactive_graph_plugin_api::PLUGIN_NAME_PREFIX;
10use reactive_graph_plugin_service_api::PluginContainerManager;
11
12pub struct GraphQLPlugin {
13    pub id: Uuid,
14}
15
16#[Object(name = "Plugin")]
17impl GraphQLPlugin {
18    async fn id(&self) -> Uuid {
19        self.id
20    }
21
22    async fn stem(&self, context: &Context<'_>) -> Result<String> {
23        let plugin_container_manager = context.data::<Arc<dyn PluginContainerManager + Send + Sync>>()?;
24        plugin_container_manager
25            .get_stem(&self.id)
26            .ok_or_else(|| Error::new("Failed to resolve plugin stem"))
27    }
28
29    async fn path(&self, context: &Context<'_>) -> Result<String> {
30        let plugin_container_manager = context.data::<Arc<dyn PluginContainerManager + Send + Sync>>()?;
31        plugin_container_manager
32            .get_plugin_path(&self.id)
33            .ok_or_else(|| Error::new("Failed to resolve plugin path"))
34    }
35
36    async fn state(&self, context: &Context<'_>) -> Result<String> {
37        let plugin_container_manager = context.data::<Arc<dyn PluginContainerManager + Send + Sync>>()?;
38        plugin_container_manager
39            .get_plugin_state(&self.id)
40            .map(|state| format!("{state:?}"))
41            .ok_or_else(|| Error::new("Failed to resolve plugin state"))
42    }
43
44    async fn name(&self, context: &Context<'_>) -> Result<String> {
45        let plugin_container_manager = context.data::<Arc<dyn PluginContainerManager + Send + Sync>>()?;
46        plugin_container_manager
47            .name(&self.id)
48            .ok_or_else(|| Error::new("Failed to resolve plugin name"))
49    }
50
51    async fn short_name(&self, context: &Context<'_>) -> Result<String> {
52        let plugin_container_manager = context.data::<Arc<dyn PluginContainerManager + Send + Sync>>()?;
53        plugin_container_manager
54            .name(&self.id)
55            .map(|name| name.replace(PLUGIN_NAME_PREFIX, ""))
56            .ok_or_else(|| Error::new("Failed to resolve plugin short name"))
57    }
58
59    async fn description(&self, context: &Context<'_>) -> Result<String> {
60        let plugin_container_manager = context.data::<Arc<dyn PluginContainerManager + Send + Sync>>()?;
61        plugin_container_manager
62            .description(&self.id)
63            .ok_or_else(|| Error::new("Failed to resolve plugin description"))
64    }
65
66    async fn version(&self, context: &Context<'_>) -> Result<String> {
67        let plugin_container_manager = context.data::<Arc<dyn PluginContainerManager + Send + Sync>>()?;
68        plugin_container_manager
69            .version(&self.id)
70            .ok_or_else(|| Error::new("Failed to resolve plugin version"))
71    }
72
73    async fn rustc_version(&self, context: &Context<'_>) -> Result<String> {
74        let plugin_container_manager = context.data::<Arc<dyn PluginContainerManager + Send + Sync>>()?;
75        plugin_container_manager
76            .rustc_version(&self.id)
77            .ok_or_else(|| Error::new("Failed to resolve plugin rustc_version"))
78    }
79
80    async fn plugin_api_version(&self, context: &Context<'_>) -> Result<String> {
81        let plugin_container_manager = context.data::<Arc<dyn PluginContainerManager + Send + Sync>>()?;
82        plugin_container_manager
83            .plugin_api_version(&self.id)
84            .ok_or_else(|| Error::new("Failed to resolve plugin plugin_api_version"))
85    }
86
87    async fn dependencies(&self, context: &Context<'_>) -> Result<Vec<GraphQLPlugin>> {
88        let plugin_container_manager = context.data::<Arc<dyn PluginContainerManager + Send + Sync>>()?;
89        let mut dependencies = Vec::new();
90        for dependency in plugin_container_manager.get_dependencies(&self.id) {
91            if let Some(dependency_id) = plugin_container_manager.get_plugin_by_dependency(&dependency) {
92                dependencies.push(GraphQLPlugin { id: dependency_id })
93            }
94        }
95        Ok(dependencies)
96    }
97
98    async fn unsatisfied_dependencies(&self, context: &Context<'_>) -> Result<Vec<GraphQLPlugin>> {
99        let plugin_container_manager = context.data::<Arc<dyn PluginContainerManager + Send + Sync>>()?;
100        let mut dependencies = Vec::new();
101        for dependency in plugin_container_manager.get_unsatisfied_dependencies(&self.id) {
102            if let Some(dependency_id) = plugin_container_manager.get_plugin_by_dependency(&dependency) {
103                dependencies.push(GraphQLPlugin { id: dependency_id })
104            }
105        }
106        Ok(dependencies)
107    }
108
109    async fn dependents(&self, context: &Context<'_>) -> Result<Vec<GraphQLPlugin>> {
110        let plugin_container_manager = context.data::<Arc<dyn PluginContainerManager + Send + Sync>>()?;
111        let mut dependents = Vec::new();
112        for id in plugin_container_manager.get_dependents(&self.id) {
113            dependents.push(GraphQLPlugin { id })
114        }
115        Ok(dependents)
116    }
117}