Skip to content

Add minimal support for the extensions field on errors #67

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 24, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,15 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.

## Unreleased

### Added

- Implemented support for the `extensions` field on errors from the June 2018 spec (#64).


### Changed

- `serde_json` is now a dependency, because the `extensions` field on errors can be contain arbitrary JSON.

## [0.2.0] - 2018-07-22

### Added
Expand Down
2 changes: 0 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ graphql_query_derive = {path = "./graphql_query_derive", version = "0.2.0"}
graphql-parser = "0.2.0"
serde = "1.0"
serde_derive = "1.0"

[dev-dependencies]
serde_json = "1.0"

[workspace]
Expand Down
51 changes: 50 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,14 @@ extern crate serde_derive;
#[macro_use]
extern crate graphql_query_derive;

#[cfg(test)]
#[macro_use]
extern crate serde_json;

#[doc(hidden)]
pub use graphql_query_derive::*;

use std::collections::HashMap;

/// A convenience trait that can be used to build a GraphQL request body.
///
/// This will be implemented for you by codegen in the normal case.
Expand Down Expand Up @@ -72,6 +73,8 @@ pub struct GraphQLError {
pub locations: Option<Vec<Location>>,
/// Which path in the query the error applies to, e.g. `["users", 0, "email"]`.
pub path: Option<Vec<PathFragment>>,
/// Additional errors. Their exact format is defined by the server.
pub extensions: Option<HashMap<String, serde_json::Value>>,
}

/// The generic shape taken by the responses of GraphQL APIs.
Expand Down Expand Up @@ -105,6 +108,7 @@ mod tests {
message: "I accidentally your whole query".to_string(),
locations: None,
path: None,
extensions: None,
}
)
}
Expand Down Expand Up @@ -139,6 +143,51 @@ mod tests {
PathFragment::Index(3),
PathFragment::Key("rating".to_owned()),
]),
extensions: None,
}
)
}

#[test]
fn full_graphql_error_with_extensions_deserialization() {
let err = json!({
"message": "I accidentally your whole query",
"locations": [{ "line": 3, "column": 13}, {"line": 56, "column": 1}],
"path": ["home", "alone", 3, "rating"],
"extensions": {
"code": "CAN_NOT_FETCH_BY_ID",
"timestamp": "Fri Feb 9 14:33:09 UTC 2018"
}
});

let deserialized_error: GraphQLError = serde_json::from_value(err).unwrap();

let mut expected_extensions = HashMap::new();
expected_extensions.insert("code".to_owned(), json!("CAN_NOT_FETCH_BY_ID"));
expected_extensions.insert("timestamp".to_owned(), json!("Fri Feb 9 14:33:09 UTC 2018"));
let expected_extensions = Some(expected_extensions);

assert_eq!(
deserialized_error,
GraphQLError {
message: "I accidentally your whole query".to_string(),
locations: Some(vec![
Location {
line: 3,
column: 13,
},
Location {
line: 56,
column: 1,
},
]),
path: Some(vec![
PathFragment::Key("home".to_owned()),
PathFragment::Key("alone".to_owned()),
PathFragment::Index(3),
PathFragment::Key("rating".to_owned()),
]),
extensions: expected_extensions,
}
)
}
Expand Down