@@ -11,100 +11,90 @@ use crate::response::graphql_error::{GraphQLError, GraphQLErrorPathSegment};
11
11
* and flatten path is ['product', 'reviews', 0, 'author']
12
12
* it becomes `["product", "reviews", "0", "author", "name"]`.
13
13
*/
14
- pub fn normalize_errors_for_representations (
14
+ pub fn normalize_error_for_representation (
15
+ error : & GraphQLError ,
15
16
subgraph_name : & str ,
16
17
normalized_path : & [ FlattenNodePathSegment ] ,
17
18
representation_hashes : & [ u64 ] ,
18
19
hashes_to_indexes : & HashMap < u64 , Vec < VecDeque < usize > > > ,
19
- errors : & Vec < GraphQLError > ,
20
20
) -> Vec < GraphQLError > {
21
21
let mut new_errors: Vec < GraphQLError > = Vec :: new ( ) ;
22
- ' error_loop: for error in errors {
23
- if let Some ( path_in_error) = & error. path {
24
- if let Some ( GraphQLErrorPathSegment :: String ( first_path) ) = path_in_error. first ( ) {
25
- if first_path == "_entities" {
26
- if let Some ( GraphQLErrorPathSegment :: Index ( entity_index) ) = path_in_error. get ( 1 )
27
- {
28
- if let Some ( representation_hash) = representation_hashes. get ( * entity_index)
29
- {
30
- if let Some ( indexes_in_paths) =
31
- hashes_to_indexes. get ( representation_hash)
32
- {
33
- for indexes_in_path in indexes_in_paths {
34
- let mut indexes_in_path = indexes_in_path. clone ( ) ;
35
- let mut real_path: Vec < GraphQLErrorPathSegment > =
36
- Vec :: with_capacity (
37
- normalized_path. len ( ) + path_in_error. len ( ) - 2 ,
38
- ) ;
39
- for segment in normalized_path {
40
- match segment {
41
- FlattenNodePathSegment :: Field ( field_name) => {
42
- real_path. push ( GraphQLErrorPathSegment :: String (
43
- field_name. to_string ( ) ,
22
+ if let Some ( path_in_error) = & error. path {
23
+ if let Some ( GraphQLErrorPathSegment :: String ( first_path) ) = path_in_error. first ( ) {
24
+ if first_path == "_entities" {
25
+ if let Some ( GraphQLErrorPathSegment :: Index ( entity_index) ) = path_in_error. get ( 1 ) {
26
+ if let Some ( representation_hash) = representation_hashes. get ( * entity_index) {
27
+ if let Some ( indexes_in_paths) = hashes_to_indexes. get ( representation_hash) {
28
+ for indexes_in_path in indexes_in_paths {
29
+ let mut indexes_in_path = indexes_in_path. clone ( ) ;
30
+ let mut real_path: Vec < GraphQLErrorPathSegment > =
31
+ Vec :: with_capacity (
32
+ normalized_path. len ( ) + path_in_error. len ( ) - 2 ,
33
+ ) ;
34
+ for segment in normalized_path {
35
+ match segment {
36
+ FlattenNodePathSegment :: Field ( field_name) => {
37
+ real_path. push ( GraphQLErrorPathSegment :: String (
38
+ field_name. to_string ( ) ,
39
+ ) ) ;
40
+ }
41
+ FlattenNodePathSegment :: List => {
42
+ if let Some ( index_in_path) = indexes_in_path. pop_front ( )
43
+ {
44
+ real_path. push ( GraphQLErrorPathSegment :: Index (
45
+ index_in_path,
44
46
) ) ;
45
47
}
46
- FlattenNodePathSegment :: List => {
47
- if let Some ( index_in_path) =
48
- indexes_in_path. pop_front ( )
49
- {
50
- real_path. push ( GraphQLErrorPathSegment :: Index (
51
- index_in_path,
52
- ) ) ;
53
- }
54
- }
55
- FlattenNodePathSegment :: Cast ( _type_condition) => {
56
- // Cast segments are not included in the error path
57
- continue ;
58
- }
59
48
}
60
- }
61
- if !indexes_in_path. is_empty ( ) {
62
- // If there are still indexes left, we need to traverse them
63
- while let Some ( index) = indexes_in_path. pop_front ( ) {
64
- real_path. push ( GraphQLErrorPathSegment :: Index ( index) ) ;
49
+ FlattenNodePathSegment :: Cast ( _type_condition) => {
50
+ // Cast segments are not included in the error path
51
+ continue ;
65
52
}
66
53
}
67
- real_path. extend_from_slice ( & path_in_error[ 2 ..] ) ;
68
- let mut new_error = error. clone ( ) ;
69
- if !real_path. is_empty ( ) {
70
- new_error. path = Some ( real_path) ;
54
+ }
55
+ if !indexes_in_path. is_empty ( ) {
56
+ // If there are still indexes left, we need to traverse them
57
+ while let Some ( index) = indexes_in_path. pop_front ( ) {
58
+ real_path. push ( GraphQLErrorPathSegment :: Index ( index) ) ;
71
59
}
72
- new_error =
73
- add_subgraph_info_to_error ( new_error, subgraph_name) ;
74
- new_errors. push ( new_error) ;
75
60
}
76
- continue ' error_loop;
61
+ real_path. extend_from_slice ( & path_in_error[ 2 ..] ) ;
62
+ let mut new_error = error. clone ( ) ;
63
+ if !real_path. is_empty ( ) {
64
+ new_error. path = Some ( real_path) ;
65
+ }
66
+ new_error = add_subgraph_info_to_error ( new_error, subgraph_name) ;
67
+ new_errors. push ( new_error) ;
77
68
}
69
+ return new_errors;
78
70
}
79
71
}
80
72
}
81
73
}
82
74
}
83
- // Use the path without indexes in case of unlocated error
84
- let mut real_path : Vec < GraphQLErrorPathSegment > = Vec :: with_capacity ( normalized_path . len ( ) ) ;
85
- for segment in normalized_path {
86
- match segment {
87
- FlattenNodePathSegment :: Field ( field_name ) => {
88
- real_path . push ( GraphQLErrorPathSegment :: String ( field_name. to_string ( ) ) ) ;
89
- }
90
- FlattenNodePathSegment :: List => {
91
- break ;
92
- }
93
- FlattenNodePathSegment :: Cast ( _type_condition ) => {
94
- // Cast segments are not included in the error path
95
- continue ;
96
- }
75
+ }
76
+ // Use the path without indexes in case of unlocated error
77
+ let mut real_path : Vec < GraphQLErrorPathSegment > = Vec :: with_capacity ( normalized_path. len ( ) ) ;
78
+ for segment in normalized_path {
79
+ match segment {
80
+ FlattenNodePathSegment :: Field ( field_name) => {
81
+ real_path . push ( GraphQLErrorPathSegment :: String ( field_name . to_string ( ) ) ) ;
82
+ }
83
+ FlattenNodePathSegment :: List => {
84
+ break ;
85
+ }
86
+ FlattenNodePathSegment :: Cast ( _type_condition ) => {
87
+ // Cast segments are not included in the error path
88
+ continue ;
97
89
}
98
90
}
99
- let mut new_error = error. clone ( ) ;
100
- if !real_path. is_empty ( ) {
101
- new_error. path = Some ( real_path) ;
102
- }
103
- new_error = add_subgraph_info_to_error ( new_error, subgraph_name) ;
104
-
105
- new_errors. push ( new_error) ;
106
91
}
107
-
92
+ let mut new_error = error. clone ( ) ;
93
+ if !real_path. is_empty ( ) {
94
+ new_error. path = Some ( real_path) ;
95
+ }
96
+ new_error = add_subgraph_info_to_error ( new_error, subgraph_name) ;
97
+ new_errors. push ( new_error) ;
108
98
new_errors
109
99
}
110
100
@@ -158,13 +148,17 @@ fn test_normalize_errors_for_representations() {
158
148
extensions: None ,
159
149
} ,
160
150
] ;
161
- let normalized_errors = normalize_errors_for_representations (
162
- "products" ,
163
- & normalized_path,
164
- & representation_hashes,
165
- & indexes_in_paths,
166
- & errors,
167
- ) ;
151
+ let mut normalized_errors = Vec :: new ( ) ;
152
+ for error in & errors {
153
+ let normalized_error = normalize_error_for_representation (
154
+ error,
155
+ "products" ,
156
+ & normalized_path,
157
+ & representation_hashes,
158
+ & indexes_in_paths,
159
+ ) ;
160
+ normalized_errors. extend ( normalized_error) ;
161
+ }
168
162
println ! ( "{:?}" , normalized_errors) ;
169
163
assert_eq ! ( normalized_errors. len( ) , 2 ) ;
170
164
assert_eq ! (
0 commit comments