Skip to content

Commit 77febb5

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 77febb5

File tree

10 files changed

+698
-258
lines changed

10 files changed

+698
-258
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
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!(juniper::graphql_value!({
89+
"name": "Query",
90+
"description": "Type Description",
91+
"fields": [
92+
{
93+
"name": "withSelf",
94+
"description": "With Self Description",
95+
"args": [],
96+
},
97+
{
98+
"name": "independent",
99+
"description": None,
100+
"args": [],
101+
},
102+
{
103+
"name": "withExecutor",
104+
"description": None,
105+
"args": [],
106+
},
107+
{
108+
"name": "withExecutorAndSelf",
109+
"description": None,
110+
"args": [],
111+
},
112+
{
113+
"name": "withContext",
114+
"description": None,
115+
"args": [],
116+
},
117+
{
118+
"name": "withContextAndSelf",
119+
"description": None,
120+
"args": [],
121+
},
122+
{
123+
"name": "renamed",
124+
"description": None,
125+
"args": [],
126+
},
127+
{
128+
"name": "hasDescriptionAttr",
129+
"description": "attr",
130+
"args": [],
131+
},
132+
{
133+
"name": "hasDescriptionDocComment",
134+
"description": "Doc description",
135+
"args": [],
136+
},
137+
{
138+
"name": "hasArgument",
139+
"description": None,
140+
"args": [
141+
{
142+
"name": "arg1",
143+
"description": None,
144+
"type": {
145+
"name": None,
146+
},
147+
}
148+
],
149+
},
150+
{
151+
"name": "hasObjectReturn",
152+
"description": None,
153+
"args": [],
154+
},
155+
]
156+
}));
157+
}
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)