Skip to content

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
wants to merge 13 commits into from
Closed
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions integration_tests/juniper_tests/src/codegen/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ mod util;
mod derive_enum;
mod derive_input_object;
mod derive_object;
mod proc_macro_param_attrs;
mod scalar_value_transparent;
211 changes: 211 additions & 0 deletions integration_tests/juniper_tests/src/codegen/proc_macro_param_attrs.rs
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,
}
}
Loading