Skip to content

Commit eb85fe2

Browse files
chore: structure subgraph delete
1 parent b080bc6 commit eb85fe2

File tree

12 files changed

+336
-241
lines changed

12 files changed

+336
-241
lines changed

crates/rover-client/src/error.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -105,10 +105,10 @@ pub enum RoverClientError {
105105
source: CompositionErrors,
106106
},
107107

108-
#[error("Encountered {} while trying to compose a supergraph.", .source.get_num_errors())]
108+
#[error("Encountered {} while trying to compose a supergraph.", .source.len())]
109109
CompositionErrors { source: CompositionErrors },
110110

111-
#[error("Encountered {} while trying to compose subgraph \"{subgraph}\" into supergraph \"{graph_ref}\".", .source.get_num_errors())]
111+
#[error("Encountered {} while trying to compose subgraph \"{subgraph}\" into supergraph \"{graph_ref}\".", .source.len())]
112112
SubgraphCompositionErrors {
113113
subgraph: String,
114114
graph_ref: GraphRef,

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use crate::operations::{
44
config::is_federated::{self, IsFederatedInput},
55
subgraph::check::types::MutationResponseData,
66
};
7-
use crate::shared::{CheckResponse, CompositionError, CompositionErrors, GraphRef, SchemaChange};
7+
use crate::shared::{CheckResponse, CompositionError, GraphRef, SchemaChange};
88
use crate::RoverClientError;
99

1010
use graphql_client::*;
@@ -112,7 +112,7 @@ fn get_check_response_from_data(
112112
Err(RoverClientError::SubgraphCompositionErrors {
113113
subgraph,
114114
graph_ref,
115-
source: CompositionErrors { composition_errors },
115+
source: composition_errors.into(),
116116
})
117117
}
118118
}

crates/rover-client/src/operations/subgraph/delete/runner.rs

+16-24
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ fn get_delete_data_from_response(
4343
}
4444

