@@ -15,7 +15,9 @@ use super::NamedFragments;
15
15
use super :: Selection ;
16
16
use super :: SelectionSet ;
17
17
use super :: SelectionValue ;
18
+ use crate :: ensure;
18
19
use crate :: error:: FederationError ;
20
+ use crate :: internal_error;
19
21
20
22
impl < ' a > FieldSelectionValue < ' a > {
21
23
/// Merges the given field selections into this one.
@@ -36,31 +38,29 @@ impl<'a> FieldSelectionValue<'a> {
36
38
let mut selection_sets = vec ! [ ] ;
37
39
for other in others {
38
40
let other_field = & other. field ;
39
- if other_field. schema != self_field. schema {
40
- return Err ( FederationError :: internal (
41
- "Cannot merge field selections from different schemas" ,
42
- ) ) ;
43
- }
44
- if other_field. field_position != self_field. field_position {
45
- return Err ( FederationError :: internal ( format ! (
46
- "Cannot merge field selection for field \" {}\" into a field selection for field \" {}\" " ,
47
- other_field. field_position,
48
- self_field. field_position,
49
- ) ) ) ;
50
- }
41
+ ensure ! (
42
+ other_field. schema == self_field. schema,
43
+ "Cannot merge field selections from different schemas" ,
44
+ ) ;
45
+ ensure ! (
46
+ other_field. field_position == self_field. field_position,
47
+ "Cannot merge field selection for field \" {}\" into a field selection for field \" {}\" " ,
48
+ other_field. field_position,
49
+ self_field. field_position,
50
+ ) ;
51
51
if self . get ( ) . selection_set . is_some ( ) {
52
52
let Some ( other_selection_set) = & other. selection_set else {
53
- return Err ( FederationError :: internal ( format ! (
53
+ internal_error ! (
54
54
"Field \" {}\" has composite type but not a selection set" ,
55
55
other_field. field_position,
56
- ) ) ) ;
56
+ ) ;
57
57
} ;
58
58
selection_sets. push ( other_selection_set) ;
59
59
} else if other. selection_set . is_some ( ) {
60
- return Err ( FederationError :: internal ( format ! (
60
+ internal_error ! (
61
61
"Field \" {}\" has non-composite type but also has a selection set" ,
62
62
other_field. field_position,
63
- ) ) ) ;
63
+ ) ;
64
64
}
65
65
}
66
66
if let Some ( self_selection_set) = self . get_selection_set_mut ( ) {
@@ -87,22 +87,16 @@ impl<'a> InlineFragmentSelectionValue<'a> {
87
87
let mut selection_sets = vec ! [ ] ;
88
88
for other in others {
89
89
let other_inline_fragment = & other. inline_fragment ;
90
- if other_inline_fragment. schema != self_inline_fragment. schema {
91
- return Err ( FederationError :: internal (
92
- "Cannot merge inline fragment from different schemas" ,
93
- ) ) ;
94
- }
95
- if other_inline_fragment. parent_type_position
96
- != self_inline_fragment. parent_type_position
97
- {
98
- return Err ( FederationError :: internal (
99
- format ! (
100
- "Cannot merge inline fragment of parent type \" {}\" into an inline fragment of parent type \" {}\" " ,
101
- other_inline_fragment. parent_type_position,
102
- self_inline_fragment. parent_type_position,
103
- ) ,
104
- ) ) ;
105
- }
90
+ ensure ! (
91
+ other_inline_fragment. schema == self_inline_fragment. schema,
92
+ "Cannot merge inline fragment from different schemas" ,
93
+ ) ;
94
+ ensure ! (
95
+ other_inline_fragment. parent_type_position == self_inline_fragment. parent_type_position,
96
+ "Cannot merge inline fragment of parent type \" {}\" into an inline fragment of parent type \" {}\" " ,
97
+ other_inline_fragment. parent_type_position,
98
+ self_inline_fragment. parent_type_position,
99
+ ) ;
106
100
selection_sets. push ( & other. selection_set ) ;
107
101
}
108
102
self . get_selection_set_mut ( )
@@ -127,11 +121,10 @@ impl<'a> FragmentSpreadSelectionValue<'a> {
127
121
let self_fragment_spread = & self . get ( ) . spread ;
128
122
for other in others {
129
123
let other_fragment_spread = & other. spread ;
130
- if other_fragment_spread. schema != self_fragment_spread. schema {
131
- return Err ( FederationError :: internal (
132
- "Cannot merge fragment spread from different schemas" ,
133
- ) ) ;
134
- }
124
+ ensure ! (
125
+ other_fragment_spread. schema == self_fragment_spread. schema,
126
+ "Cannot merge fragment spread from different schemas" ,
127
+ ) ;
135
128
// Nothing to do since the fragment spread is already part of the selection set.
136
129
// Fragment spreads are uniquely identified by fragment name and applied directives.
137
130
// Since there is already an entry for the same fragment spread, there is no point
@@ -157,20 +150,16 @@ impl SelectionSet {
157
150
) -> Result < ( ) , FederationError > {
158
151
let mut selections_to_merge = vec ! [ ] ;
159
152
for other in others {
160
- if other. schema != self . schema {
161
- return Err ( FederationError :: internal (
162
- "Cannot merge selection sets from different schemas" ,
163
- ) ) ;
164
- }
165
- if other. type_position != self . type_position {
166
- return Err ( FederationError :: internal (
167
- format ! (
168
- "Cannot merge selection set for type \" {}\" into a selection set for type \" {}\" " ,
169
- other. type_position,
170
- self . type_position,
171
- ) ,
172
- ) ) ;
173
- }
153
+ ensure ! (
154
+ other. schema == self . schema,
155
+ "Cannot merge selection sets from different schemas" ,
156
+ ) ;
157
+ ensure ! (
158
+ other. type_position == self . type_position,
159
+ "Cannot merge selection set for type \" {}\" into a selection set for type \" {}\" " ,
160
+ other. type_position,
161
+ self . type_position,
162
+ ) ;
174
163
selections_to_merge. extend ( other. selections . values ( ) ) ;
175
164
}
176
165
self . merge_selections_into ( selections_to_merge. into_iter ( ) )
@@ -198,12 +187,10 @@ impl SelectionSet {
198
187
selection_map:: Entry :: Occupied ( existing) => match existing. get ( ) {
199
188
Selection :: Field ( self_field_selection) => {
200
189
let Selection :: Field ( other_field_selection) = other_selection else {
201
- return Err ( FederationError :: internal (
202
- format ! (
203
- "Field selection key for field \" {}\" references non-field selection" ,
204
- self_field_selection. field. field_position,
205
- ) ,
206
- ) ) ;
190
+ internal_error ! (
191
+ "Field selection key for field \" {}\" references non-field selection" ,
192
+ self_field_selection. field. field_position,
193
+ ) ;
207
194
} ;
208
195
fields
209
196
. entry ( other_key)
@@ -214,12 +201,10 @@ impl SelectionSet {
214
201
let Selection :: FragmentSpread ( other_fragment_spread_selection) =
215
202
other_selection
216
203
else {
217
- return Err ( FederationError :: internal (
218
- format ! (
219
- "Fragment spread selection key for fragment \" {}\" references non-field selection" ,
220
- self_fragment_spread_selection. spread. fragment_name,
221
- ) ,
222
- ) ) ;
204
+ internal_error ! (
205
+ "Fragment spread selection key for fragment \" {}\" references non-field selection" ,
206
+ self_fragment_spread_selection. spread. fragment_name,
207
+ ) ;
223
208
} ;
224
209
fragment_spreads
225
210
. entry ( other_key)
@@ -230,17 +215,15 @@ impl SelectionSet {
230
215
let Selection :: InlineFragment ( other_inline_fragment_selection) =
231
216
other_selection
232
217
else {
233
- return Err ( FederationError :: internal (
234
- format ! (
235
- "Inline fragment selection key under parent type \" {}\" {}references non-field selection" ,
236
- self_inline_fragment_selection. inline_fragment. parent_type_position,
237
- self_inline_fragment_selection. inline_fragment. type_condition_position. clone( )
238
- . map_or_else(
239
- String :: new,
240
- |cond| format!( "(type condition: {}) " , cond) ,
241
- ) ,
242
- ) ,
243
- ) ) ;
218
+ internal_error ! (
219
+ "Inline fragment selection key under parent type \" {}\" {}references non-field selection" ,
220
+ self_inline_fragment_selection. inline_fragment. parent_type_position,
221
+ self_inline_fragment_selection. inline_fragment. type_condition_position. clone( )
222
+ . map_or_else(
223
+ String :: new,
224
+ |cond| format!( "(type condition: {}) " , cond) ,
225
+ ) ,
226
+ ) ;
244
227
} ;
245
228
inline_fragments
246
229
. entry ( other_key)
@@ -306,9 +289,8 @@ impl SelectionSet {
306
289
& mut self ,
307
290
selection : & Selection ,
308
291
) -> Result < ( ) , FederationError > {
309
- debug_assert_eq ! (
310
- & self . schema,
311
- selection. schema( ) ,
292
+ ensure ! (
293
+ self . schema == * selection. schema( ) ,
312
294
"In order to add selection it needs to point to the same schema"
313
295
) ;
314
296
self . merge_selections_into ( std:: iter:: once ( selection) )
@@ -328,12 +310,12 @@ impl SelectionSet {
328
310
& mut self ,
329
311
selection_set : & SelectionSet ,
330
312
) -> Result < ( ) , FederationError > {
331
- debug_assert_eq ! (
332
- self . schema, selection_set. schema,
313
+ ensure ! (
314
+ self . schema == selection_set. schema,
333
315
"In order to add selection set it needs to point to the same schema."
334
316
) ;
335
- debug_assert_eq ! (
336
- self . type_position, selection_set. type_position,
317
+ ensure ! (
318
+ self . type_position == selection_set. type_position,
337
319
"In order to add selection set it needs to point to the same type position"
338
320
) ;
339
321
self . merge_into ( std:: iter:: once ( selection_set) )
@@ -386,9 +368,7 @@ pub(crate) fn merge_selection_sets(
386
368
mut selection_sets : Vec < SelectionSet > ,
387
369
) -> Result < SelectionSet , FederationError > {
388
370
let Some ( ( first, remainder) ) = selection_sets. split_first_mut ( ) else {
389
- return Err ( FederationError :: internal (
390
- "merge_selection_sets(): must have at least one selection set" ,
391
- ) ) ;
371
+ internal_error ! ( "merge_selection_sets(): must have at least one selection set" ) ;
392
372
} ;
393
373
first. merge_into ( remainder. iter ( ) ) ?;
394
374
0 commit comments