Skip to content

Commit

Permalink
Improve juniper::impl_object macro implementation
Browse files Browse the repository at this point in the history
- Upgrade syn to 0.15
- Improve implementation
- Finalized macro syntax (context, scalar)
  • Loading branch information
theduke committed Mar 8, 2019
1 parent bd53872 commit 651007e
Show file tree
Hide file tree
Showing 10 changed files with 695 additions and 258 deletions.
160 changes: 160 additions & 0 deletions integration_tests/juniper_tests/src/codegen/impl_object.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
use super::util;
use juniper::{EmptyMutation, Executor, GraphQLType};

#[derive(GraphQLObject)]
struct SomeType {
a: bool,
}

#[derive(Default)]
struct Context {
version: Option<String>,
}

#[derive(Default)]
struct TheQuery {
b: bool,
}

#[juniper::impl_object(
scalar = juniper::DefaultScalarValue,
name = "Query",
description = "Type Description",
context = Context,
)]
impl TheQuery {
#[graphql(description = "With Self Description")]
fn with_self(&self) -> bool {
self.b
}

fn independent() -> i32 {
100
}

fn with_executor(exec: &Executor<Context>) -> bool {
true
}

fn with_executor_and_self(&self, exec: &Executor<Context>) -> bool {
true
}

fn with_context(exec: &Context) -> bool {
true
}

fn with_context_and_self(&self, exec: &Context) -> bool {
true
}

#[graphql(name = "renamed")]
fn has_custom_name() -> bool {
true
}

#[graphql(description = "attr")]
fn has_description_attr() -> bool {
true
}

/// Doc description
fn has_description_doc_comment() -> bool {
true
}

fn has_argument(arg1: bool) -> bool {
true
}

fn has_object_return(&self) -> SomeType {
SomeType { a: true }
}
}

#[derive(Default)]
struct Mutation;

#[juniper::impl_object(context = Context)]
impl Mutation {
fn empty() -> bool {
true
}
}

#[test]
fn impl_object_full_introspect() {
let res = util::run_info_query::<TheQuery, Mutation, Context>("Query");
assert_eq!(
res,
juniper::graphql_value!({
"name": "Query",
"description": "Type Description",
"fields": [
{
"name": "withSelf",
"description": "With Self Description",
"args": [],
},
{
"name": "independent",
"description": None,
"args": [],
},
{
"name": "withExecutor",
"description": None,
"args": [],
},
{
"name": "withExecutorAndSelf",
"description": None,
"args": [],
},
{
"name": "withContext",
"description": None,
"args": [],
},
{
"name": "withContextAndSelf",
"description": None,
"args": [],
},
{
"name": "renamed",
"description": None,
"args": [],
},
{
"name": "hasDescriptionAttr",
"description": "attr",
"args": [],
},
{
"name": "hasDescriptionDocComment",
"description": "Doc description",
"args": [],
},
{
"name": "hasArgument",
"description": None,
"args": [
{
"name": "arg1",
"description": None,
"type": {
"name": None,
},
}
],
},
{
"name": "hasObjectReturn",
"description": None,
"args": [],
},
]
})
);
}
3 changes: 3 additions & 0 deletions integration_tests/juniper_tests/src/codegen/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
mod util;

mod derive_enum;
mod derive_input_object;
mod derive_object;
mod impl_object;
55 changes: 55 additions & 0 deletions integration_tests/juniper_tests/src/codegen/util.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
use fnv::FnvHashMap;
use juniper::{DefaultScalarValue, GraphQLType, RootNode, Value, Variables};
use std::default::Default;

pub fn run_query<Query, Mutation, Context>(query: &str) -> Value
where
Query: GraphQLType<DefaultScalarValue, TypeInfo = (), Context = Context> + Default,
Mutation: GraphQLType<DefaultScalarValue, TypeInfo = (), Context = Context> + Default,
Context: Default,
{
let schema = RootNode::new(Query::default(), Mutation::default());
let (result, errs) =
juniper::execute(query, None, &schema, &Variables::new(), &Context::default())
.expect("Execution failed");

assert_eq!(errs, []);
result
}

pub fn run_info_query<Query, Mutation, Context>(type_name: &str) -> Value
where
Query: GraphQLType<DefaultScalarValue, TypeInfo = (), Context = Context> + Default,
Mutation: GraphQLType<DefaultScalarValue, TypeInfo = (), Context = Context> + Default,
Context: Default,
{
let query = format!(
r#"
{{
__type(name: "{}") {{
name,
description,
fields {{
name
description
args {{
name
description
type {{
name
}}
}}
}}
}}
}}
"#,
type_name
);
let result = run_query::<Query, Mutation, Context>(&query);
result
.as_object_value()
.expect("Result is not an object")
.get_field_value("__type")
.expect("__type field missing")
.clone()
}
1 change: 1 addition & 0 deletions integration_tests/juniper_tests/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@ extern crate fnv;
#[cfg(test)]
extern crate indexmap;

#[cfg(test)]
mod codegen;
mod custom_scalar;
1 change: 0 additions & 1 deletion juniper/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ harness = false
path = "benches/bench.rs"

[features]
nightly = []
expose-test-schema = []
default = [
"chrono",
Expand Down
2 changes: 1 addition & 1 deletion juniper_codegen/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ proc-macro = true

[dependencies]
proc-macro2 = "0.4"
syn = { version = "0.14", features = ["full", "extra-traits"] }
syn = { version = "0.15.28", features = ["full", "extra-traits", "parsing"] }
quote = "0.6"
regex = "1"
lazy_static = "1.0.0"
Expand Down
Loading

0 comments on commit 651007e

Please sign in to comment.