-
Notifications
You must be signed in to change notification settings - Fork 94
/
Copy pathcheck.rs
103 lines (88 loc) · 3.58 KB
/
check.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
use crate::blocking::StudioClient;
use crate::RoverClientError;
use graphql_client::*;
use reqwest::Url;
type Timestamp = String;
#[derive(GraphQLQuery)]
// The paths are relative to the directory where your `Cargo.toml` is located.
// Both json and the GraphQL schema language are supported as sources for the schema
#[graphql(
query_path = "src/query/subgraph/check.graphql",
schema_path = ".schema/schema.graphql",
response_derives = "PartialEq, Debug, Serialize, Deserialize",
deprecated = "warn"
)]
/// This struct is used to generate the module containing `Variables` and
/// `ResponseData` structs.
/// Snake case of this name is the mod name. i.e. check_partial_schema_query
pub struct CheckPartialSchemaQuery;
/// The main function to be used from this module.
/// This function takes a proposed schema and validates it against a pushed
/// schema.
pub fn run(
variables: check_partial_schema_query::Variables,
client: &StudioClient,
) -> Result<CheckResponse, RoverClientError> {
let graph = variables.graph_id.clone();
let data = client.post::<CheckPartialSchemaQuery>(variables)?;
get_check_response_from_data(data, graph)
}
pub enum CheckResponse {
CompositionErrors(Vec<check_partial_schema_query::CheckPartialSchemaQueryServiceCheckPartialSchemaCompositionValidationResultErrors>),
CheckResult(CheckResult)
}
#[derive(Debug)]
pub struct CheckResult {
pub target_url: Option<Url>,
pub number_of_checked_operations: i64,
pub change_severity: check_partial_schema_query::ChangeSeverity,
pub changes: Vec<check_partial_schema_query::CheckPartialSchemaQueryServiceCheckPartialSchemaCheckSchemaResultDiffToPreviousChanges>,
}
fn get_check_response_from_data(
data: check_partial_schema_query::ResponseData,
graph: String,
) -> Result<CheckResponse, RoverClientError> {
let service = data.service.ok_or(RoverClientError::NoService { graph })?;
// for some reason this is a `Vec<Option<CompositionError>>`
// we convert this to just `Vec<CompositionError>` because the `None`
// errors would be useless.
let composition_errors: Vec<check_partial_schema_query::CheckPartialSchemaQueryServiceCheckPartialSchemaCompositionValidationResultErrors> = service
.check_partial_schema
.composition_validation_result
.errors;
if composition_errors.is_empty() {
let check_schema_result = service.check_partial_schema.check_schema_result.ok_or(
RoverClientError::MalformedResponse {
null_field: "service.check_partial_schema.check_schema_result".to_string(),
},
)?;
let target_url = get_url(check_schema_result.target_url);
let diff_to_previous = check_schema_result.diff_to_previous;
let number_of_checked_operations =
diff_to_previous.number_of_checked_operations.unwrap_or(0);
let change_severity = diff_to_previous.severity;
let changes = diff_to_previous.changes;
let check_result = CheckResult {
target_url,
number_of_checked_operations,
change_severity,
changes,
};
Ok(CheckResponse::CheckResult(check_result))
} else {
Ok(CheckResponse::CompositionErrors(composition_errors))
}
}
fn get_url(url: Option<String>) -> Option<Url> {
match url {
Some(url) => {
let url = Url::parse(&url);
match url {
Ok(url) => Some(url),
// if the API returns an invalid URL, don't put it in the response
Err(_) => None,
}
}
None => None,
}
}