-
Notifications
You must be signed in to change notification settings - Fork 429
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve juniper::impl_object macro implementation
- Upgrade syn to 0.15 - Improve implementation - Finalized macro syntax (context, scalar)
- Loading branch information
Showing
10 changed files
with
695 additions
and
258 deletions.
There are no files selected for viewing
160 changes: 160 additions & 0 deletions
160
integration_tests/juniper_tests/src/codegen/impl_object.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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": [], | ||
}, | ||
] | ||
}) | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,5 +9,6 @@ extern crate fnv; | |
#[cfg(test)] | ||
extern crate indexmap; | ||
|
||
#[cfg(test)] | ||
mod codegen; | ||
mod custom_scalar; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.