From f9383a2a39aa440633a433843fa02e2bbca1b98c Mon Sep 17 00:00:00 2001 From: Magnus Hallin Date: Tue, 20 Sep 2016 21:50:53 +0200 Subject: [PATCH] Add basic interface introspection tests --- src/executor_tests/introspection.rs | 111 +++++++++++++++++++++++++++- 1 file changed, 109 insertions(+), 2 deletions(-) diff --git a/src/executor_tests/introspection.rs b/src/executor_tests/introspection.rs index c9c11cd8..ea271bcd 100644 --- a/src/executor_tests/introspection.rs +++ b/src/executor_tests/introspection.rs @@ -9,6 +9,8 @@ enum Sample { Two, } +struct Interface {} + struct Root {} graphql_enum!(Sample as "SampleEnum" { @@ -16,7 +18,21 @@ graphql_enum!(Sample as "SampleEnum" { Sample::Two => "TWO", }); +graphql_interface!(Interface: () as "SampleInterface" |&self| { + description: "A sample interface" + + field sample_enum() -> FieldResult as "A sample field in the interface" { + Ok(Sample::One) + } + + instance_resolvers: |&_| [ + Some(Root {}), + ] +}); + graphql_object!(Root: () as "Root" |&self| { + interfaces: [Interface] + field sample_enum() -> FieldResult { Ok(Sample::One) } @@ -27,6 +43,9 @@ fn enum_introspection() { let doc = r#" { __type(name: "SampleEnum") { + name + kind + description enumValues { name description @@ -45,10 +64,16 @@ fn enum_introspection() { println!("Result: {:?}", result); - let values = result + let type_info = result .as_object_value().expect("Result is not an object") .get("__type").expect("__type field missing") - .as_object_value().expect("__type field not an object value") + .as_object_value().expect("__type field not an object value"); + + assert_eq!(type_info.get("name"), Some(&Value::string("SampleEnum"))); + assert_eq!(type_info.get("kind"), Some(&Value::string("ENUM"))); + assert_eq!(type_info.get("description"), Some(&Value::null())); + + let values = type_info .get("enumValues").expect("enumValues field missing") .as_list_value().expect("enumValues not a list"); @@ -68,3 +93,85 @@ fn enum_introspection() { ("deprecationReason", Value::null()), ].into_iter().collect()))); } + +#[test] +fn interface_introspection() { + let doc = r#" + { + __type(name: "SampleInterface") { + name + kind + description + possibleTypes { + name + } + fields { + name + description + args { + name + } + type { + name + kind + ofType { + name + kind + } + } + isDeprecated + deprecationReason + } + } + } + "#; + 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") + .as_object_value().expect("__type field not an object value"); + + assert_eq!(type_info.get("name"), Some(&Value::string("SampleInterface"))); + assert_eq!(type_info.get("kind"), Some(&Value::string("INTERFACE"))); + assert_eq!(type_info.get("description"), Some(&Value::string("A sample interface"))); + + let possible_types = type_info + .get("possibleTypes").expect("possibleTypes field missing") + .as_list_value().expect("possibleTypes not a list"); + + assert_eq!(possible_types.len(), 1); + + assert!(possible_types.contains(&Value::object(vec![ + ("name", Value::string("Root")), + ].into_iter().collect()))); + + let fields = type_info + .get("fields").expect("fields field missing") + .as_list_value().expect("fields field not an object value"); + + assert_eq!(fields.len(), 2); + + assert!(fields.contains(&Value::object(vec![ + ("name", Value::string("sampleEnum")), + ("description", Value::string("A sample field in the interface")), + ("args", Value::list(vec![])), + ("type", Value::object(vec![ + ("name", Value::null()), + ("kind", Value::string("NON_NULL")), + ("ofType", Value::object(vec![ + ("name", Value::string("SampleEnum")), + ("kind", Value::string("ENUM")), + ].into_iter().collect())), + ].into_iter().collect())), + ("isDeprecated", Value::boolean(false)), + ("deprecationReason", Value::null()), + ].into_iter().collect()))); +}