File tree Expand file tree Collapse file tree 3 files changed +27
-4
lines changed
graphql_client_codegen/src Expand file tree Collapse file tree 3 files changed +27
-4
lines changed Original file line number Diff line number Diff line change @@ -13,6 +13,7 @@ fn skip_serializing_none() {
1313 use skip_serializing_none_mutation:: * ;
1414
1515 let query = SkipSerializingNoneMutation :: build_query ( Variables {
16+ foo : None ,
1617 param : Some ( Param {
1718 data : Author {
1819 name : "test" . to_owned ( ) ,
@@ -25,5 +26,18 @@ fn skip_serializing_none() {
2526
2627 println ! ( "{}" , stringified) ;
2728
28- assert ! ( stringified. contains( r#""data":{"name":"test"}"# ) ) ;
29+ assert ! ( stringified. contains( r#""variables":{"param":{"data":{"name":"test"}}}"# ) ) ;
30+
31+ let query = SkipSerializingNoneMutation :: build_query ( Variables {
32+ foo : Some ( 42 ) ,
33+ param : Some ( Param {
34+ data : Author {
35+ name : "test" . to_owned ( ) ,
36+ id : None ,
37+ } ,
38+ } ) ,
39+ } ) ;
40+ let stringified = serde_json:: to_string ( & query) . expect ( "SkipSerializingNoneMutation is valid" ) ;
41+ println ! ( "{}" , stringified) ;
42+ assert ! ( stringified. contains( r#""variables":{"param":{"data":{"name":"test"}},"foo":42}"# ) ) ;
2943}
Original file line number Diff line number Diff line change 1- mutation SkipSerializingNoneMutation ($param : Param ) {
1+ mutation SkipSerializingNoneMutation ($param : Param , $foo : Int ) {
22 optInput (query : $param ) {
33 name
44 __typename
Original file line number Diff line number Diff line change @@ -135,10 +135,19 @@ fn generate_variable_struct_field(
135135 let snake_case_name = variable. name . to_snake_case ( ) ;
136136 let safe_name = shared:: keyword_replace ( & snake_case_name) ;
137137 let ident = Ident :: new ( & safe_name, Span :: call_site ( ) ) ;
138- let annotation = shared:: field_rename_annotation ( & variable. name , & safe_name) ;
138+ let rename_annotation = shared:: field_rename_annotation ( & variable. name , & safe_name) ;
139+ let skip_serializing_annotation = if * options. skip_serializing_none ( ) {
140+ if variable. r#type . qualifiers . first ( ) != Some ( & GraphqlTypeQualifier :: Required ) {
141+ Some ( quote ! ( #[ serde( skip_serializing_if = "Option::is_none" ) ] ) )
142+ } else {
143+ None
144+ }
145+ } else {
146+ None
147+ } ;
139148 let r#type = render_variable_field_type ( variable, options, query) ;
140149
141- quote:: quote!( #annotation pub #ident : #r#type)
150+ quote:: quote!( #skip_serializing_annotation #rename_annotation pub #ident : #r#type)
142151}
143152
144153fn generate_scalar_definitions < ' a , ' schema : ' a > (
You can’t perform that action at this time.
0 commit comments