|
| 1 | +use anyhow::{Context, Result}; |
| 2 | +use prettytable::{cell, row, Table}; |
| 3 | +use serde::Serialize; |
| 4 | +use structopt::StructOpt; |
| 5 | + |
| 6 | +use rover_client::query::subgraph::check; |
| 7 | + |
| 8 | +use crate::client::StudioClientConfig; |
| 9 | +use crate::command::RoverStdout; |
| 10 | +use crate::utils::loaders::load_schema_from_flag; |
| 11 | +use crate::utils::parsers::{parse_graph_ref, parse_schema_source, GraphRef, SchemaSource}; |
| 12 | + |
| 13 | +#[derive(Debug, Serialize, StructOpt)] |
| 14 | +pub struct Check { |
| 15 | + /// <NAME>@<VARIANT> of graph in Apollo Studio to validate. |
| 16 | + /// @<VARIANT> may be left off, defaulting to @current |
| 17 | + #[structopt(name = "GRAPH_REF", parse(try_from_str = parse_graph_ref))] |
| 18 | + #[serde(skip_serializing)] |
| 19 | + graph: GraphRef, |
| 20 | + |
| 21 | + /// Name of the implementing service to validate |
| 22 | + #[structopt(long = "service", required = true)] |
| 23 | + #[serde(skip_serializing)] |
| 24 | + service_name: String, |
| 25 | + |
| 26 | + /// Name of configuration profile to use |
| 27 | + #[structopt(long = "profile", default_value = "default")] |
| 28 | + #[serde(skip_serializing)] |
| 29 | + profile_name: String, |
| 30 | + |
| 31 | + /// The schema file to push |
| 32 | + /// Can pass `-` to use stdin instead of a file |
| 33 | + #[structopt(long, short = "s", parse(try_from_str = parse_schema_source))] |
| 34 | + #[serde(skip_serializing)] |
| 35 | + schema: SchemaSource, |
| 36 | +} |
| 37 | + |
| 38 | +impl Check { |
| 39 | + pub fn run(&self, client_config: StudioClientConfig) -> Result<RoverStdout> { |
| 40 | + let client = client_config.get_client(&self.profile_name)?; |
| 41 | + |
| 42 | + let sdl = load_schema_from_flag(&self.schema, std::io::stdin())?; |
| 43 | + |
| 44 | + let partial_schema = check::check_partial_schema_query::PartialSchemaInput { |
| 45 | + sdl: Some(sdl), |
| 46 | + // we never need to send the hash since the back end computes it from SDL |
| 47 | + hash: None, |
| 48 | + }; |
| 49 | + let res = check::run( |
| 50 | + check::check_partial_schema_query::Variables { |
| 51 | + graph_id: self.graph.name.clone(), |
| 52 | + variant: self.graph.variant.clone(), |
| 53 | + partial_schema, |
| 54 | + implementing_service_name: self.service_name.clone(), |
| 55 | + }, |
| 56 | + &client, |
| 57 | + ) |
| 58 | + .context("Failed to validate schema")?; |
| 59 | + |
| 60 | + tracing::info!( |
| 61 | + "Checked the proposed subgraph against {}@{}", |
| 62 | + &self.graph.name, |
| 63 | + &self.graph.variant |
| 64 | + ); |
| 65 | + |
| 66 | + match res { |
| 67 | + check::CheckResponse::CompositionErrors(composition_errors) => { |
| 68 | + handle_composition_errors(&composition_errors) |
| 69 | + } |
| 70 | + check::CheckResponse::CheckResult(check_result) => handle_checks(check_result), |
| 71 | + } |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +fn handle_checks(check_result: check::CheckResult) -> Result<RoverStdout> { |
| 76 | + let num_changes = check_result.changes.len(); |
| 77 | + |
| 78 | + let msg = match num_changes { |
| 79 | + 0 => "There were no changes detected between the proposed subgraph and the subgraph that already exists in the graph registry.".to_string(), |
| 80 | + _ => format!("Compared {} schema changes against {} operations", check_result.changes.len(), check_result.number_of_checked_operations), |
| 81 | + }; |
| 82 | + |
| 83 | + tracing::info!("{}", &msg); |
| 84 | + |
| 85 | + let mut num_failures = 0; |
| 86 | + |
| 87 | + if !check_result.changes.is_empty() { |
| 88 | + let mut table = Table::new(); |
| 89 | + table.add_row(row!["Change", "Code", "Description"]); |
| 90 | + for check in check_result.changes { |
| 91 | + let change = match check.severity { |
| 92 | + check::check_partial_schema_query::ChangeSeverity::NOTICE => "PASS", |
| 93 | + check::check_partial_schema_query::ChangeSeverity::FAILURE => { |
| 94 | + num_failures += 1; |
| 95 | + "FAIL" |
| 96 | + } |
| 97 | + _ => unreachable!("Unknown change severity"), |
| 98 | + }; |
| 99 | + table.add_row(row![change, check.code, check.description]); |
| 100 | + } |
| 101 | + |
| 102 | + eprintln!("{}", table); |
| 103 | + } |
| 104 | + |
| 105 | + if let Some(url) = check_result.target_url { |
| 106 | + tracing::info!("View full details here"); |
| 107 | + tracing::info!("{}", url.to_string()); |
| 108 | + } |
| 109 | + |
| 110 | + match num_failures { |
| 111 | + 0 => Ok(RoverStdout::None), |
| 112 | + 1 => Err(anyhow::anyhow!( |
| 113 | + "Encountered 1 failure while checking your subgraph." |
| 114 | + )), |
| 115 | + _ => Err(anyhow::anyhow!( |
| 116 | + "Encountered {} failures while checking your subgraph.", |
| 117 | + num_failures |
| 118 | + )), |
| 119 | + } |
| 120 | +} |
| 121 | + |
| 122 | +fn handle_composition_errors( |
| 123 | + composition_errors: &[check::check_partial_schema_query::CheckPartialSchemaQueryServiceCheckPartialSchemaCompositionValidationResultErrors], |
| 124 | +) -> Result<RoverStdout> { |
| 125 | + let mut num_failures = 0; |
| 126 | + for error in composition_errors { |
| 127 | + num_failures += 1; |
| 128 | + tracing::error!("{}", &error.message); |
| 129 | + } |
| 130 | + match num_failures { |
| 131 | + 0 => Ok(RoverStdout::None), |
| 132 | + 1 => Err(anyhow::anyhow!( |
| 133 | + "Encountered 1 composition error while composing the subgraph." |
| 134 | + )), |
| 135 | + _ => Err(anyhow::anyhow!( |
| 136 | + "Encountered {} composition errors while composing the subgraph.", |
| 137 | + num_failures |
| 138 | + )), |
| 139 | + } |
| 140 | +} |
0 commit comments