reactive_graph_runtime_graphql_schema/query/
mod.rs

1use std::sync::Arc;
2
3use async_graphql::Context;
4use async_graphql::Object;
5use async_graphql::Result;
6
7use reactive_graph_command_api::CommandManager;
8use reactive_graph_remotes_api::InstanceService;
9use reactive_graph_remotes_api::RemotesManager;
10
11use crate::query::command::GraphQLCommand;
12use crate::query::instance::GraphQLInstanceInfo;
13
14pub mod command;
15pub mod instance;
16
17pub struct RuntimeQuery;
18
19/// Search queries for the type system, the instances and the flows.
20#[Object(name = "Query")]
21impl RuntimeQuery {
22    /// Returns the instance information.
23    async fn instance_info(&self, context: &Context<'_>) -> Result<GraphQLInstanceInfo> {
24        let instance_service = context.data::<Arc<dyn InstanceService + Send + Sync>>()?;
25        let instance_info = instance_service.get_instance_info();
26        Ok(GraphQLInstanceInfo { instance_info })
27    }
28
29    /// Returns the list of remotes.
30    async fn remotes(&self, context: &Context<'_>) -> Result<Vec<GraphQLInstanceInfo>> {
31        let remotes_manager = context.data::<Arc<dyn RemotesManager + Send + Sync>>()?;
32        Ok(remotes_manager.get_all().into_iter().map(GraphQLInstanceInfo::from).collect())
33    }
34
35    /// Returns the commands.
36    async fn commands(&self, context: &Context<'_>, name: Option<String>) -> Result<Vec<GraphQLCommand>> {
37        let command_manager = context.data::<Arc<dyn CommandManager + Send + Sync>>()?;
38        Ok(command_manager
39            .get_commands()
40            .into_iter()
41            .filter_map(|command| match name.clone() {
42                Some(name) => {
43                    if let Some(command_name) = command.name() {
44                        if name == command_name { Some(GraphQLCommand { command }) } else { None }
45                    } else {
46                        None
47                    }
48                }
49                None => Some(GraphQLCommand { command }),
50            })
51            .collect())
52    }
53}