Skip to content

Commit c3b0978

Browse files
fix: actually throw subgraph client errors
1 parent 8a46547 commit c3b0978

File tree

13 files changed

+89
-115
lines changed

13 files changed

+89
-115
lines changed

ARCHITECTURE.md

+5-5
Original file line numberDiff line numberDiff line change
@@ -271,15 +271,15 @@ Whenever you create a new command, make sure to add `#[serde(skip_serializing)]`
271271

272272
##### Adding a query to Apollo Studio
273273

274-
The only piece of the `rover-client` crate that we need to be concerned with for now is the `src/query` directory. This is where all the queries to Apollo Studio live. This directory is roughly organized by the command names as well, but there might be some queries in these directories that are used by multiple commands.
274+
The only piece of the `rover-client` crate that we need to be concerned with for now is the `src/operations` directory. This is where all the queries to Apollo Studio live. This directory is roughly organized by the command names as well, but there might be some queries in these directories that are used by multiple commands.
275275

276-
You can see in the `src/query/graph` directory a number of `.rs` files paired with `.graphql` files. The `.graphql` files are the files where the GraphQL operations live, and the matching `.rs` files contain the logic needed to execute those operations.
276+
You can see in the `src/operations/graph` directory a number of `.rs` files paired with `.graphql` files. The `.graphql` files are the files where the GraphQL operations live, and the matching `.rs` files contain the logic needed to execute those operations.
277277

278278
##### Writing a GraphQL operation
279279

280280
For our basic `graph hello` command, we're going to make a request to Apollo Studio that inquires about the existence of a particular graph, and nothing else. For this, we can use the `Query.service` field.
281281

282-
Create a `hello.graphql` file in `crates/rover-client/src/query/graph` and paste the following into it:
282+
Create a `hello.graphql` file in `crates/rover-client/src/operations/graph` and paste the following into it:
283283

284284
```graphql
285285
query GraphHello($graphId: ID!) {
@@ -295,9 +295,9 @@ This basic GraphQL operation uses a graph's unique ID (which we get from the `Gr
295295

