Skip to content

Commit f1b42fd

Browse files
committed
Add impl_object macro tests
1 parent f056c91 commit f1b42fd

File tree

3 files changed

+70
-21
lines changed

3 files changed

+70
-21
lines changed
+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
use crate::{graphql_value, EmptyMutation, Executor, GraphQLObject, GraphQLType, RootNode};
2+
3+
struct Context {
4+
val1: bool,
5+
}
6+
7+
impl crate::Context for Context {}
8+
9+
struct Query {
10+
field_bool: bool,
11+
}
12+
13+
#[crate::impl_object_internal(
14+
Context = Context,
15+
scalar = crate::DefaultScalarValue,
16+
)]
17+
impl Query {
18+
fn staticfield() -> bool {
19+
true
20+
}
21+
22+
fn with_context(context: &Context) -> bool {
23+
context.val1
24+
}
25+
26+
fn field_bool(&self) -> bool {
27+
self.field_bool
28+
}
29+
}
30+
31+
#[test]
32+
fn test_impl_object_query() {
33+
let doc = r#"
34+
query {
35+
staticfield
36+
withContext
37+
fieldBool
38+
}
39+
"#;
40+
let schema = RootNode::new(Query { field_bool: true }, EmptyMutation::<Context>::new());
41+
let vars = std::collections::HashMap::new();
42+
43+
let (result, errs) = crate::execute(doc, None, &schema, &vars, &Context { val1: true })
44+
.expect("Execution failed");
45+
assert_eq!(errs, []);
46+
assert_eq!(
47+
result,
48+
graphql_value!({
49+
"staticfield": true,
50+
"withContext": true,
51+
"fieldBool": true,
52+
})
53+
);
54+
}

juniper/src/macros/tests/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
mod args;
22
mod field;
3+
mod impl_object;
34
mod interface;
45
mod object;
56
mod scalar;

juniper/src/macros/tests/object.rs

+15-21
Original file line numberDiff line numberDiff line change
@@ -18,49 +18,36 @@ Syntax to validate:
1818
1919
*/
2020

21-
struct Interface;
22-
2321
struct CustomName;
22+
graphql_object!(CustomName: () as "ACustomNamedType" |&self| {
23+
field simple() -> i32 { 0 }
24+
});
2425

2526
#[allow(dead_code)]
2627
struct WithLifetime<'a> {
2728
data: PhantomData<&'a i32>,
2829
}
30+
graphql_object!(<'a> WithLifetime<'a>: () as "WithLifetime" |&self| {
31+
field simple() -> i32 { 0 }
32+
});
2933

3034
#[allow(dead_code)]
3135
struct WithGenerics<T> {
3236
data: T,
3337
}
34-
35-
struct DescriptionFirst;
36-
struct FieldsFirst;
37-
struct InterfacesFirst;
38-
39-
struct CommasWithTrailing;
40-
struct CommasOnMeta;
41-
42-
struct Root;
43-
44-
graphql_object!(CustomName: () as "ACustomNamedType" |&self| {
45-
field simple() -> i32 { 0 }
46-
});
47-
48-
graphql_object!(<'a> WithLifetime<'a>: () as "WithLifetime" |&self| {
49-
field simple() -> i32 { 0 }
50-
});
51-
5238
graphql_object!(<T> WithGenerics<T>: () as "WithGenerics" |&self| {
5339
field simple() -> i32 { 0 }
5440
});
5541

42+
struct Interface;
43+
struct DescriptionFirst;
5644
graphql_interface!(Interface: () |&self| {
5745
field simple() -> i32 { 0 }
5846

5947
instance_resolvers: |_| {
6048
DescriptionFirst => Some(DescriptionFirst {}),
6149
}
6250
});
63-
6451
graphql_object!(DescriptionFirst: () |&self| {
6552
description: "A description"
6653

@@ -69,6 +56,7 @@ graphql_object!(DescriptionFirst: () |&self| {
6956
interfaces: [Interface]
7057
});
7158

59+
struct FieldsFirst;
7260
graphql_object!(FieldsFirst: () |&self| {
7361
field simple() -> i32 { 0 }
7462

@@ -77,6 +65,7 @@ graphql_object!(FieldsFirst: () |&self| {
7765
interfaces: [Interface]
7866
});
7967

68+
struct InterfacesFirst;
8069
graphql_object!(InterfacesFirst: ()|&self| {
8170
interfaces: [Interface]
8271

@@ -85,6 +74,7 @@ graphql_object!(InterfacesFirst: ()|&self| {
8574
description: "A description"
8675
});
8776

77+
struct CommasWithTrailing;
8878
graphql_object!(CommasWithTrailing: () |&self| {
8979
interfaces: [Interface],
9080

@@ -93,13 +83,17 @@ graphql_object!(CommasWithTrailing: () |&self| {
9383
description: "A description",
9484
});
9585

86+
struct CommasOnMeta;
87+
9688
graphql_object!(CommasOnMeta: () |&self| {
9789
interfaces: [Interface],
9890
description: "A description",
9991

10092
field simple() -> i32 { 0 }
10193
});
10294

95+
struct Root;
96+
10397
struct InnerContext;
10498
impl Context for InnerContext {}
10599

0 commit comments

Comments
 (0)