-
Notifications
You must be signed in to change notification settings - Fork 431
/
Copy pathjson.rs
106 lines (87 loc) · 2.58 KB
/
json.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
use serde_json::Value as JsonValue;
use parser::{ParseError, ScalarToken, Token};
use value::ParseScalarResult;
use Value;
graphql_scalar!(JsonValue as "JsonString" where Scalar = <S> {
description: "JSON serialized as a string"
resolve(&self) -> Value {
Value::scalar(self.to_string())
}
from_input_value(v: &InputValue) -> Option<JsonValue> {
v.as_scalar_value::<String>()
.and_then(|s| serde_json::from_str(s).ok())
}
from_str<'a>(value: ScalarToken<'a>) -> ParseScalarResult<'a, S> {
if let ScalarToken::String(value) = value {
Ok(S::from(value.to_owned()))
} else {
Err(ParseError::UnexpectedToken(Token::Scalar(value)))
}
}
});
#[cfg(test)]
mod test {
use super::*;
#[test]
fn json_from_input_value() {
let raw = r#"{ "foo": "bar"}"#;
let input: ::InputValue = ::InputValue::scalar(raw.to_string());
let parsed: JsonValue = ::FromInputValue::from_input_value(&input).unwrap();
let expected: JsonValue = serde_json::from_str(raw).unwrap();
assert_eq!(parsed, expected);
}
}
#[cfg(test)]
mod integration_test {
use super::*;
use executor::Variables;
use schema::model::RootNode;
use types::scalars::EmptyMutation;
use value::Value;
#[test]
fn test_json_serialization() {
let example_raw: JsonValue = serde_json::from_str(
r#"{
"x": 2,
"y": 42
}
"#,
)
.unwrap();
let example_raw = example_raw.to_string();
struct Root;
graphql_object!(Root: () |&self| {
field example_json() -> JsonValue {
serde_json::from_str(r#"{
"x": 2,
"y": 42
}
"#).unwrap()
}
field input_json(input: JsonValue) -> bool {
input.is_array()
}
});
let doc = r#"
{
exampleJson,
inputJson(input: "[]"),
}
"#;
let schema = RootNode::new(Root, EmptyMutation::<()>::new());
let (result, errs) =
::execute(doc, None, &schema, &Variables::new(), &()).expect("Execution failed");
assert_eq!(errs, []);
assert_eq!(
result,
Value::object(
vec![
("exampleJson", Value::scalar(example_raw)),
("inputJson", Value::scalar(true)),
]
.into_iter()
.collect()
)
);
}
}