Is there currently a way to export the graphql schema that is implicit in the annotated rust code to a .graphql file?
I would like to have a single source of truth for my schema, and this would be a practical way to achieve this. Another approach that I considered was using juniper-from-schema and defining the single source of truth in a .graphql file. However, this doesn't seem to be as flexible in regards to re-using the datatypes inside rust for other purposes.
@lkoelman we're using something like that:
//! Binary which exports server's GraphQL schema into a JSON file.
//!
//! # Usage
//!
//! ```bash
//! cargo run --bin export_schema
//! ```
use std::{fs, io};
/// Runs application by performing introspection GraphQL query onto current
/// GraphQL schema of application and writes result into `graphql.schema.json`
/// file in the project's root.
fn main() -> io::Result<()> {
let (res, _) = juniper::introspect(
&my_api_graphql::schema::new(),
&my_api_graphql::dumb::context(),
juniper::IntrospectionFormat::default(),
)
.expect("Failed to execute introspection query");
fs::write(
"graphql.schema.json",
// "data" wrapping is required by GraphDoc.
// See: https://github.com/2fd/graphdoc/issues/54
format!(r#"{{"data":{}}}"#, serde_json::to_string_pretty(&res)?),
)?;
Ok(())
}
It's even easier using master! Check out https://graphql-rust.github.io/juniper/master/schema/schemas_and_mutations.html#outputting-schemas-in-the-a-hrefhttpsgraphqlorglearnschematype-languagegraphql-schema-languagea
RootNode now has an as_schema_language() function that should do what you want.
@LegNeato Thank you, this is exactly what I was looking for.
Great, hope it works. I wrote that support so please open another issue if there are bugs with it!
I cannot find the function as_schema_language() in version juniper = "^0.14.2"
Is it still available?
My Schema looks like this:
pub type Schema = RootNode<'static, QueryRoot, MutationRoot>;
@auryn31 unfortunately, it's available on master only.
Oh ok
Thanks