@@ -96,7 +96,7 @@ impl<'doc> SchemaVisitor<'doc> for CodeGenPass<'doc> {
96
96
let field_tokens = obj_type
97
97
. fields
98
98
. iter ( )
99
- . map ( |field| self . collect_data_for_field_gen ( field) )
99
+ . map ( |field| self . collect_data_for_field_gen ( field, QuoteFor :: ProcMacro ) )
100
100
. collect :: < Vec < _ > > ( ) ;
101
101
102
102
let trait_methods = field_tokens
@@ -154,7 +154,7 @@ impl<'doc> SchemaVisitor<'doc> for CodeGenPass<'doc> {
154
154
let description = obj_type
155
155
. description
156
156
. as_ref ( )
157
- . map ( |d| quote ! { description: #d } )
157
+ . map ( |d| quote ! { description = #d , } )
158
158
. unwrap_or_else ( empty_token_stream) ;
159
159
160
160
let interfaces = if obj_type. implements_interfaces . is_empty ( ) {
@@ -164,17 +164,23 @@ impl<'doc> SchemaVisitor<'doc> for CodeGenPass<'doc> {
164
164
let name = ident ( name) ;
165
165
quote ! { & #name }
166
166
} ) ;
167
- quote ! { interfaces: [ #( #interface_names) , * ] }
167
+ quote ! { interfaces = [ #( #interface_names) , * ] , }
168
168
} ;
169
169
170
170
let context_type = & self . context_type ;
171
171
172
+ let macro_args = vec ! [
173
+ quote! { Context = #context_type, } ,
174
+ description,
175
+ quote! { scalar = juniper:: DefaultScalarValue , } ,
176
+ interfaces,
177
+ ] ;
178
+
172
179
let code = quote ! {
173
- juniper:: graphql_object! ( #struct_name : #context_type | & self | {
174
- #description
180
+ # [ juniper:: object ( # ( #macro_args ) * ) ]
181
+ impl #struct_name {
175
182
#( #fields) *
176
- #interfaces
177
- } ) ;
183
+ }
178
184
} ;
179
185
self . extend ( code)
180
186
}
@@ -231,7 +237,7 @@ impl<'doc> SchemaVisitor<'doc> for CodeGenPass<'doc> {
231
237
let field_tokens: Vec < FieldTokens > = interface
232
238
. fields
233
239
. iter ( )
234
- . map ( |field| self . collect_data_for_field_gen ( field) )
240
+ . map ( |field| self . collect_data_for_field_gen ( field, QuoteFor :: Macro ) )
235
241
. collect :: < Vec < _ > > ( ) ;
236
242
237
243
let field_token_streams = field_tokens
@@ -256,7 +262,7 @@ impl<'doc> SchemaVisitor<'doc> for CodeGenPass<'doc> {
256
262
}
257
263
} ) ;
258
264
259
- let all_args = to_field_args_list ( & args) ;
265
+ let all_args = to_field_args_list ( & args, QuoteFor :: Macro ) ;
260
266
261
267
let error_type = & self . error_type ;
262
268
@@ -680,10 +686,14 @@ impl<'doc> CodeGenPass<'doc> {
680
686
}
681
687
}
682
688
683
- fn collect_data_for_field_gen ( & mut self , field : & ' doc Field ) -> FieldTokens < ' doc > {
689
+ fn collect_data_for_field_gen (
690
+ & mut self ,
691
+ field : & ' doc Field ,
692
+ quote_for : QuoteFor ,
693
+ ) -> FieldTokens < ' doc > {
684
694
self . error_if_has_unsupported_directive ( & field) ;
685
695
686
- let deprecation = self . quote_deprecations ( & field. directives ) ;
696
+ let deprecation = self . quote_deprecations ( & field. directives , quote_for ) ;
687
697
688
698
let name = ident ( & field. name ) ;
689
699
@@ -711,6 +721,16 @@ impl<'doc> CodeGenPass<'doc> {
711
721
. map ( |arg| {
712
722
let name = ident ( & arg. name ) ;
713
723
let arg_type = & arg. macro_type ;
724
+
725
+ if arg. description . is_some ( ) && quote_for == QuoteFor :: ProcMacro {
726
+ // TODO: support descriptions field arguments.
727
+ // That is currently annoying in juniper when using the `#[object]` proc macro
728
+ // https://docs.rs/juniper_codegen/0.13.2/juniper_codegen/attr.object.html#customization-documentation-renaming-
729
+ return quote ! {
730
+ #name: #arg_type
731
+ } ;
732
+ }
733
+
714
734
let description = doc_tokens ( & arg. description ) ;
715
735
quote ! {
716
736
#description
@@ -772,7 +792,7 @@ impl<'doc> CodeGenPass<'doc> {
772
792
let name = to_enum_name ( & graphql_name) ;
773
793
let description = doc_tokens ( & enum_value. description ) ;
774
794
775
- let deprecation = self . quote_deprecations ( & enum_value. directives ) ;
795
+ let deprecation = self . quote_deprecations ( & enum_value. directives , QuoteFor :: Macro ) ;
776
796
777
797
quote ! {
778
798
#[ allow( missing_docs) ]
@@ -783,7 +803,7 @@ impl<'doc> CodeGenPass<'doc> {
783
803
}
784
804
}
785
805
786
- fn quote_deprecations ( & mut self , directives : & [ Directive ] ) -> TokenStream {
806
+ fn quote_deprecations ( & mut self , directives : & [ Directive ] , quote_for : QuoteFor ) -> TokenStream {
787
807
for directive in directives {
788
808
if directive. name == "deprecated" {
789
809
let mut arguments = BTreeMap :: new ( ) ;
@@ -800,18 +820,27 @@ impl<'doc> CodeGenPass<'doc> {
800
820
801
821
if let Some ( value) = arguments. get ( & "reason" . to_string ( ) ) {
802
822
let tokens = match value {
803
- Value :: String ( reason) => quote ! { #[ deprecated( note = #reason) ] } ,
823
+ Value :: String ( reason) => match quote_for {
824
+ QuoteFor :: Macro => quote ! { #[ deprecated( note = #reason) ] } ,
825
+ QuoteFor :: ProcMacro => quote ! { deprecated = #reason, } ,
826
+ } ,
804
827
_ => {
805
828
self . emit_non_fatal_error (
806
829
directive. position ,
807
830
ErrorKind :: InvalidArgumentsToDeprecateDirective ,
808
831
) ;
809
- quote ! { #[ deprecated] }
832
+ match quote_for {
833
+ QuoteFor :: Macro => quote ! { #[ deprecated] } ,
834
+ QuoteFor :: ProcMacro => quote ! { deprecated, } ,
835
+ }
810
836
}
811
837
} ;
812
838
return tokens;
813
839
} else {
814
- return quote ! { #[ deprecated] } ;
840
+ return match quote_for {
841
+ QuoteFor :: Macro => quote ! { #[ deprecated] } ,
842
+ QuoteFor :: ProcMacro => quote ! { deprecated, } ,
843
+ } ;
815
844
}
816
845
}
817
846
}
@@ -1059,14 +1088,18 @@ fn gen_field(
1059
1088
. map ( ToString :: to_string)
1060
1089
. unwrap_or_else ( String :: new) ;
1061
1090
1062
- let all_args = to_field_args_list ( args) ;
1091
+ let all_args = to_field_args_list ( args, QuoteFor :: ProcMacro ) ;
1063
1092
1064
1093
let deprecation = & field. deprecation ;
1065
1094
1095
+ let macro_args = vec ! [
1096
+ quote! { description = #description } ,
1097
+ quote! { #deprecation } ,
1098
+ ] ;
1099
+
1066
1100
quote ! {
1067
- #[ doc = #description]
1068
- #deprecation
1069
- field #field_name( #all_args) -> std:: result:: Result <#field_type, #error_type> {
1101
+ #[ graphql( #( #macro_args) , * ) ]
1102
+ fn #field_name( #all_args) -> std:: result:: Result <#field_type, #error_type> {
1070
1103
#body
1071
1104
}
1072
1105
}
@@ -1098,11 +1131,28 @@ fn gen_field_body(
1098
1131
}
1099
1132
}
1100
1133
1101
- fn to_field_args_list ( args : & [ TokenStream ] ) -> TokenStream {
1102
- if args. is_empty ( ) {
1103
- quote ! { & executor }
1104
- } else {
1105
- quote ! { & executor, #( #args) , * }
1134
+ #[ derive( Debug , Copy , Clone , Eq , PartialEq ) ]
1135
+ enum QuoteFor {
1136
+ Macro ,
1137
+ ProcMacro ,
1138
+ }
1139
+
1140
+ fn to_field_args_list ( args : & [ TokenStream ] , quote_for : QuoteFor ) -> TokenStream {
1141
+ match quote_for {
1142
+ QuoteFor :: ProcMacro => {
1143
+ if args. is_empty ( ) {
1144
+ quote ! { & self , executor: & Executor }
1145
+ } else {
1146
+ quote ! { & self , executor: & Executor , #( #args) , * }
1147
+ }
1148
+ }
1149
+ QuoteFor :: Macro => {
1150
+ if args. is_empty ( ) {
1151
+ quote ! { & executor }
1152
+ } else {
1153
+ quote ! { & executor, #( #args) , * }
1154
+ }
1155
+ }
1106
1156
}
1107
1157
}
1108
1158
0 commit comments