-
Notifications
You must be signed in to change notification settings - Fork 434
Make proc macro support parameter attributes #441
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from 5 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
3c8cf55
Add failing test
davidpdrsn 5a3cccf
Support customizing proc macro args with attrs directly on the args
davidpdrsn 1239eb7
Improve error message
davidpdrsn 51df5b9
Remove needless comments
davidpdrsn eee3784
Format
davidpdrsn 5a024cf
Remove support for `#[graphql(arguments(...))]` attribute
davidpdrsn a368f77
Add crate "assert-json-diff" for more friend test failures
davidpdrsn 1028adb
Support renaming arguments through param attr
davidpdrsn 79c98c2
Update changelog
davidpdrsn 99a99fc
Demonstrate naming args in docs
davidpdrsn 2858764
Format
davidpdrsn 5f31875
Revert "Format"
davidpdrsn ce34e54
Merge remote-tracking branch 'upstream/master' into parameter-attributes
davidpdrsn File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
211 changes: 211 additions & 0 deletions
211
integration_tests/juniper_tests/src/codegen/proc_macro_param_attrs.rs
This file contains hidden or 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,211 @@ | ||
use juniper::*; | ||
use serde_json::{self, Value}; | ||
|
||
struct Query; | ||
|
||
#[juniper::object] | ||
impl Query { | ||
#[graphql(arguments( | ||
arg1(default = true, description = "arg1 desc"), | ||
arg2(default = false, description = "arg2 desc"), | ||
))] | ||
fn field_old_attrs(arg1: bool, arg2: bool) -> bool { | ||
arg1 && arg2 | ||
} | ||
|
||
fn field_new_attrs( | ||
#[graphql(default = true, description = "arg1 desc")] arg1: bool, | ||
#[graphql(default = false, description = "arg2 desc")] arg2: bool, | ||
) -> bool { | ||
arg1 && arg2 | ||
} | ||
} | ||
|
||
// The query that GraphiQL runs to inspect the schema | ||
static SCHEMA_INTROSPECTION_QUERY: &str = r#" | ||
query IntrospectionQuery { | ||
__schema { | ||
types { | ||
...FullType | ||
} | ||
} | ||
} | ||
|
||
fragment FullType on __Type { | ||
name | ||
fields(includeDeprecated: true) { | ||
name | ||
description | ||
args { | ||
...InputValue | ||
} | ||
} | ||
} | ||
|
||
fragment InputValue on __InputValue { | ||
name | ||
description | ||
defaultValue | ||
} | ||
"#; | ||
|
||
#[test] | ||
fn old_descriptions_applied_correctly() { | ||
let schema = introspect_schema(); | ||
let query = schema.types.iter().find(|ty| ty.name == "Query").unwrap(); | ||
|
||
let field = query | ||
.fields | ||
.iter() | ||
.find(|field| field.name == "fieldOldAttrs") | ||
.unwrap(); | ||
|
||
let arg1 = field.args.iter().find(|arg| arg.name == "arg1").unwrap(); | ||
assert_eq!(&arg1.description, &Some("arg1 desc".to_string())); | ||
assert_eq!( | ||
&arg1.default_value, | ||
&Some(Value::String("true".to_string())) | ||
); | ||
|
||
let arg2 = field.args.iter().find(|arg| arg.name == "arg2").unwrap(); | ||
assert_eq!(&arg2.description, &Some("arg2 desc".to_string())); | ||
assert_eq!( | ||
&arg2.default_value, | ||
&Some(Value::String("false".to_string())) | ||
); | ||
} | ||
|
||
#[test] | ||
fn new_descriptions_applied_correctly() { | ||
let schema = introspect_schema(); | ||
let query = schema.types.iter().find(|ty| ty.name == "Query").unwrap(); | ||
|
||
let field = query | ||
.fields | ||
.iter() | ||
.find(|field| field.name == "fieldNewAttrs") | ||
.unwrap(); | ||
|
||
let arg1 = field.args.iter().find(|arg| arg.name == "arg1").unwrap(); | ||
assert_eq!(&arg1.description, &Some("arg1 desc".to_string())); | ||
assert_eq!( | ||
&arg1.default_value, | ||
&Some(Value::String("true".to_string())) | ||
); | ||
|
||
let arg2 = field.args.iter().find(|arg| arg.name == "arg2").unwrap(); | ||
assert_eq!(&arg2.description, &Some("arg2 desc".to_string())); | ||
assert_eq!( | ||
&arg2.default_value, | ||
&Some(Value::String("false".to_string())) | ||
); | ||
} | ||
|
||
#[derive(Debug)] | ||
struct Schema { | ||
types: Vec<Type>, | ||
} | ||
|
||
#[derive(Debug)] | ||
struct Type { | ||
name: String, | ||
fields: Vec<Field>, | ||
} | ||
|
||
#[derive(Debug)] | ||
struct Field { | ||
name: String, | ||
args: Vec<Arg>, | ||
description: Option<String>, | ||
} | ||
|
||
#[derive(Debug)] | ||
struct Arg { | ||
name: String, | ||
description: Option<String>, | ||
default_value: Option<Value>, | ||
} | ||
|
||
fn introspect_schema() -> Schema { | ||
let (value, _errors) = juniper::execute( | ||
SCHEMA_INTROSPECTION_QUERY, | ||
None, | ||
&RootNode::new(Query, juniper::EmptyMutation::new()), | ||
&Variables::new(), | ||
&(), | ||
) | ||
.unwrap(); | ||
|
||
let value: Value = serde_json::from_str(&serde_json::to_string(&value).unwrap()).unwrap(); | ||
|
||
let types = value["__schema"]["types"] | ||
.as_array() | ||
.unwrap() | ||
.iter() | ||
.map(parse_type) | ||
.collect::<Vec<_>>(); | ||
|
||
Schema { types } | ||
} | ||
|
||
fn parse_type(value: &Value) -> Type { | ||
let name = value["name"].as_str().unwrap().to_string(); | ||
|
||
let fields = if value["fields"].is_null() { | ||
vec![] | ||
} else { | ||
value["fields"] | ||
.as_array() | ||
.unwrap() | ||
.iter() | ||
.map(parse_field) | ||
.collect::<Vec<_>>() | ||
}; | ||
|
||
Type { name, fields } | ||
} | ||
|
||
fn parse_field(value: &Value) -> Field { | ||
let name = value["name"].as_str().unwrap().to_string(); | ||
|
||
let description = if value["description"].is_null() { | ||
None | ||
} else { | ||
Some(value["description"].as_str().unwrap().to_string()) | ||
}; | ||
|
||
let args = value["args"] | ||
.as_array() | ||
.unwrap() | ||
.iter() | ||
.map(parse_arg) | ||
.collect::<Vec<_>>(); | ||
|
||
Field { | ||
name, | ||
description, | ||
args, | ||
} | ||
} | ||
|
||
fn parse_arg(value: &Value) -> Arg { | ||
let name = value["name"].as_str().unwrap().to_string(); | ||
|
||
let description = if value["description"].is_null() { | ||
None | ||
} else { | ||
Some(value["description"].as_str().unwrap().to_string()) | ||
}; | ||
|
||
let default_value = if value["defaultValue"].is_null() { | ||
None | ||
} else { | ||
Some(value["defaultValue"].clone()) | ||
}; | ||
|
||
Arg { | ||
name, | ||
description, | ||
default_value, | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.