reactive_graph_utils_schema_generator/
graphql_schema.rs

1use anyhow::anyhow;
2use async_graphql::dynamic::SchemaError;
3use reactive_graph_runtime_impl::RuntimeBuilder;
4use std::fs::write;
5use std::path::Path;
6use std::path::PathBuf;
7use std::process::exit;
8use thiserror::Error;
9use workspace_root::get_workspace_root;
10
11#[derive(Debug, Error)]
12pub enum SchemaGenerationError {
13    #[error("Failed to generate Dynamic Graph Schema: {0}")]
14    DynamicSchemaError(#[from] SchemaError),
15}
16
17#[derive(Debug, Clone)]
18pub enum GraphQLSchemaTypes {
19    DynamicGraphSchema,
20    ReactiveGraphSchema,
21    ReactiveGraphPluginSchema,
22    ReactiveGraphRuntimeSchema,
23}
24
25impl GraphQLSchemaTypes {
26    pub fn schema_path(&self) -> PathBuf {
27        match self {
28            GraphQLSchemaTypes::DynamicGraphSchema => Path::new("schema/graphql/dynamic-graph-schema.graphql"),
29            GraphQLSchemaTypes::ReactiveGraphSchema => Path::new("schema/graphql/reactive-graph-schema.graphql"),
30            GraphQLSchemaTypes::ReactiveGraphPluginSchema => Path::new("schema/graphql/reactive-graph-plugin-schema.graphql"),
31            GraphQLSchemaTypes::ReactiveGraphRuntimeSchema => Path::new("schema/graphql/reactive-graph-runtime-schema.graphql"),
32        }
33        .to_owned()
34    }
35}
36
37pub fn generate_graphql_schema(schema_type: &GraphQLSchemaTypes) -> Result<String, SchemaGenerationError> {
38    let runtime = RuntimeBuilder::new().ignore_config_files().get();
39    let sdl = match schema_type {
40        GraphQLSchemaTypes::DynamicGraphSchema => runtime.get_dynamic_graph_schema_manager().create_dynamic_schema_sync()?.sdl(),
41        GraphQLSchemaTypes::ReactiveGraphSchema => runtime.get_graphql_schema_manager().get_schema().sdl(),
42        GraphQLSchemaTypes::ReactiveGraphPluginSchema => runtime.get_plugin_schema_manager().get_schema().sdl(),
43        GraphQLSchemaTypes::ReactiveGraphRuntimeSchema => runtime.get_runtime_schema_manager().get_schema().sdl(),
44    };
45    Ok(sdl)
46}
47
48pub fn write_graphql_schema(schema_type: GraphQLSchemaTypes) -> anyhow::Result<()> {
49    let schema_path = get_workspace_root().join(schema_type.schema_path());
50    if !schema_path.exists() {
51        eprintln!("Schema path doesn't exist: {:?}", schema_path.display());
52        exit(1);
53    }
54    let sdl = generate_graphql_schema(&schema_type).map_err(|e| anyhow!("Failed to generate GraphQL schema {:?}:  {:?}", schema_type, e))?;
55    write(schema_path.clone(), sdl).map_err(|_| anyhow!("Failed to write GraphQL schema {:?} to {:?}", schema_type, schema_path))?;
56    Ok(())
57}
58
59#[cfg(test)]
60mod tests {
61    use super::GraphQLSchemaTypes::ReactiveGraphPluginSchema;
62    use super::GraphQLSchemaTypes::ReactiveGraphRuntimeSchema;
63    use super::GraphQLSchemaTypes::ReactiveGraphSchema;
64    use super::generate_graphql_schema;
65
66    #[test]
67    fn test_generate_graphql_schemas() {
68        assert!(
69            generate_graphql_schema(&ReactiveGraphSchema)
70                .expect("Failed to generate Reactive Graph GraphQL Schema")
71                .len()
72                > 0
73        );
74        assert!(
75            generate_graphql_schema(&ReactiveGraphPluginSchema)
76                .expect("Failed to generate Reactive Graph Plugin GraphQL Schema")
77                .len()
78                > 0
79        );
80        assert!(
81            generate_graphql_schema(&ReactiveGraphRuntimeSchema)
82                .expect("Failed to generate Reactive Graph Runtime GraphQL Schema")
83                .len()
84                > 0
85        );
86    }
87}