use std::{io::Write as _, path::Path};
use anyhow::{Context, Result};
use fraiseql_core::schema::CompiledSchema;
pub fn render(schema_path: &Path) -> Result<String> {
let json = std::fs::read_to_string(schema_path)
.with_context(|| format!("failed to read {}", schema_path.display()))?;
let schema = CompiledSchema::from_json(&json, false)
.with_context(|| format!("{} is not a compiled schema", schema_path.display()))?;
schema
.federation_service_sdl()
.with_context(|| format!("{} is not a federated subgraph", schema_path.display()))
}
pub fn run(schema_path: &Path, output: Option<&Path>) -> Result<()> {
let sdl = render(schema_path)?;
if let Some(path) = output {
return std::fs::write(path, sdl)
.with_context(|| format!("failed to write {}", path.display()));
}
let mut stdout = std::io::stdout().lock();
stdout.write_all(sdl.as_bytes()).context("failed to write the SDL to stdout")?;
stdout.flush().context("failed to write the SDL to stdout")
}