Skip to content

Commit a79be89

Browse files
committed
Add skip_none to codegen and query_derive
1 parent 45faf4a commit a79be89

File tree

4 files changed

+95
-5
lines changed

4 files changed

+95
-5
lines changed

graphql_client_codegen/src/codegen/inputs.rs

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,20 @@ pub(super) fn generate_input_object_definitions(
4343
quote!(#annotation pub #name_ident: #field_type)
4444
});
4545

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+
},
5160
}
5261
})
5362
.collect()

graphql_client_codegen/src/codegen_options.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ pub struct GraphQLClientCodegenOptions {
4545
extern_enums: Vec<String>,
4646
/// Flag to trigger generation of Other variant for fragments Enum
4747
fragments_other_variant: bool,
48+
/// Skip Serialization of None values.
49+
skip_none: bool,
4850
}
4951

5052
impl GraphQLClientCodegenOptions {
@@ -65,6 +67,7 @@ impl GraphQLClientCodegenOptions {
6567
custom_scalars_module: Default::default(),
6668
extern_enums: Default::default(),
6769
fragments_other_variant: Default::default(),
70+
skip_none: Default::default(),
6871
}
6972
}
7073

@@ -214,4 +217,14 @@ impl GraphQLClientCodegenOptions {
214217
pub fn fragments_other_variant(&self) -> &bool {
215218
&self.fragments_other_variant
216219
}
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+
}
217230
}

graphql_query_derive/src/attributes.rs

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,13 @@ pub fn extract_fragments_other_variant(ast: &syn::DeriveInput) -> bool {
103103
.unwrap_or(false)
104104
}
105105

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+
106113
#[cfg(test)]
107114
mod test {
108115
use super::*;
@@ -219,4 +226,63 @@ mod test {
219226
let parsed = syn::parse_str(input).unwrap();
220227
assert!(!extract_fragments_other_variant(&parsed));
221228
}
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+
}
222288
}

graphql_query_derive/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,12 @@ fn build_graphql_client_derive_options(
6464
let custom_scalars_module = attributes::extract_attr(input, "custom_scalars_module").ok();
6565
let extern_enums = attributes::extract_attr_list(input, "extern_enums").ok();
6666
let fragments_other_variant: bool = attributes::extract_fragments_other_variant(input);
67+
let skip_none: bool = attributes::extract_skip_none(input);
6768

6869
let mut options = GraphQLClientCodegenOptions::new(CodegenMode::Derive);
6970
options.set_query_file(query_path);
7071
options.set_fragments_other_variant(fragments_other_variant);
72+
options.set_skip_none(skip_none);
7173

7274
if let Some(variables_derives) = variables_derives {
7375
options.set_variables_derives(variables_derives);

0 commit comments

Comments
 (0)