@@ -10,13 +10,14 @@ extern crate serde_derive;
1010#[ macro_use]
1111extern crate graphql_query_derive;
1212
13- #[ cfg( test) ]
1413#[ macro_use]
1514extern crate serde_json;
1615
1716#[ doc( hidden) ]
1817pub use graphql_query_derive:: * ;
1918
19+ use std:: collections:: HashMap ;
20+
2021/// A convenience trait that can be used to build a GraphQL request body.
2122///
2223/// This will be implemented for you by codegen in the normal case.
@@ -72,6 +73,8 @@ pub struct GraphQLError {
7273 pub locations : Option < Vec < Location > > ,
7374 /// Which path in the query the error applies to, e.g. `["users", 0, "email"]`.
7475 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 > > ,
7578}
7679
7780/// The generic shape taken by the responses of GraphQL APIs.
@@ -105,6 +108,7 @@ mod tests {
105108 message: "I accidentally your whole query" . to_string( ) ,
106109 locations: None ,
107110 path: None ,
111+ extensions: None ,
108112 }
109113 )
110114 }
@@ -139,6 +143,51 @@ mod tests {
139143 PathFragment :: Index ( 3 ) ,
140144 PathFragment :: Key ( "rating" . to_owned( ) ) ,
141145 ] ) ,
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,
142191 }
143192 )
144193 }
0 commit comments