1
+ use graphql_client:: * ;
1
2
use crate :: blocking:: StudioClient ;
2
3
use crate :: query:: config:: is_federated;
3
4
use crate :: RoverClientError ;
5
+ use super :: types:: * ;
4
6
5
- use graphql_client:: * ;
6
-
7
- use reqwest:: Url ;
8
-
9
- type Timestamp = String ;
10
7
#[ derive( GraphQLQuery ) ]
11
8
// The paths are relative to the directory where your `Cargo.toml` is located.
12
9
// Both json and the GraphQL schema language are supported as sources for the schema
13
10
#[ graphql(
14
- query_path = "src/query/subgraph/check.graphql" ,
15
- schema_path = ".schema/schema.graphql" ,
16
- response_derives = "PartialEq, Debug, Serialize, Deserialize" ,
17
- deprecated = "warn"
11
+ query_path = "src/query/subgraph/check/check_query .graphql" ,
12
+ schema_path = ".schema/schema.graphql" ,
13
+ response_derives = "PartialEq, Debug, Serialize, Deserialize" ,
14
+ deprecated = "warn"
18
15
) ]
19
16
/// This struct is used to generate the module containing `Variables` and
20
17
/// `ResponseData` structs.
21
- /// Snake case of this name is the mod name. i.e. check_partial_schema_query
22
- pub struct CheckPartialSchemaQuery ;
18
+ /// Snake case of this name is the mod name. i.e. subgraph_check_query
19
+ pub struct SubgraphCheckQuery ;
20
+
23
21
24
22
/// The main function to be used from this module.
25
23
/// This function takes a proposed schema and validates it against a published
26
24
/// schema.
27
25
pub fn run (
28
- variables : check_partial_schema_query :: Variables ,
26
+ variables : subgraph_check_query :: Variables ,
29
27
client : & StudioClient ,
30
- ) -> Result < CheckResponse , RoverClientError > {
28
+ ) -> Result < SubgraphCheckResponse , RoverClientError > {
31
29
let graph = variables. graph_id . clone ( ) ;
32
30
// This response is used to check whether or not the current graph is federated.
33
31
let is_federated = is_federated:: run (
@@ -43,77 +41,67 @@ pub fn run(
43
41
can_operation_convert : false ,
44
42
} ) ;
45
43
}
46
- let data = client. post :: < CheckPartialSchemaQuery > ( variables) ?;
44
+ let data = client. post :: < SubgraphCheckQuery > ( variables) ?;
47
45
get_check_response_from_data ( data, graph)
48
46
}
49
47
50
- pub enum CheckResponse {
51
- CompositionErrors ( Vec < check_partial_schema_query:: CheckPartialSchemaQueryServiceCheckPartialSchemaCompositionValidationResultErrors > ) ,
52
- CheckResult ( CheckResult )
53
- }
54
-
55
- #[ derive( Debug ) ]
56
- pub struct CheckResult {
57
- pub target_url : Option < Url > ,
58
- pub number_of_checked_operations : i64 ,
59
- pub change_severity : check_partial_schema_query:: ChangeSeverity ,
60
- pub changes : Vec < check_partial_schema_query:: CheckPartialSchemaQueryServiceCheckPartialSchemaCheckSchemaResultDiffToPreviousChanges > ,
61
- }
62
-
63
48
fn get_check_response_from_data (
64
- data : check_partial_schema_query :: ResponseData ,
65
- graph : String ,
66
- ) -> Result < CheckResponse , RoverClientError > {
67
- let service = data. service . ok_or ( RoverClientError :: NoService { graph } ) ?;
49
+ data : subgraph_check_query :: ResponseData ,
50
+ graph_name : String ,
51
+ ) -> Result < SubgraphCheckResponse , RoverClientError > {
52
+ let service = data. service . ok_or ( RoverClientError :: NoService { graph : graph_name . clone ( ) } ) ?;
68
53
69
54
// for some reason this is a `Vec<Option<CompositionError>>`
70
55
// we convert this to just `Vec<CompositionError>` because the `None`
71
56
// errors would be useless.
72
- let composition_errors : Vec < check_partial_schema_query :: CheckPartialSchemaQueryServiceCheckPartialSchemaCompositionValidationResultErrors > = service
57
+ let query_composition_errors : Vec < subgraph_check_query :: SubgraphCheckQueryServiceCheckPartialSchemaCompositionValidationResultErrors > = service
73
58
. check_partial_schema
74
59
. composition_validation_result
75
60
. errors ;
76
61
77
- if composition_errors . is_empty ( ) {
62
+ if query_composition_errors . is_empty ( ) {
78
63
let check_schema_result = service. check_partial_schema . check_schema_result . ok_or (
79
64
RoverClientError :: MalformedResponse {
80
65
null_field : "service.check_partial_schema.check_schema_result" . to_string ( ) ,
81
66
} ,
82
67
) ?;
83
68
84
- let target_url = get_url ( check_schema_result. target_url ) ;
69
+ let target_url = check_schema_result. target_url . map ( |u| u . to_string ( ) ) ;
85
70
86
71
let diff_to_previous = check_schema_result. diff_to_previous ;
87
72
88
73
let number_of_checked_operations =
89
74
diff_to_previous. number_of_checked_operations . unwrap_or ( 0 ) ;
90
75
91
- let change_severity = diff_to_previous. severity ;
92
- let changes = diff_to_previous. changes ;
76
+ let change_severity = diff_to_previous. severity . into ( ) ;
77
+
78
+ let mut changes = Vec :: with_capacity ( diff_to_previous. changes . len ( ) ) ;
79
+ for change in diff_to_previous. changes {
80
+ changes. push ( SchemaChange {
81
+ code : change. code ,
82
+ severity : change. severity . into ( ) ,
83
+ description : change. description
84
+ } ) ;
85
+ }
93
86
94
- let check_result = CheckResult {
87
+ let check_result = SubgraphCheckResponse {
95
88
target_url,
96
89
number_of_checked_operations,
97
- change_severity,
98
90
changes,
91
+ change_severity
99
92
} ;
100
93
101
- Ok ( CheckResponse :: CheckResult ( check_result) )
94
+ Ok ( check_result)
102
95
} else {
103
- Ok ( CheckResponse :: CompositionErrors ( composition_errors) )
104
- }
105
- }
106
-
107
- fn get_url ( url : Option < String > ) -> Option < Url > {
108
- match url {
109
- Some ( url) => {
110
- let url = Url :: parse ( & url) ;
111
- match url {
112
- Ok ( url) => Some ( url) ,
113
- // if the API returns an invalid URL, don't put it in the response
114
- Err ( _) => None ,
115
- }
96
+ let num_failures = query_composition_errors. len ( ) ;
97
+
98
+ let mut composition_errors = Vec :: with_capacity ( num_failures) ;
99
+ for query_composition_error in query_composition_errors {
100
+ composition_errors. push ( CompositionError {
101
+ message : query_composition_error. message ,
102
+ code : query_composition_error. code
103
+ } ) ;
116
104
}
117
- None => None ,
105
+ Err ( RoverClientError :: SubgraphCompositionErrors { graph_name , composition_errors } )
118
106
}
119
107
}
0 commit comments