@@ -10,13 +10,14 @@ extern crate serde_derive;
10
10
#[ macro_use]
11
11
extern crate graphql_query_derive;
12
12
13
- #[ cfg( test) ]
14
13
#[ macro_use]
15
14
extern crate serde_json;
16
15
17
16
#[ doc( hidden) ]
18
17
pub use graphql_query_derive:: * ;
19
18
19
+ use std:: collections:: HashMap ;
20
+
20
21
/// A convenience trait that can be used to build a GraphQL request body.
21
22
///
22
23
/// This will be implemented for you by codegen in the normal case.
@@ -72,6 +73,8 @@ pub struct GraphQLError {
72
73
pub locations : Option < Vec < Location > > ,
73
74
/// Which path in the query the error applies to, e.g. `["users", 0, "email"]`.
74
75
pub path : Option < Vec < PathFragment > > ,
76
+ /// Additional errors. Their exact format is defined by the server.
77
+ pub extensions : Option < HashMap < String , serde_json:: Value > > ,
75
78
}
76
79
77
80
/// The generic shape taken by the responses of GraphQL APIs.
@@ -105,6 +108,7 @@ mod tests {
105
108
message: "I accidentally your whole query" . to_string( ) ,
106
109
locations: None ,
107
110
path: None ,
111
+ extensions: None ,
108
112
}
109
113
)
110
114
}
@@ -139,6 +143,51 @@ mod tests {
139
143
PathFragment :: Index ( 3 ) ,
140
144
PathFragment :: Key ( "rating" . to_owned( ) ) ,
141
145
] ) ,
146
+ extensions: None ,
147
+ }
148
+ )
149
+ }
150
+
151
+ #[ test]
152
+ fn full_graphql_error_with_extensions_deserialization ( ) {
153
+ let err = json ! ( {
154
+ "message" : "I accidentally your whole query" ,
155
+ "locations" : [ { "line" : 3 , "column" : 13 } , { "line" : 56 , "column" : 1 } ] ,
156
+ "path" : [ "home" , "alone" , 3 , "rating" ] ,
157
+ "extensions" : {
158
+ "code" : "CAN_NOT_FETCH_BY_ID" ,
159
+ "timestamp" : "Fri Feb 9 14:33:09 UTC 2018"
160
+ }
161
+ } ) ;
162
+
163
+ let deserialized_error: GraphQLError = serde_json:: from_value ( err) . unwrap ( ) ;
164
+
165
+ let mut expected_extensions = HashMap :: new ( ) ;
166
+ expected_extensions. insert ( "code" . to_owned ( ) , json ! ( "CAN_NOT_FETCH_BY_ID" ) ) ;
167
+ expected_extensions. insert ( "timestamp" . to_owned ( ) , json ! ( "Fri Feb 9 14:33:09 UTC 2018" ) ) ;
168
+ let expected_extensions = Some ( expected_extensions) ;
169
+
170
+ assert_eq ! (
171
+ deserialized_error,
172
+ GraphQLError {
173
+ message: "I accidentally your whole query" . to_string( ) ,
174
+ locations: Some ( vec![
175
+ Location {
176
+ line: 3 ,
177
+ column: 13 ,
178
+ } ,
179
+ Location {
180
+ line: 56 ,
181
+ column: 1 ,
182
+ } ,
183
+ ] ) ,
184
+ path: Some ( vec![
185
+ PathFragment :: Key ( "home" . to_owned( ) ) ,
186
+ PathFragment :: Key ( "alone" . to_owned( ) ) ,
187
+ PathFragment :: Index ( 3 ) ,
188
+ PathFragment :: Key ( "rating" . to_owned( ) ) ,
189
+ ] ) ,
190
+ extensions: expected_extensions,
142
191
}
143
192
)
144
193
}
0 commit comments