1
+ use super :: types:: * ;
1
2
use crate :: blocking:: StudioClient ;
2
3
use crate :: query:: config:: is_federated;
3
4
use crate :: RoverClientError ;
4
-
5
5
use graphql_client:: * ;
6
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" ,
11
+ query_path = "src/query/subgraph/check/check_query .graphql" ,
15
12
schema_path = ".schema/schema.graphql" ,
16
13
response_derives = "PartialEq, Debug, Serialize, Deserialize" ,
17
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 ;
23
20
24
21
/// The main function to be used from this module.
25
22
/// This function takes a proposed schema and validates it against a published
26
23
/// schema.
27
24
pub fn run (
28
- variables : check_partial_schema_query :: Variables ,
25
+ input : SubgraphCheckInput ,
29
26
client : & StudioClient ,
30
- ) -> Result < CheckResponse , RoverClientError > {
31
- let graph = variables . graph_id . clone ( ) ;
27
+ ) -> Result < SubgraphCheckResponse , RoverClientError > {
28
+ let graph = input . graph_id . clone ( ) ;
32
29
// This response is used to check whether or not the current graph is federated.
33
30
let is_federated = is_federated:: run (
34
31
is_federated:: is_federated_graph:: Variables {
35
- graph_id : variables . graph_id . clone ( ) ,
36
- graph_variant : variables . variant . clone ( ) ,
32
+ graph_id : input . graph_id . clone ( ) ,
33
+ graph_variant : input . variant . clone ( ) ,
37
34
} ,
38
35
& client,
39
36
) ?;
@@ -43,77 +40,71 @@ pub fn run(
43
40
can_operation_convert : false ,
44
41
} ) ;
45
42
}
46
- let data = client. post :: < CheckPartialSchemaQuery > ( variables) ?;
43
+ let variables = input. into ( ) ;
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 {
53
+ graph : graph_name. clone ( ) ,
54
+ } ) ?;
68
55
69
56
// for some reason this is a `Vec<Option<CompositionError>>`
70
57
// we convert this to just `Vec<CompositionError>` because the `None`
71
58
// errors would be useless.
72
- let composition_errors : Vec < check_partial_schema_query :: CheckPartialSchemaQueryServiceCheckPartialSchemaCompositionValidationResultErrors > = service
59
+ let query_composition_errors : Vec < subgraph_check_query :: SubgraphCheckQueryServiceCheckPartialSchemaCompositionValidationResultErrors > = service
73
60
. check_partial_schema
74
61
. composition_validation_result
75
62
. errors ;
76
63
77
- if composition_errors . is_empty ( ) {
64
+ if query_composition_errors . is_empty ( ) {
78
65
let check_schema_result = service. check_partial_schema . check_schema_result . ok_or (
79
66
RoverClientError :: MalformedResponse {
80
67
null_field : "service.check_partial_schema.check_schema_result" . to_string ( ) ,
81
68
} ,
82
69
) ?;
83
70
84
- let target_url = get_url ( check_schema_result. target_url ) ;
85
-
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 ( ) ;
93
77
94
- let check_result = CheckResult {
95
- target_url,
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
+ }
86
+
87
+ let check_result = SubgraphCheckResponse {
88
+ target_url : check_schema_result. 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 {
106
+ graph_name,
107
+ composition_errors,
108
+ } )
118
109
}
119
110
}
0 commit comments