|
| 1 | +use std::path::PathBuf; |
| 2 | + |
| 3 | +use anyhow::{Context, Result}; |
| 4 | +use prettytable::{cell, row, Table}; |
| 5 | +use serde::Serialize; |
| 6 | +use structopt::StructOpt; |
| 7 | + |
| 8 | +use rover_client::query::subgraph::check; |
| 9 | + |
| 10 | +use crate::client::get_studio_client; |
| 11 | +use crate::command::RoverStdout; |
| 12 | +use crate::utils::parsers::{parse_graph_ref, GraphRef}; |
| 13 | + |
| 14 | +#[derive(Debug, Serialize, StructOpt)] |
| 15 | +pub struct Check { |
| 16 | + /// <NAME>@<VARIANT> of graph in Apollo Studio to validate. |
| 17 | + /// @<VARIANT> may be left off, defaulting to @current |
| 18 | + #[structopt(name = "GRAPH_REF", parse(try_from_str = parse_graph_ref))] |
| 19 | + #[serde(skip_serializing)] |
| 20 | + graph: GraphRef, |
| 21 | + |
| 22 | + /// Name of the implementing service to validate |
| 23 | + #[structopt(long = "service", required = true)] |
| 24 | + #[serde(skip_serializing)] |
| 25 | + service_name: String, |
| 26 | + |
| 27 | + /// Name of configuration profile to use |
| 28 | + #[structopt(long = "profile", default_value = "default")] |
| 29 | + #[serde(skip_serializing)] |
| 30 | + profile_name: String, |
| 31 | + |
| 32 | + /// Path of .graphql/.gql schema file to push |
| 33 | + #[structopt(long = "schema", short = "s")] |
| 34 | + #[serde(skip_serializing)] |
| 35 | + schema_path: PathBuf, |
| 36 | +} |
| 37 | + |
| 38 | +impl Check { |
| 39 | + pub fn run(&self) -> Result<RoverStdout> { |
| 40 | + let client = |
| 41 | + get_studio_client(&self.profile_name).context("Failed to get studio client")?; |
| 42 | + let sdl = std::fs::read_to_string(&self.schema_path) |
| 43 | + .with_context(|| format!("Could not read file `{}`", &self.schema_path.display()))?; |
| 44 | + let partial_schema = check::check_partial_schema_query::PartialSchemaInput { |
| 45 | + sdl: Some(sdl), |
| 46 | + hash: None, |
| 47 | + }; |
| 48 | + let res = check::run( |
| 49 | + check::check_partial_schema_query::Variables { |
| 50 | + graph_id: self.graph.name.clone(), |
| 51 | + variant: self.graph.variant.clone(), |
| 52 | + partial_schema, |
| 53 | + implementing_service_name: self.service_name.clone(), |
| 54 | + }, |
| 55 | + &client, |
| 56 | + ) |
| 57 | + .context("Failed to validate schema")?; |
| 58 | + |
| 59 | + tracing::info!( |
| 60 | + "Validated schema against metrics from variant {} on subgraph {}", |
| 61 | + &self.graph.variant, |
| 62 | + &self.graph.name |
| 63 | + ); |
| 64 | + tracing::info!( |
| 65 | + "Compared {} schema changes against {} operations", |
| 66 | + res.changes.len(), |
| 67 | + res.number_of_checked_operations |
| 68 | + ); |
| 69 | + |
| 70 | + if let Some(url) = res.target_url { |
| 71 | + tracing::info!("View full details here"); |
| 72 | + tracing::info!("{}", url.to_string()); |
| 73 | + } |
| 74 | + |
| 75 | + let num_failures = print_changes(&res.changes); |
| 76 | + |
| 77 | + match num_failures { |
| 78 | + 0 => Ok(RoverStdout::None), |
| 79 | + 1 => Err(anyhow::anyhow!("Encountered 1 failure.")), |
| 80 | + _ => Err(anyhow::anyhow!("Encountered {} failures.", num_failures)), |
| 81 | + } |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +fn print_changes( |
| 86 | + checks: &[check::check_partial_schema_query::CheckPartialSchemaQueryServiceCheckPartialSchemaCheckSchemaResultDiffToPreviousChanges], |
| 87 | +) -> u64 { |
| 88 | + let mut num_failures = 0; |
| 89 | + |
| 90 | + if checks.is_empty() { |
| 91 | + let mut table = Table::new(); |
| 92 | + table.add_row(row!["Change", "Code", "Description"]); |
| 93 | + for check in checks { |
| 94 | + let change = match check.severity { |
| 95 | + check::check_partial_schema_query::ChangeSeverity::NOTICE => "PASS", |
| 96 | + check::check_partial_schema_query::ChangeSeverity::FAILURE => { |
| 97 | + num_failures += 1; |
| 98 | + "FAIL" |
| 99 | + } |
| 100 | + _ => unreachable!("Unknown change severity"), |
| 101 | + }; |
| 102 | + table.add_row(row![change, check.code, check.description]); |
| 103 | + } |
| 104 | + |
| 105 | + eprintln!("{}", table); |
| 106 | + } |
| 107 | + |
| 108 | + num_failures |
| 109 | +} |
0 commit comments