Skip to content

Commit 8adea08

Browse files
committed
Improve juniper::impl_object macro implementation
- Upgrade syn to 0.15 - Improve implementation - Finalized macro syntax (context, scalar)
1 parent bd53872 commit 8adea08

File tree

10 files changed

+701
-258
lines changed

10 files changed

+701
-258
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
use super::util;
2+
use juniper::{EmptyMutation, Executor, GraphQLType};
3+
4+
#[derive(GraphQLObject)]
5+
struct SomeType {
6+
a: bool,
7+
}
8+
9+
#[derive(Default)]
10+
struct Context {
11+
version: Option<String>,
12+
}
13+
14+
#[derive(Default)]
15+
struct TheQuery {
16+
b: bool,
17+
}
18+
19+
#[juniper::impl_object(
20+
scalar = juniper::DefaultScalarValue,
21+
name = "Query",
22+
description = "Type Description",
23+
context = Context,
24+
)]
25+
impl TheQuery {
26+
#[graphql(description = "With Self Description")]
27+
fn with_self(&self) -> bool {
28+
self.b
29+
}
30+
31+
fn independent() -> i32 {
32+
100
33+
}
34+
35+
fn with_executor(exec: &Executor<Context>) -> bool {
36+
true
37+
}
38+
39+
fn with_executor_and_self(&self, exec: &Executor<Context>) -> bool {
40+
true
41+
}
42+
43+
fn with_context(exec: &Context) -> bool {
44+
true
45+
}
46+
47+
fn with_context_and_self(&self, exec: &Context) -> bool {
48+
true
49+
}
50+
51+
#[graphql(name = "renamed")]
52+
fn has_custom_name() -> bool {
53+
true
54+
}
55+
56+
#[graphql(description = "attr")]
57+
fn has_description_attr() -> bool {
58+
true
59+
}
60+
61+
/// Doc description
62+
fn has_description_doc_comment() -> bool {
63+
true
64+
}
65+
66+
fn has_argument(arg1: bool) -> bool {
67+
true
68+
}
69+
70+
fn has_object_return(&self) -> SomeType {
71+
SomeType { a: true }
72+
}
73+
}
74+
75+
#[derive(Default)]
76+
struct Mutation;
77+
78+
#[juniper::impl_object(context = Context)]
79+
impl Mutation {
80+
fn empty() -> bool {
81+
true
82+
}
83+
}
84+
85+
#[test]
86+
fn impl_object_full_introspect() {
87+
let res = util::run_info_query::<TheQuery, Mutation, Context>("Query");
88+
assert_eq!(
89+
dbg!(res),
90+
juniper::graphql_value!({
91+
"name": "Query",
92+
"description": "Type Description",
93+
"fields": [
94+
{
95+
"name": "withSelf",
96+
"description": "With Self Description",
97+
"args": [],
98+
},
99+
{
100+
"name": "independent",
101+
"description": None,
102+
"args": [],
103+
},
104+
{
105+
"name": "withExecutor",
106+
"description": None,
107+
"args": [],
108+
},
109+
{
110+
"name": "withExecutorAndSelf",
111+
"description": None,
112+
"args": [],
113+
},
114+
{
115+
"name": "withContext",
116+
"description": None,
117+
"args": [],
118+
},
119+
{
120+
"name": "withContextAndSelf",
121+
"description": None,
122+
"args": [],
123+
},
124+
{
125+
"name": "renamed",
126+
"description": None,
127+
"args": [],
128+
},
129+
{
130+
"name": "hasDescriptionAttr",
131+
"description": "attr",
132+
"args": [],
133+
},
134+
{
135+
"name": "hasDescriptionDocComment",
136+
"description": "Doc description",
137+
"args": [],
138+
},
139+
{
140+
"name": "hasArgument",
141+
"description": None,
142+
"args": [
143+
{
144+
"name": "arg1",
145+
"description": None,
146+
"type": {
147+
"name": None,
148+
},
149+
}
150+
],
151+
},
152+
{
153+
"name": "hasObjectReturn",
154+
"description": None,
155+
"args": [],
156+
},
157+
]
158+
})
159+
);
160+
}
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
mod util;
2+
13
mod derive_enum;
24
mod derive_input_object;
35
mod derive_object;
6+
mod impl_object;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
use fnv::FnvHashMap;
2+
use juniper::{DefaultScalarValue, GraphQLType, RootNode, Value, Variables};
3+
use std::default::Default;
4+
5+
pub fn run_query<Query, Mutation, Context>(query: &str) -> Value
6+
where
7+
Query: GraphQLType<DefaultScalarValue, TypeInfo = (), Context = Context> + Default,
8+
Mutation: GraphQLType<DefaultScalarValue, TypeInfo = (), Context = Context> + Default,
9+
Context: Default,
10+
{
11+
let schema = RootNode::new(Query::default(), Mutation::default());
12+
let (result, errs) =
13+
juniper::execute(query, None, &schema, &Variables::new(), &Context::default())
14+
.expect("Execution failed");
15+
16+
assert_eq!(errs, []);
17+
result
18+
}
19+
20+
pub fn run_info_query<Query, Mutation, Context>(type_name: &str) -> Value
21+
where
22+
Query: GraphQLType<DefaultScalarValue, TypeInfo = (), Context = Context> + Default,
23+
Mutation: GraphQLType<DefaultScalarValue, TypeInfo = (), Context = Context> + Default,
24+
Context: Default,
25+
{
26+
let query = format!(
27+
r#"
28+
{{
29+
__type(name: "{}") {{
30+
name,
31+
description,
32+
fields {{
33+
name
34+
description
35+
args {{
36+
name
37+
description
38+
type {{
39+
name
40+
}}
41+
}}
42+
}}
43+
}}
44+
}}
45+
"#,
46+
type_name
47+
);
48+
let result = run_query::<Query, Mutation, Context>(&query);
49+
result
50+
.as_object_value()
51+
.expect("Result is not an object")
52+
.get_field_value("__type")
53+
.expect("__type field missing")
54+
.clone()
55+
}

integration_tests/juniper_tests/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,6 @@ extern crate fnv;
99
#[cfg(test)]
1010
extern crate indexmap;
1111

12+
#[cfg(test)]
1213
mod codegen;
1314
mod custom_scalar;

juniper/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ harness = false
2222
path = "benches/bench.rs"
2323

2424
[features]
25-
nightly = []
25+
nightly = ["juniper_codegen/nightly"]
2626
expose-test-schema = []
2727
default = [
2828
"chrono",

juniper_codegen/Cargo.toml

+4-1
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,12 @@ repository = "https://github.com/graphql-rust/juniper"
1313
[lib]
1414
proc-macro = true
1515

16+
[features]
17+
nightly = []
18+
1619
[dependencies]
1720
proc-macro2 = "0.4"
18-
syn = { version = "0.14", features = ["full", "extra-traits"] }
21+
syn = { version = "0.15.28", features = ["full", "extra-traits", "parsing"] }
1922
quote = "0.6"
2023
regex = "1"
2124
lazy_static = "1.0.0"

0 commit comments

Comments
 (0)