From 1e9c63e085b5d28e82e062cce18689b5ea805fd2 Mon Sep 17 00:00:00 2001 From: Magnus Hallin Date: Tue, 20 Sep 2016 22:31:02 +0200 Subject: [PATCH] Add simple scalar introspection tests --- src/ast.rs | 8 ++++ src/executor_tests/introspection.rs | 70 +++++++++++++++++++++++++++++ 2 files changed, 78 insertions(+) diff --git a/src/ast.rs b/src/ast.rs index e681e40e..7c83033f 100644 --- a/src/ast.rs +++ b/src/ast.rs @@ -329,6 +329,14 @@ impl InputValue { } } + /// View the underlying int value, if present. + pub fn as_int_value(&self) -> Option { + match *self { + InputValue::Int(i) => Some(i), + _ => None, + } + } + /// View the underlying string value, if present. pub fn as_string_value(&self) -> Option<&str> { match *self { diff --git a/src/executor_tests/introspection.rs b/src/executor_tests/introspection.rs index ea271bcd..b1355ba6 100644 --- a/src/executor_tests/introspection.rs +++ b/src/executor_tests/introspection.rs @@ -9,10 +9,22 @@ enum Sample { Two, } +struct Scalar(i64); + struct Interface {} struct Root {} +graphql_scalar!(Scalar as "SampleScalar" { + resolve(&self) -> Value { + Value::int(self.0) + } + + from_input_value(v: &InputValue) -> Option { + v.as_int_value().map(|i| Scalar(i)) + } +}); + graphql_enum!(Sample as "SampleEnum" { Sample::One => "ONE", Sample::Two => "TWO", @@ -36,8 +48,35 @@ graphql_object!(Root: () as "Root" |&self| { field sample_enum() -> FieldResult { Ok(Sample::One) } + + field sample_scalar() -> FieldResult { + Ok(Scalar(123)) + } }); +#[test] +fn test_execution() { + let doc = r#" + { + sampleEnum + sampleScalar + } + "#; + let schema = RootNode::new(Root {}, ()); + + let (result, errs) = ::execute(doc, None, &schema, &HashMap::new(), &()) + .expect("Execution failed"); + + assert_eq!(errs, []); + + println!("Result: {:?}", result); + + assert_eq!(result, Value::object(vec![ + ("sampleEnum", Value::string("ONE")), + ("sampleScalar", Value::int(123)), + ].into_iter().collect())); +} + #[test] fn enum_introspection() { let doc = r#" @@ -175,3 +214,34 @@ fn interface_introspection() { ("deprecationReason", Value::null()), ].into_iter().collect()))); } + +#[test] +fn scalar_introspection() { + let doc = r#" + { + __type(name: "SampleScalar") { + name + kind + description + } + } + "#; + let schema = RootNode::new(Root {}, ()); + + let (result, errs) = ::execute(doc, None, &schema, &HashMap::new(), &()) + .expect("Execution failed"); + + assert_eq!(errs, []); + + println!("Result: {:?}", result); + + let type_info = result + .as_object_value().expect("Result is not an object") + .get("__type").expect("__type field missing"); + + assert_eq!(type_info, &Value::object(vec![ + ("name", Value::string("SampleScalar")), + ("kind", Value::string("SCALAR")), + ("description", Value::null()), + ].into_iter().collect())); +}