Skip to content

Commit 471d1f5

Browse files
authored
Merge pull request #67 from tomhoule/error-extensions
Add minimal support for the extensions field on errors
2 parents 5436b3c + f2be853 commit 471d1f5

File tree

3 files changed

+59
-3
lines changed

3 files changed

+59
-3
lines changed

CHANGELOG.md

+9
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,15 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
77

88
## Unreleased
99

10+
### Added
11+
12+
- Implemented support for the `extensions` field on errors from the June 2018 spec (#64).
13+
14+
15+
### Changed
16+
17+
- `serde_json` is now a dependency, because the `extensions` field on errors can be contain arbitrary JSON.
18+
1019
## [0.2.0] - 2018-07-22
1120

1221
### Added

Cargo.toml

-2
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@ graphql_query_derive = {path = "./graphql_query_derive", version = "0.2.0"}
1515
graphql-parser = "0.2.0"
1616
serde = "1.0"
1717
serde_derive = "1.0"
18-
19-
[dev-dependencies]
2018
serde_json = "1.0"
2119

2220
[workspace]

src/lib.rs

+50-1
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,14 @@ extern crate serde_derive;
1010
#[macro_use]
1111
extern crate graphql_query_derive;
1212

13-
#[cfg(test)]
1413
#[macro_use]
1514
extern crate serde_json;
1615

1716
#[doc(hidden)]
1817
pub 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

Comments
 (0)