296296
This project uses [graphql-client](https://docs.rs/graphql_client/latest/graphql_client/) to generate types for each raw `.graphql` query that we write.
297297

298-
First, create an empty file at `crates/rover-client/src/query/graph/hello.rs`.
298+
First, create an empty file at `crates/rover-client/src/operations/graph/hello.rs`.
299299

300-
To start compiling this file, we need to export the module in `crates/rover-client/src/query/graph/mod.rs`:
300+
To start compiling this file, we need to export the module in `crates/rover-client/src/operations/graph/mod.rs`:
301301

302302
```rust
303303
...

crates/rover-client/src/operations/graph/check/mutation_runner.rs

+9-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
use crate::blocking::StudioClient;
2-
use crate::operations::graph::check::types::{GraphCheckInput, MutationResponseData};
2+
use crate::operations::graph::check::types::{
3+
GraphCheckInput, MutationChangeSeverity, MutationResponseData,
4+
};
35
use crate::shared::CheckResponse;
46
use crate::RoverClientError;
57

@@ -45,14 +47,19 @@ fn get_check_response_from_data(
4547

4648
let change_severity = diff_to_previous.severity.into();
4749
let mut changes = Vec::with_capacity(diff_to_previous.changes.len());
50+
let mut num_failures = 0;
4851
for change in diff_to_previous.changes {
52+
if let MutationChangeSeverity::FAILURE = change.severity {
53+
num_failures += 1;
54+
}
4955
changes.push(change.into());
5056
}
5157

5258
Ok(CheckResponse {
59+
num_failures,
5360
target_url,
5461
number_of_checked_operations,
55-
change_severity,
5662
changes,
63+
change_severity,
5764
})
5865
}

crates/rover-client/src/operations/graph/check/types.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ impl From<CheckConfig> for MutationConfig {
4141
type MutationVariables = graph_check_mutation::Variables;
4242
pub(crate) type MutationResponseData = graph_check_mutation::ResponseData;
4343

44-
type MutationChangeSeverity = graph_check_mutation::ChangeSeverity;
44+
pub(crate) type MutationChangeSeverity = graph_check_mutation::ChangeSeverity;
4545
impl From<MutationChangeSeverity> for ChangeSeverity {
4646
fn from(severity: MutationChangeSeverity) -> Self {
4747
match severity {

crates/rover-client/src/operations/graph/introspect/schema.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -367,7 +367,7 @@ mod tests {
367367

368368
#[test]
369369
fn it_builds_simple_schema() {
370-
let file = File::open("src/query/graph/introspect/fixtures/simple.json").unwrap();
370+
let file = File::open("src/operations/graph/introspect/fixtures/simple.json").unwrap();
371371
let res: Response<QueryResponseData> = serde_json::from_reader(file).unwrap();
372372

373373
let data = res.data.unwrap();
@@ -401,7 +401,7 @@ mod tests {
401401

402402
#[test]
403403
fn it_builds_swapi_schema() {
404-
let file = File::open("src/query/graph/introspect/fixtures/swapi.json").unwrap();
404+
let file = File::open("src/operations/graph/introspect/fixtures/swapi.json").unwrap();
405405
let res: Response<QueryResponseData> = serde_json::from_reader(file).unwrap();
406406

407407
let data = res.data.unwrap();
@@ -1364,7 +1364,7 @@ mod tests {
13641364

13651365
#[test]
13661366
fn it_builds_schema_with_interfaces() {
1367-
let file = File::open("src/query/graph/introspect/fixtures/interfaces.json").unwrap();
1367+
let file = File::open("src/operations/graph/introspect/fixtures/interfaces.json").unwrap();
13681368
let res: Response<QueryResponseData> = serde_json::from_reader(file).unwrap();
13691369

13701370
let data = res.data.unwrap();

crates/rover-client/src/operations/subgraph/check/mutation_runner.rs

+5
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,11 @@ fn get_check_response_from_data(
7979
let change_severity = diff_to_previous.severity.into();
8080

8181
let mut changes = Vec::with_capacity(diff_to_previous.changes.len());
82+
let mut num_failures = 0;
8283
for change in diff_to_previous.changes {
84+
if let MutationChangeSeverity::FAILURE = change.severity {
85+
num_failures += 1;
86+
}
8387
changes.push(SchemaChange {
8488
code: change.code,
8589
severity: change.severity.into(),
@@ -88,6 +92,7 @@ fn get_check_response_from_data(
8892
}
8993

9094
let check_result = CheckResponse {
95+
num_failures,
9196
target_url: check_schema_result.target_url,
9297
number_of_checked_operations,
9398
changes,

crates/rover-client/src/operations/subgraph/check/types.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ pub(crate) type MutationCompositionErrors =
1010
type MutationSchema = subgraph_check_mutation::PartialSchemaInput;
1111
type MutationConfig = subgraph_check_mutation::HistoricQueryParameters;
1212

13-
type MutationChangeSeverity = subgraph_check_mutation::ChangeSeverity;
13+
pub(crate) type MutationChangeSeverity = subgraph_check_mutation::ChangeSeverity;
1414
impl From<MutationChangeSeverity> for ChangeSeverity {
1515
fn from(severity: MutationChangeSeverity) -> Self {
1616
match severity {

crates/rover-client/src/shared/check_response.rs

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use std::fmt;
44
/// `graph` and `subgraph` check operations
55
#[derive(Debug, Clone, PartialEq)]
66
pub struct CheckResponse {
7+
pub num_failures: i64,
78
pub target_url: Option<String>,
89
pub number_of_checked_operations: i64,
910
pub changes: Vec<SchemaChange>,

src/command/graph/check.rs

+18-68
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
use serde::Serialize;
22
use structopt::StructOpt;
33

4-
use rover_client::operations::graph::check;
5-
use rover_client::shared::GitContext;
4+
use rover_client::operations::graph::check::{self, GraphCheckInput};
5+
use rover_client::shared::{CheckConfig, GitContext};
66

7+
use crate::command::shared::check::print_check_response;
78
use crate::command::RoverStdout;
89
use crate::utils::client::StudioClientConfig;
910
use crate::utils::loaders::load_schema_from_flag;
1011
use crate::utils::parsers::{
1112
parse_graph_ref, parse_query_count_threshold, parse_query_percentage_threshold,
1213
parse_schema_source, parse_validation_period, GraphRef, SchemaSource, ValidationPeriod,
1314
};
14-
use crate::utils::table::{self, cell, row};
1515
use crate::Result;
1616

1717
#[derive(Debug, Serialize, StructOpt)]
@@ -56,79 +56,29 @@ impl Check {
5656
git_context: GitContext,
5757
) -> Result<RoverStdout> {
5858
let client = client_config.get_client(&self.profile_name)?;
59-
let sdl = load_schema_from_flag(&self.schema, std::io::stdin())?;
59+
let proposed_schema = load_schema_from_flag(&self.schema, std::io::stdin())?;
60+
61+
eprintln!(
62+
"Checking the proposed schema against metrics from {}",
63+
&self.graph
64+
);
65+
6066
let res = check::run(
61-
check::check_schema_query::Variables {
67+
GraphCheckInput {
6268
graph_id: self.graph.name.clone(),
63-
variant: Some(self.graph.variant.clone()),
64-
schema: Some(sdl),
65-
git_context: git_context.into(),
66-
config: check::check_schema_query::HistoricQueryParameters {
69+
variant: self.graph.variant.clone(),
70+
proposed_schema,
71+
git_context,
72+
config: CheckConfig {
6773
query_count_threshold: self.query_count_threshold,
6874
query_count_threshold_percentage: self.query_percentage_threshold,
69-
from: self.validation_period.clone().unwrap_or_default().from,
70-
to: self.validation_period.clone().unwrap_or_default().to,
71-
// we don't support configuring these, but we can't leave them out
72-
excluded_clients: None,
73-
ignored_operations: None,
74-
included_variants: None,
75+
validation_period_from: self.validation_period.clone().unwrap_or_default().from,
76+
validation_period_to: self.validation_period.clone().unwrap_or_default().to,
7577
},
7678
},
7779
&client,
7880
)?;
7981

80-
eprintln!(
81-
"Validated the proposed subgraph against metrics from {}",
82-
&self.graph
83-
);
84-
85-
let num_changes = res.changes.len();
86-
87-
let msg = match num_changes {
88-
0 => "There is no difference between the proposed graph and the graph that already exists in the graph registry. Try making a change to your proposed graph before running this command.".to_string(),
89-
_ => format!("Compared {} schema changes against {} operations", res.changes.len(), res.number_of_checked_operations),
90-
};
91-
92-
eprintln!("{}", &msg);
93-
94-
let num_failures = print_changes(&res.changes);
95-
96-
if let Some(url) = res.target_url {
97-
eprintln!("View full details at {}", &url);
98-
}
99-
100-
match num_failures {
101-
0 => Ok(RoverStdout::None),
102-
1 => Err(anyhow::anyhow!("Encountered 1 failure.").into()),
103-
_ => Err(anyhow::anyhow!("Encountered {} failures.", num_failures).into()),
104-
}
105-
}
106-
}
107-
108-
fn print_changes(
109-
checks: &[check::check_schema_query::CheckSchemaQueryServiceCheckSchemaDiffToPreviousChanges],
110-
) -> u64 {
111-
let mut num_failures = 0;
112-
113-
if !checks.is_empty() {
114-
let mut table = table::get_table();
115-
116-
// bc => sets top row to be bold and center
117-
table.add_row(row![bc => "Change", "Code", "Description"]);
118-
for check in checks {
119-
let change = match check.severity {
120-
check::check_schema_query::ChangeSeverity::NOTICE => "PASS",
121-
check::check_schema_query::ChangeSeverity::FAILURE => {
122-
num_failures += 1;
123-
"FAIL"
124-
}
125-
_ => unreachable!("Unknown change severity"),
126-
};
127-
table.add_row(row![change, check.code, check.description]);
128-
}
129-
130-
eprintln!("{}", table);
82+
print_check_response(res)
13183
}
132-
133-
num_failures
13484
}

src/command/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ mod graph;
55
mod info;
66
mod install;
77
mod output;
8+
pub(crate) mod shared;
89
mod subgraph;
910
mod supergraph;
1011
mod update;

src/command/output.rs

+1-34
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,7 @@ use crate::utils::table::{self, cell, row};
66
use ansi_term::{Colour::Yellow, Style};
77
use atty::Stream;
88
use crossterm::style::Attribute::Underlined;
9-
use rover_client::operations::subgraph::{
10-
check::SubgraphCheckResponse, list::SubgraphListResponse,
11-
};
9+
use rover_client::operations::subgraph::list::SubgraphListResponse;
1210
use termimad::MadSkin;
1311

1412
/// RoverStdout defines all of the different types of data that are printed
@@ -27,7 +25,6 @@ pub enum RoverStdout {
2725
CoreSchema(String),
2826
SchemaHash(String),
2927
SubgraphList(SubgraphListResponse),
30-
SubgraphCheck(SubgraphCheckResponse),
3128
VariantList(Vec<String>),
3229
Profiles(Vec<String>),
3330
Introspection(String),
@@ -101,36 +98,6 @@ impl RoverStdout {
10198
details.root_url, details.graph_name
10299
);
103100
}
104-
RoverStdout::SubgraphCheck(check_response) => {
105-
let num_changes = check_response.changes.len();
106-
107-
let msg = match num_changes {
108-
0 => "There were no changes detected in the composed schema.".to_string(),
109-
_ => format!(
110-
"Compared {} schema changes against {} operations",
111-
check_response.changes.len(),
112-
check_response.number_of_checked_operations
113-
),
114-
};
115-
116-
eprintln!("{}", &msg);
117-
118-
if !check_response.changes.is_empty() {
119-
let mut table = table::get_table();
120-
121-
// bc => sets top row to be bold and center
122-
table.add_row(row![bc => "Change", "Code", "Description"]);
123-
for check in &check_response.changes {
124-
table.add_row(row![check.severity, check.code, check.description]);
125-
}
126-
127-
print_content(table.to_string());
128-
}
129-
130-
if let Some(url) = &check_response.target_url {
131-
eprintln!("View full details at {}", url);
132-
}
133-
}
134101
RoverStdout::VariantList(variants) => {
135102
print_descriptor("Variants");
136103
for variant in variants {

src/command/shared/check.rs

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
use crate::command::RoverStdout;
2+
use crate::{anyhow, Result};
3+
4+
use crate::utils::table::{self, cell, row};
5+
use rover_client::shared::CheckResponse;
6+
7+
pub(crate) fn print_check_response(check_response: CheckResponse) -> Result<RoverStdout> {
8+
let num_changes = check_response.changes.len();
9+
10+
let msg = match num_changes {
11+
0 => "There were no changes detected in the composed schema.".to_string(),
12+
_ => format!(
13+
"Compared {} schema changes against {} operations",
14+
check_response.changes.len(),
15+
check_response.number_of_checked_operations
16+
),
17+
};
18+
19+
eprintln!("{}", &msg);
20+
21+
if !check_response.changes.is_empty() {
22+
let mut table = table::get_table();
23+
24+
// bc => sets top row to be bold and center
25+
table.add_row(row![bc => "Change", "Code", "Description"]);
26+
for check in &check_response.changes {
27+
table.add_row(row![check.severity, check.code, check.description]);
28+
}
29+
30+
println!("{}", table);
31+
}
32+
33+
if let Some(url) = &check_response.target_url {
34+
eprintln!("View full details at {}", url);
35+
}
36+
match &check_response.num_failures {
37+
0 => Ok(RoverStdout::None),
38+
1 => Err(anyhow!("Encountered 1 failure.").into()),
39+
_ => Err(anyhow!("Encountered {} failures.", &check_response.num_failures).into()),
40+
}
41+
}

src/command/shared/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
pub(crate) mod check;

src/command/subgraph/check.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use structopt::StructOpt;
44
use rover_client::operations::subgraph::check::{mutation_runner, SubgraphCheckInput};
55
use rover_client::shared::{CheckConfig, GitContext};
66

7+
use crate::command::shared::check::print_check_response;
78
use crate::command::RoverStdout;
89
use crate::utils::client::StudioClientConfig;
910
use crate::utils::loaders::load_schema_from_flag;
@@ -85,6 +86,6 @@ impl Check {
8586
&client,
8687
)?;
8788

88-
Ok(RoverStdout::SubgraphCheck(res))
89+
print_check_response(res)
8990
}
9091
}

0 commit comments

Comments
 (0)