4545
fn build_response(response: MutationComposition) -> SubgraphDeleteResponse {
46-
let composition_errors: Vec<CompositionError> = response
46+
let composition_errors: CompositionErrors = response
4747
.errors
4848
.iter()
4949
.filter_map(|error| {
@@ -54,15 +54,8 @@ fn build_response(response: MutationComposition) -> SubgraphDeleteResponse {
5454
})
5555
.collect();
5656

57-
// if there are no errors, just return None
58-
let composition_errors = if !composition_errors.is_empty() {
59-
Some(CompositionErrors { composition_errors })
60-
} else {
61-
None
62-
};
63-
6457
SubgraphDeleteResponse {
65-
updated_gateway: response.updated_gateway,
58+
supergraph_was_updated: response.updated_gateway,
6659
composition_errors,
6760
}
6861
}
@@ -136,19 +129,18 @@ mod tests {
136129
assert_eq!(
137130
parsed,
138131
SubgraphDeleteResponse {
139-
composition_errors: Some(CompositionErrors {
140-
composition_errors: vec![
141-
CompositionError {
142-
message: "wow".to_string(),
143-
code: None
144-
},
145-
CompositionError {
146-
message: "boo".to_string(),
147-
code: Some("BOO".to_string())
148-
}
149-
]
150-
}),
151-
updated_gateway: false,
132+
composition_errors: vec![
133+
CompositionError {
134+
message: "wow".to_string(),
135+
code: None
136+
},
137+
CompositionError {
138+
message: "boo".to_string(),
139+
code: Some("BOO".to_string())
140+
}
141+
]
142+
.into(),
143+
supergraph_was_updated: false,
152144
}
153145
);
154146
}
@@ -164,8 +156,8 @@ mod tests {
164156
assert_eq!(
165157
parsed,
166158
SubgraphDeleteResponse {
167-
composition_errors: None,
168-
updated_gateway: true,
159+
composition_errors: CompositionErrors::new(),
160+
supergraph_was_updated: true,
169161
}
170162
);
171163
}

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

+7-3
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ use crate::{
66
pub(crate) type MutationComposition = subgraph_delete_mutation::SubgraphDeleteMutationServiceRemoveImplementingServiceAndTriggerComposition;
77
pub(crate) type MutationVariables = subgraph_delete_mutation::Variables;
88

9+
use serde::Serialize;
10+
911
#[cfg(test)]
1012
pub(crate) type MutationCompositionErrors = subgraph_delete_mutation::SubgraphDeleteMutationServiceRemoveImplementingServiceAndTriggerCompositionErrors;
1113

@@ -20,10 +22,12 @@ pub struct SubgraphDeleteInput {
2022
/// `updated_gateway` is true when composition succeeds and the gateway config
2123
/// is updated for the gateway to consume. `composition_errors` is just a list
2224
/// of strings for when there are composition errors as a result of the delete.
23-
#[derive(Debug, PartialEq)]
25+
#[derive(Debug, Clone, Serialize, PartialEq)]
2426
pub struct SubgraphDeleteResponse {
25-
pub updated_gateway: bool,
26-
pub composition_errors: Option<CompositionErrors>,
27+
pub supergraph_was_updated: bool,
28+
29+
#[serde(flatten)]
30+
pub composition_errors: CompositionErrors,
2731
}
2832

2933
impl From<SubgraphDeleteInput> for MutationVariables {

crates/rover-client/src/operations/subgraph/publish/runner.rs

+19-32
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ fn get_publish_response_from_data(
6060
}
6161

6262
fn build_response(publish_response: UpdateResponse) -> SubgraphPublishResponse {
63-
let composition_errors: Vec<CompositionError> = publish_response
63+
let composition_errors: CompositionErrors = publish_response
6464
.errors
6565
.iter()
6666
.filter_map(|error| {
@@ -71,23 +71,14 @@ fn build_response(publish_response: UpdateResponse) -> SubgraphPublishResponse {
7171
})
7272
.collect();
7373

74-
// if there are no errors, just return None
75-
let composition_errors = if !composition_errors.is_empty() {
76-
Some(CompositionErrors { composition_errors })
77-
} else {
78-
None
79-
};
80-
8174
SubgraphPublishResponse {
8275
schema_hash: match publish_response.composition_config {
8376
Some(config) => Some(config.schema_hash),
8477
None => None,
8578
},
8679
supergraph_was_updated: publish_response.did_update_gateway,
8780
subgraph_was_created: publish_response.service_was_created,
88-
composition_errors: composition_errors.unwrap_or_else(|| CompositionErrors {
89-
composition_errors: vec![],
90-
}),
81+
composition_errors,
9182
}
9283
}
9384

@@ -120,18 +111,17 @@ mod tests {
120111
output,
121112
SubgraphPublishResponse {
122113
schema_hash: Some("5gf564".to_string()),
123-
composition_errors: CompositionErrors {
124-
composition_errors: vec![
125-
CompositionError {
126-
message: "[Accounts] User -> composition error".to_string(),
127-
code: None
128-
},
129-
CompositionError {
130-
message: "[Products] Product -> another one".to_string(),
131-
code: Some("ERROR".to_string())
132-
}
133-
]
134-
},
114+
composition_errors: vec![
115+
CompositionError {
116+
message: "[Accounts] User -> composition error".to_string(),
117+
code: None
118+
},
119+
CompositionError {
120+
message: "[Products] Product -> another one".to_string(),
121+
code: Some("ERROR".to_string())
122+
}
123+
]
124+
.into(),
135125
supergraph_was_updated: false,
136126
subgraph_was_created: true,
137127
}
@@ -153,9 +143,7 @@ mod tests {
153143
output,
154144
SubgraphPublishResponse {
155145
schema_hash: Some("5gf564".to_string()),
156-
composition_errors: CompositionErrors {
157-
composition_errors: vec![]
158-
},
146+
composition_errors: CompositionErrors::new(),
159147
supergraph_was_updated: true,
160148
subgraph_was_created: true,
161149
}
@@ -182,12 +170,11 @@ mod tests {
182170
output,
183171
SubgraphPublishResponse {
184172
schema_hash: None,
185-
composition_errors: CompositionErrors {
186-
composition_errors: vec![CompositionError {
187-
message: "[Accounts] -> Things went really wrong".to_string(),
188-
code: None
189-
}]
190-
},
173+
composition_errors: vec![CompositionError {
174+
message: "[Accounts] -> Things went really wrong".to_string(),
175+
code: None
176+
}]
177+
.into(),
191178
supergraph_was_updated: false,
192179
subgraph_was_created: false,
193180
}

crates/rover-client/src/operations/supergraph/fetch/runner.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ fn get_supergraph_sdl_from_response_data(
6767
} else if let Some(most_recent_composition_publish) =
6868
service_data.most_recent_composition_publish
6969
{
70-
let composition_errors = most_recent_composition_publish
70+
let composition_errors: CompositionErrors = most_recent_composition_publish
7171
.errors
7272
.into_iter()
7373
.map(|error| CompositionError {
@@ -77,7 +77,7 @@ fn get_supergraph_sdl_from_response_data(
7777
.collect();
7878
Err(RoverClientError::NoCompositionPublishes {
7979
graph_ref,
80-
source: CompositionErrors { composition_errors },
80+
source: composition_errors.into(),
8181
})
8282
} else {
8383
let mut valid_variants = Vec::new();
@@ -190,7 +190,7 @@ mod tests {
190190
let output = get_supergraph_sdl_from_response_data(data, graph_ref.clone());
191191
let expected_error = RoverClientError::NoCompositionPublishes {
192192
graph_ref,
193-
source: CompositionErrors { composition_errors },
193+
source: composition_errors.into(),
194194
}
195195
.to_string();
196196
let actual_error = output.unwrap_err().to_string();

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

+43-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use std::{
22
error::Error,
33
fmt::{self, Display},
4+
iter::FromIterator,
45
};
56

67
use serde::Serialize;
@@ -24,11 +25,17 @@ impl Display for CompositionError {
2425

2526
#[derive(Debug, Serialize, Clone, PartialEq)]
2627
pub struct CompositionErrors {
27-
pub composition_errors: Vec<CompositionError>,
28+
composition_errors: Vec<CompositionError>,
2829
}
2930

3031
impl CompositionErrors {
31-
pub fn get_num_errors(&self) -> String {
32+
pub fn new() -> Self {
33+
CompositionErrors {
34+
composition_errors: Vec::new(),
35+
}
36+
}
37+
38+
pub fn len(&self) -> String {
3239
let num_failures = self.composition_errors.len();
3340
if num_failures == 0 {
3441
unreachable!("No composition errors were encountered while composing the supergraph.");
@@ -39,6 +46,22 @@ impl CompositionErrors {
3946
_ => format!("{} composition errors", num_failures),
4047
}
4148
}
49+
50+
pub fn push(&mut self, error: CompositionError) {
51+
self.composition_errors.push(error);
52+
}
53+
54+
pub fn is_empty(&self) -> bool {
55+
self.composition_errors.is_empty()
56+
}
57+
}
58+
59+
impl Iterator for CompositionErrors {
60+
type Item = CompositionError;
61+
62+
fn next(&mut self) -> Option<Self::Item> {
63+
self.composition_errors.clone().into_iter().next()
64+
}
4265
}
4366

4467
impl Display for CompositionErrors {
@@ -50,5 +73,23 @@ impl Display for CompositionErrors {
5073
}
5174
}
5275

76+
impl From<Vec<CompositionError>> for CompositionErrors {
77+
fn from(composition_errors: Vec<CompositionError>) -> Self {
78+
CompositionErrors { composition_errors }
79+
}
80+
}
81+
82+
impl FromIterator<CompositionError> for CompositionErrors {
83+
fn from_iter<I: IntoIterator<Item = CompositionError>>(iter: I) -> Self {
84+
let mut c = CompositionErrors::new();
85+
86+
for i in iter {
87+
c.push(i);
88+
}
89+
90+
c
91+
}
92+
}
93+
5394
impl Error for CompositionError {}
5495
impl Error for CompositionErrors {}

0 commit comments

Comments
 (0)