|
| 1 | +use std::collections::HashMap; |
| 2 | + |
| 3 | +use executor::FieldResult; |
| 4 | +use value::Value; |
| 5 | +use schema::model::RootNode; |
| 6 | + |
| 7 | +enum Sample { |
| 8 | + One, |
| 9 | + Two, |
| 10 | +} |
| 11 | + |
| 12 | +struct Root {} |
| 13 | + |
| 14 | +graphql_enum!(Sample as "SampleEnum" { |
| 15 | + Sample::One => "ONE", |
| 16 | + Sample::Two => "TWO", |
| 17 | +}); |
| 18 | + |
| 19 | +graphql_object!(Root: () as "Root" |&self| { |
| 20 | + field sample_enum() -> FieldResult<Sample> { |
| 21 | + Ok(Sample::One) |
| 22 | + } |
| 23 | +}); |
| 24 | + |
| 25 | +#[test] |
| 26 | +fn enum_introspection() { |
| 27 | + let doc = r#" |
| 28 | + { |
| 29 | + __type(name: "SampleEnum") { |
| 30 | + enumValues { |
| 31 | + name |
| 32 | + description |
| 33 | + isDeprecated |
| 34 | + deprecationReason |
| 35 | + } |
| 36 | + } |
| 37 | + } |
| 38 | + "#; |
| 39 | + let schema = RootNode::new(Root {}, ()); |
| 40 | + |
| 41 | + let (result, errs) = ::execute(doc, None, &schema, &HashMap::new(), &()) |
| 42 | + .expect("Execution failed"); |
| 43 | + |
| 44 | + assert_eq!(errs, []); |
| 45 | + |
| 46 | + println!("Result: {:?}", result); |
| 47 | + |
| 48 | + let values = result |
| 49 | + .as_object_value().expect("Result is not an object") |
| 50 | + .get("__type").expect("__type field missing") |
| 51 | + .as_object_value().expect("__type field not an object value") |
| 52 | + .get("enumValues").expect("enumValues field missing") |
| 53 | + .as_list_value().expect("enumValues not a list"); |
| 54 | + |
| 55 | + assert_eq!(values.len(), 2); |
| 56 | + |
| 57 | + assert!(values.contains(&Value::object(vec![ |
| 58 | + ("name", Value::string("ONE")), |
| 59 | + ("description", Value::null()), |
| 60 | + ("isDeprecated", Value::boolean(false)), |
| 61 | + ("deprecationReason", Value::null()), |
| 62 | + ].into_iter().collect()))); |
| 63 | + |
| 64 | + assert!(values.contains(&Value::object(vec![ |
| 65 | + ("name", Value::string("TWO")), |
| 66 | + ("description", Value::null()), |
| 67 | + ("isDeprecated", Value::boolean(false)), |
| 68 | + ("deprecationReason", Value::null()), |
| 69 | + ].into_iter().collect()))); |
| 70 | +} |
0 commit comments