Skip to content

Commit f9383a2

Browse files
committed
Add basic interface introspection tests
1 parent 8d9949e commit f9383a2

File tree

1 file changed

+109
-2
lines changed

1 file changed

+109
-2
lines changed

src/executor_tests/introspection.rs

Lines changed: 109 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,30 @@ enum Sample {
99
Two,
1010
}
1111

12+
struct Interface {}
13+
1214
struct Root {}
1315

1416
graphql_enum!(Sample as "SampleEnum" {
1517
Sample::One => "ONE",
1618
Sample::Two => "TWO",
1719
});
1820

21+
graphql_interface!(Interface: () as "SampleInterface" |&self| {
22+
description: "A sample interface"
23+
24+
field sample_enum() -> FieldResult<Sample> as "A sample field in the interface" {
25+
Ok(Sample::One)
26+
}
27+
28+
instance_resolvers: |&_| [
29+
Some(Root {}),
30+
]
31+
});
32+
1933
graphql_object!(Root: () as "Root" |&self| {
34+
interfaces: [Interface]
35+
2036
field sample_enum() -> FieldResult<Sample> {
2137
Ok(Sample::One)
2238
}
@@ -27,6 +43,9 @@ fn enum_introspection() {
2743
let doc = r#"
2844
{
2945
__type(name: "SampleEnum") {
46+
name
47+
kind
48+
description
3049
enumValues {
3150
name
3251
description
@@ -45,10 +64,16 @@ fn enum_introspection() {
4564

4665
println!("Result: {:?}", result);
4766

48-
let values = result
67+
let type_info = result
4968
.as_object_value().expect("Result is not an object")
5069
.get("__type").expect("__type field missing")
51-
.as_object_value().expect("__type field not an object value")
70+
.as_object_value().expect("__type field not an object value");
71+
72+
assert_eq!(type_info.get("name"), Some(&Value::string("SampleEnum")));
73+
assert_eq!(type_info.get("kind"), Some(&Value::string("ENUM")));
74+
assert_eq!(type_info.get("description"), Some(&Value::null()));
75+
76+
let values = type_info
5277
.get("enumValues").expect("enumValues field missing")
5378
.as_list_value().expect("enumValues not a list");
5479

@@ -68,3 +93,85 @@ fn enum_introspection() {
6893
("deprecationReason", Value::null()),
6994
].into_iter().collect())));
7095
}
96+
97+
#[test]
98+
fn interface_introspection() {
99+
let doc = r#"
100+
{
101+
__type(name: "SampleInterface") {
102+
name
103+
kind
104+
description
105+
possibleTypes {
106+
name
107+
}
108+
fields {
109+
name
110+
description
111+
args {
112+
name
113+
}
114+
type {
115+
name
116+
kind
117+
ofType {
118+
name
119+
kind
120+
}
121+
}
122+
isDeprecated
123+
deprecationReason
124+
}
125+
}
126+
}
127+
"#;
128+
let schema = RootNode::new(Root {}, ());
129+
130+
let (result, errs) = ::execute(doc, None, &schema, &HashMap::new(), &())
131+
.expect("Execution failed");
132+
133+
assert_eq!(errs, []);
134+
135+
println!("Result: {:?}", result);
136+
137+
let type_info = result
138+
.as_object_value().expect("Result is not an object")
139+
.get("__type").expect("__type field missing")
140+
.as_object_value().expect("__type field not an object value");
141+
142+
assert_eq!(type_info.get("name"), Some(&Value::string("SampleInterface")));
143+
assert_eq!(type_info.get("kind"), Some(&Value::string("INTERFACE")));
144+
assert_eq!(type_info.get("description"), Some(&Value::string("A sample interface")));
145+
146+
let possible_types = type_info
147+
.get("possibleTypes").expect("possibleTypes field missing")
148+
.as_list_value().expect("possibleTypes not a list");
149+
150+
assert_eq!(possible_types.len(), 1);
151+
152+
assert!(possible_types.contains(&Value::object(vec![
153+
("name", Value::string("Root")),
154+
].into_iter().collect())));
155+
156+
let fields = type_info
157+
.get("fields").expect("fields field missing")
158+
.as_list_value().expect("fields field not an object value");
159+
160+
assert_eq!(fields.len(), 2);
161+
162+
assert!(fields.contains(&Value::object(vec![
163+
("name", Value::string("sampleEnum")),
164+
("description", Value::string("A sample field in the interface")),
165+
("args", Value::list(vec![])),
166+
("type", Value::object(vec![
167+
("name", Value::null()),
168+
("kind", Value::string("NON_NULL")),
169+
("ofType", Value::object(vec![
170+
("name", Value::string("SampleEnum")),
171+
("kind", Value::string("ENUM")),
172+
].into_iter().collect())),
173+
].into_iter().collect())),
174+
("isDeprecated", Value::boolean(false)),
175+
("deprecationReason", Value::null()),
176+
].into_iter().collect())));
177+
}

0 commit comments

Comments
 (0)