Add simple scalar introspection tests
This commit is contained in:
parent
f9383a2a39
commit
1e9c63e085
2 changed files with 78 additions and 0 deletions
|
@ -329,6 +329,14 @@ impl InputValue {
|
|||
}
|
||||
}
|
||||
|
||||
/// View the underlying int value, if present.
|
||||
pub fn as_int_value(&self) -> Option<i64> {
|
||||
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 {
|
||||
|
|
|
@ -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<Scalar> {
|
||||
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<Sample> {
|
||||
Ok(Sample::One)
|
||||
}
|
||||
|
||||
field sample_scalar() -> FieldResult<Scalar> {
|
||||
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()));
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue