Skip to content

Commit 1e9c63e

Browse files
committed
Add simple scalar introspection tests
1 parent f9383a2 commit 1e9c63e

File tree

2 files changed

+78
-0
lines changed

2 files changed

+78
-0
lines changed

src/ast.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,14 @@ impl InputValue {
329329
}
330330
}
331331

332+
/// View the underlying int value, if present.
333+
pub fn as_int_value(&self) -> Option<i64> {
334+
match *self {
335+
InputValue::Int(i) => Some(i),
336+
_ => None,
337+
}
338+
}
339+
332340
/// View the underlying string value, if present.
333341
pub fn as_string_value(&self) -> Option<&str> {
334342
match *self {

src/executor_tests/introspection.rs

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,22 @@ enum Sample {
99
Two,
1010
}
1111

12+
struct Scalar(i64);
13+
1214
struct Interface {}
1315

1416
struct Root {}
1517

18+
graphql_scalar!(Scalar as "SampleScalar" {
19+
resolve(&self) -> Value {
20+
Value::int(self.0)
21+
}
22+
23+
from_input_value(v: &InputValue) -> Option<Scalar> {
24+
v.as_int_value().map(|i| Scalar(i))
25+
}
26+
});
27+
1628
graphql_enum!(Sample as "SampleEnum" {
1729
Sample::One => "ONE",
1830
Sample::Two => "TWO",
@@ -36,8 +48,35 @@ graphql_object!(Root: () as "Root" |&self| {
3648
field sample_enum() -> FieldResult<Sample> {
3749
Ok(Sample::One)
3850
}
51+
52+
field sample_scalar() -> FieldResult<Scalar> {
53+
Ok(Scalar(123))
54+
}
3955
});
4056

57+
#[test]
58+
fn test_execution() {
59+
let doc = r#"
60+
{
61+
sampleEnum
62+
sampleScalar
63+
}
64+
"#;
65+
let schema = RootNode::new(Root {}, ());
66+
67+
let (result, errs) = ::execute(doc, None, &schema, &HashMap::new(), &())
68+
.expect("Execution failed");
69+
70+
assert_eq!(errs, []);
71+
72+
println!("Result: {:?}", result);
73+
74+
assert_eq!(result, Value::object(vec![
75+
("sampleEnum", Value::string("ONE")),
76+
("sampleScalar", Value::int(123)),
77+
].into_iter().collect()));
78+
}
79+
4180
#[test]
4281
fn enum_introspection() {
4382
let doc = r#"
@@ -175,3 +214,34 @@ fn interface_introspection() {
175214
("deprecationReason", Value::null()),
176215
].into_iter().collect())));
177216
}
217+
218+
#[test]
219+
fn scalar_introspection() {
220+
let doc = r#"
221+
{
222+
__type(name: "SampleScalar") {
223+
name
224+
kind
225+
description
226+
}
227+
}
228+
"#;
229+
let schema = RootNode::new(Root {}, ());
230+
231+
let (result, errs) = ::execute(doc, None, &schema, &HashMap::new(), &())
232+
.expect("Execution failed");
233+
234+
assert_eq!(errs, []);
235+
236+
println!("Result: {:?}", result);
237+
238+
let type_info = result
239+
.as_object_value().expect("Result is not an object")
240+
.get("__type").expect("__type field missing");
241+
242+
assert_eq!(type_info, &Value::object(vec![
243+
("name", Value::string("SampleScalar")),
244+
("kind", Value::string("SCALAR")),
245+
("description", Value::null()),
246+
].into_iter().collect()));
247+
}

0 commit comments

Comments
 (0)