From 81ce7780fa66cf287f2542c8750a0726b38b3a69 Mon Sep 17 00:00:00 2001 From: theduke Date: Thu, 3 Aug 2017 04:18:11 +0200 Subject: [PATCH] Codegen: implement description for custom derives + improve tests Description attribute was available, but not actually used for derive Object, InputObject and enum and the fields on input object. Description is now properly set in each of those cases. Also, tests for all three derives now properly verify the meta information (name and description) for the derived GraphQLType. --- juniper_codegen/src/derive_enum.rs | 12 ++++++-- juniper_codegen/src/derive_input_object.rs | 28 ++++++++++++++----- juniper_codegen/src/derive_object.rs | 1 - juniper_tests/src/codegen/derive_enum.rs | 3 ++ .../src/codegen/derive_input_object.rs | 14 ++++++++-- juniper_tests/src/codegen/derive_object.rs | 12 +++++++- 6 files changed, 55 insertions(+), 15 deletions(-) diff --git a/juniper_codegen/src/derive_enum.rs b/juniper_codegen/src/derive_enum.rs index 2e254858..76292621 100644 --- a/juniper_codegen/src/derive_enum.rs +++ b/juniper_codegen/src/derive_enum.rs @@ -84,6 +84,11 @@ pub fn impl_enum(ast: &syn::DeriveInput) -> Tokens { let attrs = EnumAttrs::from_input(ast); let name = attrs.name.unwrap_or(ast.ident.to_string()); + let meta_description = match attrs.description { + Some(descr) => quote!{ let meta = meta.description(#descr); }, + None => quote!{ let meta = meta; }, + }; + let mut values = Vec::::new(); let mut resolves = Vec::::new(); let mut from_inputs = Vec::::new(); @@ -146,10 +151,11 @@ pub fn impl_enum(ast: &syn::DeriveInput) -> Tokens { } fn meta<'r>(registry: &mut ::juniper::Registry<'r>) -> ::juniper::meta::MetaType<'r> { - registry.build_enum_type::<#ident>(&[ + let meta = registry.build_enum_type::<#ident>(&[ #(#values)* - ]) - .into_meta() + ]); + #meta_description + meta.into_meta() } fn resolve(&self, _: Option<&[::juniper::Selection]>, _: &::juniper::Executor) -> ::juniper::Value { diff --git a/juniper_codegen/src/derive_input_object.rs b/juniper_codegen/src/derive_input_object.rs index 78440b14..f00b2a06 100644 --- a/juniper_codegen/src/derive_input_object.rs +++ b/juniper_codegen/src/derive_input_object.rs @@ -90,6 +90,11 @@ pub fn impl_input_object(ast: &syn::DeriveInput) -> Tokens { let attrs = ObjAttrs::from_input(ast); let name = attrs.name.unwrap_or(ast.ident.to_string()); + let meta_description = match attrs.description { + Some(descr) => quote!{ let meta = meta.description(#descr); }, + None => quote!{ let meta = meta; }, + }; + let mut meta_fields = Vec::::new(); let mut from_inputs = Vec::::new(); let mut to_inputs = Vec::::new(); @@ -110,9 +115,9 @@ pub fn impl_input_object(ast: &syn::DeriveInput) -> Tokens { ::util::to_camel_case(field_ident.as_ref()) }, }; - let descr = match field_attrs.description { - Some(s) => quote!{ Some(#s.to_string()) }, - None => quote!{ None }, + let field_description = match field_attrs.description { + Some(s) => quote!{ let field = field.description(#s); }, + None => quote!{ let field = field; }, }; let default = match field_attrs.default { @@ -127,18 +132,25 @@ pub fn impl_input_object(ast: &syn::DeriveInput) -> Tokens { None => None, }; - let meta_field = match default { + let create_meta_field = match default { Some(ref def) => { quote!{ - registry.arg_with_default::<#field_ty>( #name, &#def), + let field = registry.arg_with_default::<#field_ty>( #name, &#def); } }, None => { quote!{ - registry.arg::<#field_ty>(#name), + let field = registry.arg::<#field_ty>(#name); } } }; + let meta_field = quote!{ + { + #create_meta_field + #field_description + field + }, + }; meta_fields.push(meta_field); // Buil from_input clause. @@ -183,7 +195,9 @@ pub fn impl_input_object(ast: &syn::DeriveInput) -> Tokens { let fields = &[ #(#meta_fields)* ]; - registry.build_input_object_type::<#ident>(fields).into_meta() + let meta = registry.build_input_object_type::<#ident>(fields); + #meta_description + meta.into_meta() } } diff --git a/juniper_codegen/src/derive_object.rs b/juniper_codegen/src/derive_object.rs index e49f8b0d..04a6b303 100644 --- a/juniper_codegen/src/derive_object.rs +++ b/juniper_codegen/src/derive_object.rs @@ -101,7 +101,6 @@ pub fn impl_object(ast: &syn::DeriveInput) -> Tokens { let field_ty = &field.ty; let field_attrs = ObjFieldAttrs::from_input(field); let field_ident = field.ident.as_ref().unwrap(); - let field_ident_name = field_ident.to_string(); // Build value. let name = match field_attrs.name { diff --git a/juniper_tests/src/codegen/derive_enum.rs b/juniper_tests/src/codegen/derive_enum.rs index f9ba66a9..84512c8c 100644 --- a/juniper_tests/src/codegen/derive_enum.rs +++ b/juniper_tests/src/codegen/derive_enum.rs @@ -24,6 +24,9 @@ fn test_derived_enum() { let mut registry = juniper::Registry::new(HashMap::new()); let meta = SomeEnum::meta(&mut registry); + assert_eq!(meta.name(), Some("Some")); + assert_eq!(meta.description(), Some(&"enum descr".to_string())); + // Test Regular variant. assert_eq!( SomeEnum::Regular.to(), diff --git a/juniper_tests/src/codegen/derive_input_object.rs b/juniper_tests/src/codegen/derive_input_object.rs index 41ec3351..c1bbeb72 100644 --- a/juniper_tests/src/codegen/derive_input_object.rs +++ b/juniper_tests/src/codegen/derive_input_object.rs @@ -1,10 +1,12 @@ -use juniper::{ToInputValue, GraphQLType, FromInputValue}; +use std::collections::HashMap; + +use juniper::{self, ToInputValue, GraphQLType, FromInputValue}; #[derive(GraphQLInputObject, Debug, PartialEq)] -#[graphql(name="MyInput")] +#[graphql(name="MyInput", description="input descr")] struct Input { regular_field: String, - #[graphql(name="haha", default="33")] + #[graphql(name="haha", default="33", description="haha descr")] c: i32, } @@ -12,6 +14,12 @@ struct Input { fn test_derived_input_object() { assert_eq!(Input::name(), Some("MyInput")); + // Validate meta info. + let mut registry = juniper::Registry::new(HashMap::new()); + let meta = Input::meta(&mut registry); + assert_eq!(meta.name(), Some("MyInput")); + assert_eq!(meta.description(), Some(&"input descr".to_string())); + let obj = Input { regular_field: "a".to_string(), c: 33, diff --git a/juniper_tests/src/codegen/derive_object.rs b/juniper_tests/src/codegen/derive_object.rs index 97ffa0df..26d07d79 100644 --- a/juniper_tests/src/codegen/derive_object.rs +++ b/juniper_tests/src/codegen/derive_object.rs @@ -1,4 +1,6 @@ -use juniper::{execute, GraphQLType, Value, Variables, EmptyMutation, RootNode}; +use std::collections::HashMap; + +use juniper::{self, execute, GraphQLType, Value, Variables, EmptyMutation, RootNode}; #[derive(GraphQLObject, Debug, PartialEq)] #[graphql(name="MyObj", description="obj descr")] @@ -22,6 +24,14 @@ graphql_object!(Query: () |&self| { #[test] fn test_derived_object() { assert_eq!(Obj::name(), Some("MyObj")); + + // Verify meta info. + let mut registry = juniper::Registry::new(HashMap::new()); + let meta = Obj::meta(&mut registry); + + assert_eq!(meta.name(), Some("MyObj")); + assert_eq!(meta.description(), Some(&"obj descr".to_string())); + let doc = r#" { obj {