Skip to content

Commit

Permalink
feat(supergraph): add --output flag (#561)
Browse files Browse the repository at this point in the history
useful for big, big composition results; spitting those out to stdout
can take a non-trivial amount of time, especially if they're using
`--output` on the rover side (meaning stdout is useless apart as a way
of communicating the composition result, which can be done faster with
this approach)

let me know (via slack) if you'd like more details! this is part of
trying to figure out a particular customer's pain
  • Loading branch information
aaronArinder authored Aug 12, 2024
2 parents ff69fee + 323acac commit 28947fc
Showing 1 changed file with 18 additions and 2 deletions.
20 changes: 18 additions & 2 deletions supergraph/src/command/compose.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,37 @@ use apollo_federation_types::{
};
use harmonizer::harmonize;

use std::fs::File;
use std::io::BufWriter;

#[derive(Debug, StructOpt)]
pub struct Compose {
/// The path to the fully resolved supergraph YAML.
///
/// NOTE: Each subgraph entry MUST contain raw SDL
/// as the schema source.
config_file: Utf8PathBuf,
/// Output to a file. Default is to use stdout.
output: Option<Utf8PathBuf>,
}

impl Compose {
pub fn run(&self) -> ! {
let composition_result = self.do_compose();
let composition_succeeded = composition_result.is_ok();

print!("{}", serde_json::json!(composition_result));
if let Some(filepath) = &self.output {
let json = composition_result;
let file =
File::create(filepath).expect(&format!("failed to create file at ${filepath}"));
let writer = BufWriter::new(file);
serde_json::to_writer(writer, &json)
.expect("failed to write composition result to file");
} else {
print!("{}", serde_json::json!(composition_result));
}

if composition_result.is_ok() {
if composition_succeeded {
std::process::exit(0);
} else {
std::process::exit(1);
Expand All @@ -45,6 +60,7 @@ impl Compose {
fn compose_test() {
let res = Compose {
config_file: "./tests/compose_test.yaml".into(),
output: None,
};

let result = res.do_compose();
Expand Down

0 comments on commit 28947fc

Please sign in to comment.