diff --git a/juniper/src/macros/tests/field.rs b/juniper/src/macros/tests/field.rs index eea4c110..d023a768 100644 --- a/juniper/src/macros/tests/field.rs +++ b/juniper/src/macros/tests/field.rs @@ -33,6 +33,16 @@ impl Root { 0 } + #[deprecated] + fn deprecated_outer() -> bool { + true + } + + #[deprecated(note = "Deprecation Reason")] + fn deprecated_outer_with_reason() -> bool { + true + } + #[graphql(deprecated = "Deprecation reason")] fn deprecated() -> i32 { 0 @@ -275,6 +285,44 @@ fn introspect_interface_field_description() { }); } +#[test] +fn introspect_object_field_deprecated_outer() { + run_field_info_query("Root", "deprecatedOuter", |field| { + assert_eq!( + field.get_field_value("name"), + Some(&Value::scalar("deprecatedOuter")) + ); + assert_eq!(field.get_field_value("description"), Some(&Value::null())); + assert_eq!( + field.get_field_value("isDeprecated"), + Some(&Value::scalar(true)) + ); + assert_eq!( + field.get_field_value("deprecationReason"), + Some(&Value::null()), + ); + }); +} + +#[test] +fn introspect_object_field_deprecated_outer_with_reason() { + run_field_info_query("Root", "deprecatedOuterWithReason", |field| { + assert_eq!( + field.get_field_value("name"), + Some(&Value::scalar("deprecatedOuterWithReason")) + ); + assert_eq!(field.get_field_value("description"), Some(&Value::null())); + assert_eq!( + field.get_field_value("isDeprecated"), + Some(&Value::scalar(true)) + ); + assert_eq!( + field.get_field_value("deprecationReason"), + Some(&Value::scalar("Deprecation Reason")), + ); + }); +} + #[test] fn introspect_object_field_deprecated() { run_field_info_query("Root", "deprecated", |field| { diff --git a/juniper_codegen/src/util.rs b/juniper_codegen/src/util.rs index 44a65fbb..7c8f8632 100644 --- a/juniper_codegen/src/util.rs +++ b/juniper_codegen/src/util.rs @@ -76,14 +76,23 @@ pub fn get_deprecated(attrs: &Vec) -> Option { fn get_deprecated_meta_list(list: &MetaList) -> DeprecationAttr { for meta in &list.nested { match meta { - &NestedMeta::Meta(Meta::NameValue(ref nv)) if nv.ident == "note" => match &nv.lit { - &Lit::Str(ref strlit) => { - return DeprecationAttr { - reason: Some(strlit.value().to_string()), - }; + &NestedMeta::Meta(Meta::NameValue(ref nv)) => { + if nv.ident == "note" { + match &nv.lit { + &Lit::Str(ref strlit) => { + return DeprecationAttr { + reason: Some(strlit.value()), + }; + } + _ => panic!("deprecated attribute note value only has string literal"), + } + } else { + panic!( + "Unrecognized setting on #[deprecated(..)] attribute: {}", + nv.ident + ); } - _ => panic!("deprecated attribute note value only has string literal"), - }, + } _ => {} } } @@ -434,7 +443,7 @@ pub enum FieldAttributeParseMode { enum FieldAttribute { Name(syn::LitStr), Description(syn::LitStr), - Deprecation(Option), + Deprecation(DeprecationAttr), Skip(syn::Ident), Arguments(HashMap), } @@ -466,11 +475,13 @@ impl parse::Parse for FieldAttribute { "deprecated" | "deprecation" => { let reason = if input.peek(Token![=]) { input.parse::()?; - Some(input.parse()?) + Some(input.parse::()?.value()) } else { None }; - Ok(FieldAttribute::Deprecation(reason)) + Ok(FieldAttribute::Deprecation(DeprecationAttr { + reason: reason, + })) } "skip" => Ok(FieldAttribute::Skip(ident)), "arguments" => { @@ -525,10 +536,8 @@ impl parse::Parse for FieldAttributes { FieldAttribute::Description(name) => { output.description = Some(name.value()); } - FieldAttribute::Deprecation(reason_opt) => { - output.deprecation = Some(DeprecationAttr { - reason: reason_opt.map(|val| val.value()), - }); + FieldAttribute::Deprecation(attr) => { + output.deprecation = Some(attr); } FieldAttribute::Skip(_) => { output.skip = true; @@ -553,6 +562,7 @@ impl FieldAttributes { _mode: FieldAttributeParseMode, ) -> syn::parse::Result { let doc_comment = get_doc_comment(&attrs); + let deprecation = get_deprecated(&attrs); let attr_opt = attrs .into_iter() @@ -562,9 +572,15 @@ impl FieldAttributes { Some(attr) => syn::parse(attr.tts.into())?, None => Self::default(), }; + + // Check for regular doc comment. if output.description.is_none() { output.description = doc_comment; } + if output.deprecation.is_none() { + output.deprecation = deprecation; + } + Ok(output) }