File tree Expand file tree Collapse file tree 4 files changed +95
-5
lines changed
graphql_client_codegen/src Expand file tree Collapse file tree 4 files changed +95
-5
lines changed Original file line number Diff line number Diff line change @@ -43,11 +43,20 @@ pub(super) fn generate_input_object_definitions(
43
43
quote ! ( #annotation pub #name_ident: #field_type)
44
44
} ) ;
45
45
46
- quote ! {
47
- #variable_derives
48
- pub struct #struct_name {
49
- #( #fields, ) *
50
- }
46
+ match * options. skip_none ( ) {
47
+ true => quote ! {
48
+ #[ serde( skip_serializing_if = "Option::is_none" ) ]
49
+ #variable_derives
50
+ pub struct #struct_name{
51
+ #( #fields, ) *
52
+ }
53
+ } ,
54
+ false => quote ! {
55
+ #variable_derives
56
+ pub struct #struct_name{
57
+ #( #fields, ) *
58
+ }
59
+ } ,
51
60
}
52
61
} )
53
62
. collect ( )
Original file line number Diff line number Diff line change @@ -45,6 +45,8 @@ pub struct GraphQLClientCodegenOptions {
45
45
extern_enums : Vec < String > ,
46
46
/// Flag to trigger generation of Other variant for fragments Enum
47
47
fragments_other_variant : bool ,
48
+ /// Skip Serialization of None values.
49
+ skip_none : bool ,
48
50
}
49
51
50
52
impl GraphQLClientCodegenOptions {
@@ -65,6 +67,7 @@ impl GraphQLClientCodegenOptions {
65
67
custom_scalars_module : Default :: default ( ) ,
66
68
extern_enums : Default :: default ( ) ,
67
69
fragments_other_variant : Default :: default ( ) ,
70
+ skip_none : Default :: default ( ) ,
68
71
}
69
72
}
70
73
@@ -214,4 +217,14 @@ impl GraphQLClientCodegenOptions {
214
217
pub fn fragments_other_variant ( & self ) -> & bool {
215
218
& self . fragments_other_variant
216
219
}
220
+
221
+ /// Set the graphql client codegen option's skip none value.
222
+ pub fn set_skip_none ( & mut self , skip_none : bool ) {
223
+ self . skip_none = skip_none
224
+ }
225
+
226
+ /// Get a reference to the graphql client codegen option's skip none value.
227
+ pub fn skip_none ( & self ) -> & bool {
228
+ & self . skip_none
229
+ }
217
230
}
Original file line number Diff line number Diff line change @@ -103,6 +103,13 @@ pub fn extract_fragments_other_variant(ast: &syn::DeriveInput) -> bool {
103
103
. unwrap_or ( false )
104
104
}
105
105
106
+ pub fn extract_skip_none ( ast : & syn:: DeriveInput ) -> bool {
107
+ extract_attr ( ast, "skip_none" )
108
+ . ok ( )
109
+ . and_then ( |s| FromStr :: from_str ( s. as_str ( ) ) . ok ( ) )
110
+ . unwrap_or ( false )
111
+ }
112
+
106
113
#[ cfg( test) ]
107
114
mod test {
108
115
use super :: * ;
@@ -219,4 +226,63 @@ mod test {
219
226
let parsed = syn:: parse_str ( input) . unwrap ( ) ;
220
227
assert ! ( !extract_fragments_other_variant( & parsed) ) ;
221
228
}
229
+
230
+ #[ test]
231
+ fn test_skip_none_set_to_true ( ) {
232
+ let input = r#"
233
+ #[derive(GraphQLQuery)]
234
+ #[graphql(
235
+ schema_path = "x",
236
+ query_path = "x",
237
+ skip_none = "true"
238
+ )]
239
+ struct MyQuery;
240
+ "# ;
241
+ let parsed = syn:: parse_str ( input) . unwrap ( ) ;
242
+ assert ! ( extract_skip_none( & parsed) ) ;
243
+ }
244
+
245
+ #[ test]
246
+ fn test_skip_none_set_to_false ( ) {
247
+ let input = r#"
248
+ #[derive(GraphQLQuery)]
249
+ #[graphql(
250
+ schema_path = "x",
251
+ query_path = "x",
252
+ skip_none = "false"
253
+ )]
254
+ struct MyQuery;
255
+ "# ;
256
+ let parsed = syn:: parse_str ( input) . unwrap ( ) ;
257
+ assert ! ( !extract_skip_none( & parsed) ) ;
258
+ }
259
+
260
+ #[ test]
261
+ fn test_skip_none_set_to_invalid ( ) {
262
+ let input = r#"
263
+ #[derive(GraphQLQuery)]
264
+ #[graphql(
265
+ schema_path = "x",
266
+ query_path = "x",
267
+ skip_none = "invalid"
268
+ )]
269
+ struct MyQuery;
270
+ "# ;
271
+ let parsed = syn:: parse_str ( input) . unwrap ( ) ;
272
+ assert ! ( !extract_skip_none( & parsed) ) ;
273
+ }
274
+
275
+ #[ test]
276
+ fn test_skip_none_unset ( ) {
277
+ let input = r#"
278
+ #[derive(GraphQLQuery)]
279
+ #[graphql(
280
+ schema_path = "x",
281
+ query_path = "x",
282
+ )]
283
+ struct MyQuery;
284
+ "# ;
285
+ let parsed = syn:: parse_str ( input) . unwrap ( ) ;
286
+ assert ! ( !extract_skip_none( & parsed) ) ;
287
+ }
222
288
}
Original file line number Diff line number Diff line change @@ -64,10 +64,12 @@ fn build_graphql_client_derive_options(
64
64
let custom_scalars_module = attributes:: extract_attr ( input, "custom_scalars_module" ) . ok ( ) ;
65
65
let extern_enums = attributes:: extract_attr_list ( input, "extern_enums" ) . ok ( ) ;
66
66
let fragments_other_variant: bool = attributes:: extract_fragments_other_variant ( input) ;
67
+ let skip_none: bool = attributes:: extract_skip_none ( input) ;
67
68
68
69
let mut options = GraphQLClientCodegenOptions :: new ( CodegenMode :: Derive ) ;
69
70
options. set_query_file ( query_path) ;
70
71
options. set_fragments_other_variant ( fragments_other_variant) ;
72
+ options. set_skip_none ( skip_none) ;
71
73
72
74
if let Some ( variables_derives) = variables_derives {
73
75
options. set_variables_derives ( variables_derives) ;
You can’t perform that action at this time.
0 commit comments