Skip to content

Commit 14ea90b

Browse files
committed
Add object introspection tests
1 parent 5659a69 commit 14ea90b

File tree

2 files changed

+120
-4
lines changed

2 files changed

+120
-4
lines changed

src/executor_tests/introspection.rs

Lines changed: 96 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,19 @@ graphql_interface!(Interface: () as "SampleInterface" |&self| {
4343
});
4444

4545
graphql_object!(Root: () as "Root" |&self| {
46+
description: "The root query object in the schema"
47+
4648
interfaces: [Interface]
4749

4850
field sample_enum() -> FieldResult<Sample> {
4951
Ok(Sample::One)
5052
}
5153

52-
field sample_scalar() -> FieldResult<Scalar> {
53-
Ok(Scalar(123))
54+
field sample_scalar(
55+
first: i64 as "The first number",
56+
second = 123: i64 as "The second number"
57+
) -> FieldResult<Scalar> {
58+
Ok(Scalar(first + second))
5459
}
5560
});
5661

@@ -59,7 +64,8 @@ fn test_execution() {
5964
let doc = r#"
6065
{
6166
sampleEnum
62-
sampleScalar
67+
first: sampleScalar(first: 0)
68+
second: sampleScalar(first: 10 second: 20)
6369
}
6470
"#;
6571
let schema = RootNode::new(Root {}, ());
@@ -73,7 +79,8 @@ fn test_execution() {
7379

7480
assert_eq!(result, Value::object(vec![
7581
("sampleEnum", Value::string("ONE")),
76-
("sampleScalar", Value::int(123)),
82+
("first", Value::int(123)),
83+
("second", Value::int(30)),
7784
].into_iter().collect()));
7885
}
7986

@@ -231,6 +238,91 @@ fn interface_introspection() {
231238
].into_iter().collect())));
232239
}
233240

241+
#[test]
242+
fn object_introspection() {
243+
let doc = r#"
244+
{
245+
__type(name: "Root") {
246+
name
247+
kind
248+
description
249+
fields {
250+
name
251+
description
252+
args {
253+
name
254+
}
255+
type {
256+
name
257+
kind
258+
ofType {
259+
name
260+
kind
261+
}
262+
}
263+
isDeprecated
264+
deprecationReason
265+
}
266+
possibleTypes { name }
267+
interfaces { name }
268+
enumValues { name }
269+
inputFields { name }
270+
ofType { name }
271+
}
272+
}
273+
"#;
274+
let schema = RootNode::new(Root {}, ());
275+
276+
let (result, errs) = ::execute(doc, None, &schema, &HashMap::new(), &())
277+
.expect("Execution failed");
278+
279+
assert_eq!(errs, []);
280+
281+
println!("Result: {:?}", result);
282+
283+
let type_info = result
284+
.as_object_value().expect("Result is not an object")
285+
.get("__type").expect("__type field missing")
286+
.as_object_value().expect("__type field not an object value");
287+
288+
assert_eq!(type_info.get("name"), Some(&Value::string("Root")));
289+
assert_eq!(type_info.get("kind"), Some(&Value::string("OBJECT")));
290+
assert_eq!(type_info.get("description"), Some(&Value::string("The root query object in the schema")));
291+
assert_eq!(
292+
type_info.get("interfaces"),
293+
Some(&Value::list(vec![
294+
Value::object(vec![
295+
("name", Value::string("SampleInterface")),
296+
].into_iter().collect()),
297+
])));
298+
assert_eq!(type_info.get("enumValues"), Some(&Value::null()));
299+
assert_eq!(type_info.get("inputFields"), Some(&Value::null()));
300+
assert_eq!(type_info.get("ofType"), Some(&Value::null()));
301+
assert_eq!(type_info.get("possibleTypes"), Some(&Value::null()));
302+
303+
let fields = type_info
304+
.get("fields").expect("fields field missing")
305+
.as_list_value().expect("fields field not an object value");
306+
307+
assert_eq!(fields.len(), 5); // The two fields, __typename, __type, __schema
308+
309+
assert!(fields.contains(&Value::object(vec![
310+
("name", Value::string("sampleEnum")),
311+
("description", Value::null()),
312+
("args", Value::list(vec![])),
313+
("type", Value::object(vec![
314+
("name", Value::null()),
315+
("kind", Value::string("NON_NULL")),
316+
("ofType", Value::object(vec![
317+
("name", Value::string("SampleEnum")),
318+
("kind", Value::string("ENUM")),
319+
].into_iter().collect())),
320+
].into_iter().collect())),
321+
("isDeprecated", Value::boolean(false)),
322+
("deprecationReason", Value::null()),
323+
].into_iter().collect())));
324+
}
325+
234326
#[test]
235327
fn scalar_introspection() {
236328
let doc = r#"

src/macros/args.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,30 @@ macro_rules! __graphql__args {
9696
(
9797
@apply_args,
9898
$reg:expr, $base:expr, ( $name:ident = $default:tt : $t:ty , $( $rest:tt )* )
99+
) => {
100+
__graphql__args!(
101+
@apply_args,
102+
$reg,
103+
$base.argument($reg.arg_with_default::<$t>(
104+
&$crate::to_snake_case(stringify!($name)),
105+
&__graphql__args!(@as_expr, $default)))
106+
.description($desc),
107+
( $($rest)* ))
108+
};
109+
110+
(
111+
@apply_args,
112+
$reg:expr, $base:expr, ( $name:ident = $default:tt : $t:ty as $desc:expr )
113+
) => {
114+
$base.argument($reg.arg_with_default::<$t>(
115+
&$crate::to_snake_case(stringify!($name)),
116+
&__graphql__args!(@as_expr, $default)))
117+
.description($desc)
118+
};
119+
120+
(
121+
@apply_args,
122+
$reg:expr, $base:expr, ( $name:ident = $default:tt : $t:ty as $desc:expr , $( $rest:tt )* )
99123
) => {
100124
__graphql__args!(
101125
@apply_args,

0 commit comments

Comments
 (0)