From 118f29f1eb0b7ec56f47144d9600b20f6d4348af Mon Sep 17 00:00:00 2001 From: Elijah Mirecki Date: Sun, 19 May 2024 17:37:13 -0300 Subject: [PATCH 1/3] refs #439. Fix skip_serializing_none for root level variables --- graphql_client/tests/skip_serializing_none.rs | 16 +++++++++++++++- .../tests/skip_serializing_none/query.graphql | 2 +- graphql_client_codegen/src/codegen.rs | 13 +++++++++++-- 3 files changed, 27 insertions(+), 4 deletions(-) diff --git a/graphql_client/tests/skip_serializing_none.rs b/graphql_client/tests/skip_serializing_none.rs index 0d261847..b5edc2fd 100644 --- a/graphql_client/tests/skip_serializing_none.rs +++ b/graphql_client/tests/skip_serializing_none.rs @@ -13,6 +13,7 @@ fn skip_serializing_none() { use skip_serializing_none_mutation::*; let query = SkipSerializingNoneMutation::build_query(Variables { + foo: None, param: Some(Param { data: Author { name: "test".to_owned(), @@ -25,5 +26,18 @@ fn skip_serializing_none() { println!("{}", stringified); - assert!(stringified.contains(r#""data":{"name":"test"}"#)); + assert!(stringified.contains(r#""variables":{"param":{"data":{"name":"test"}}}"#)); + + let query = SkipSerializingNoneMutation::build_query(Variables { + foo: Some(42), + param: Some(Param { + data: Author { + name: "test".to_owned(), + id: None, + }, + }), + }); + let stringified = serde_json::to_string(&query).expect("SkipSerializingNoneMutation is valid"); + println!("{}", stringified); + assert!(stringified.contains(r#""variables":{"param":{"data":{"name":"test"}},"foo":42}"#)); } diff --git a/graphql_client/tests/skip_serializing_none/query.graphql b/graphql_client/tests/skip_serializing_none/query.graphql index 028ae768..f6483000 100644 --- a/graphql_client/tests/skip_serializing_none/query.graphql +++ b/graphql_client/tests/skip_serializing_none/query.graphql @@ -1,4 +1,4 @@ -mutation SkipSerializingNoneMutation($param: Param) { +mutation SkipSerializingNoneMutation($param: Param, $foo: Int) { optInput(query: $param) { name __typename diff --git a/graphql_client_codegen/src/codegen.rs b/graphql_client_codegen/src/codegen.rs index f3ad1a94..3ea1235e 100644 --- a/graphql_client_codegen/src/codegen.rs +++ b/graphql_client_codegen/src/codegen.rs @@ -135,10 +135,19 @@ fn generate_variable_struct_field( let snake_case_name = variable.name.to_snake_case(); let safe_name = shared::keyword_replace(&snake_case_name); let ident = Ident::new(&safe_name, Span::call_site()); - let annotation = shared::field_rename_annotation(&variable.name, &safe_name); + let rename_annotation = shared::field_rename_annotation(&variable.name, &safe_name); + let skip_serializing_annotation = if *options.skip_serializing_none() { + if variable.r#type.qualifiers.first() != Some(&GraphqlTypeQualifier::Required) { + Some(quote!(#[serde(skip_serializing_if = "Option::is_none")])) + } else { + None + } + } else { + None + }; let r#type = render_variable_field_type(variable, options, query); - quote::quote!(#annotation pub #ident : #r#type) + quote::quote!(#skip_serializing_annotation #rename_annotation pub #ident : #r#type) } fn generate_scalar_definitions<'a, 'schema: 'a>( From 57fe26a80506ef26394d87adfe78f0271b3c089d Mon Sep 17 00:00:00 2001 From: Elijah Mirecki Date: Sun, 19 May 2024 17:55:38 -0300 Subject: [PATCH 2/3] refs #485. Update test case to cover all combinations of list/required --- graphql_client/tests/skip_serializing_none.rs | 22 +++++++++++++++---- .../tests/skip_serializing_none/query.graphql | 2 +- 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/graphql_client/tests/skip_serializing_none.rs b/graphql_client/tests/skip_serializing_none.rs index b5edc2fd..84fdc519 100644 --- a/graphql_client/tests/skip_serializing_none.rs +++ b/graphql_client/tests/skip_serializing_none.rs @@ -13,7 +13,10 @@ fn skip_serializing_none() { use skip_serializing_none_mutation::*; let query = SkipSerializingNoneMutation::build_query(Variables { - foo: None, + optional_int: None, + optional_list: None, + non_optional_int: 1337, + non_optional_list: vec![], param: Some(Param { data: Author { name: "test".to_owned(), @@ -26,10 +29,17 @@ fn skip_serializing_none() { println!("{}", stringified); - assert!(stringified.contains(r#""variables":{"param":{"data":{"name":"test"}}}"#)); + assert!(stringified.contains(r#""param":{"data":{"name":"test"}}"#)); + assert!(stringified.contains(r#""nonOptionalInt":1337"#)); + assert!(stringified.contains(r#""nonOptionalList":[]"#)); + assert!(!stringified.contains(r#""optionalInt""#)); + assert!(!stringified.contains(r#""optionalLint""#)); let query = SkipSerializingNoneMutation::build_query(Variables { - foo: Some(42), + optional_int: Some(42), + optional_list: Some(vec![]), + non_optional_int: 1337, + non_optional_list: vec![], param: Some(Param { data: Author { name: "test".to_owned(), @@ -39,5 +49,9 @@ fn skip_serializing_none() { }); let stringified = serde_json::to_string(&query).expect("SkipSerializingNoneMutation is valid"); println!("{}", stringified); - assert!(stringified.contains(r#""variables":{"param":{"data":{"name":"test"}},"foo":42}"#)); + assert!(stringified.contains(r#""param":{"data":{"name":"test"}}"#)); + assert!(stringified.contains(r#""nonOptionalInt":1337"#)); + assert!(stringified.contains(r#""nonOptionalList":[]"#)); + assert!(stringified.contains(r#""optionalInt":42"#)); + assert!(stringified.contains(r#""optionalList":[]"#)); } diff --git a/graphql_client/tests/skip_serializing_none/query.graphql b/graphql_client/tests/skip_serializing_none/query.graphql index f6483000..a91b798f 100644 --- a/graphql_client/tests/skip_serializing_none/query.graphql +++ b/graphql_client/tests/skip_serializing_none/query.graphql @@ -1,4 +1,4 @@ -mutation SkipSerializingNoneMutation($param: Param, $foo: Int) { +mutation SkipSerializingNoneMutation($param: Param, $optionalInt: Int, $optionalList: [Int!], $nonOptionalInt: Int!, $nonOptionalList: [Int!]!) { optInput(query: $param) { name __typename From f90619cd14ef0ab3d75d37e6155357be5d31adfa Mon Sep 17 00:00:00 2001 From: Elijah Mirecki Date: Sun, 19 May 2024 17:57:18 -0300 Subject: [PATCH 3/3] refs #439. Fix type in skip_serailizing_none.rs test --- graphql_client/tests/skip_serializing_none.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/graphql_client/tests/skip_serializing_none.rs b/graphql_client/tests/skip_serializing_none.rs index 84fdc519..bf177da4 100644 --- a/graphql_client/tests/skip_serializing_none.rs +++ b/graphql_client/tests/skip_serializing_none.rs @@ -33,7 +33,7 @@ fn skip_serializing_none() { assert!(stringified.contains(r#""nonOptionalInt":1337"#)); assert!(stringified.contains(r#""nonOptionalList":[]"#)); assert!(!stringified.contains(r#""optionalInt""#)); - assert!(!stringified.contains(r#""optionalLint""#)); + assert!(!stringified.contains(r#""optionalList""#)); let query = SkipSerializingNoneMutation::build_query(Variables { optional_int: Some(42),