Skip to content

Commit ed98d33

Browse files
committed
Don't require skip_serializing_none to have any attribute set and add end-to-end test for it.
1 parent 942373b commit ed98d33

File tree

5 files changed

+95
-42
lines changed

5 files changed

+95
-42
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
use graphql_client::*;
2+
3+
#[derive(GraphQLQuery)]
4+
#[graphql(
5+
schema_path = "tests/skip_serializing_none/schema.graphql",
6+
query_path = "tests/skip_serializing_none/query.graphql",
7+
skip_serializing_none
8+
)]
9+
pub struct SkipSerializingNoneMutation;
10+
11+
#[test]
12+
fn skip_serializing_none() {
13+
use skip_serializing_none_mutation::*;
14+
15+
let query = SkipSerializingNoneMutation::build_query(Variables {
16+
param: Some(Param {
17+
data: Author {
18+
name: "test".to_owned(),
19+
id: None,
20+
},
21+
}),
22+
});
23+
24+
let stringified = serde_json::to_string(&query).expect("SkipSerializingNoneMutation is valid");
25+
26+
println!("{}", stringified);
27+
28+
assert!(stringified.contains(r#""data":{"name":"test"}"#));
29+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
mutation SkipSerializingNoneMutation($param: Param) {
2+
optInput(query: $param) {
3+
name
4+
__typename
5+
}
6+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
schema {
2+
mutation: Mutation
3+
}
4+
5+
# The query type, represents all of the entry points into our object graph
6+
type Mutation {
7+
optInput(mutation: Param!): Named
8+
}
9+
10+
input Param {
11+
data: Author!
12+
}
13+
14+
input Author {
15+
id: String,
16+
name: String!
17+
}
18+
19+
# A named entity
20+
type Named {
21+
# The ID of the entity
22+
id: ID!
23+
# The name of the entity
24+
name: String!
25+
}

graphql_client_codegen/src/codegen/inputs.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ use crate::{
33
codegen_options::GraphQLClientCodegenOptions,
44
query::{BoundQuery, UsedTypes},
55
schema::input_is_recursive_without_indirection,
6-
type_qualifiers::GraphqlTypeQualifier,
76
};
87
use heck::ToSnakeCase;
98
use proc_macro2::{Ident, Span, TokenStream};
@@ -29,11 +28,12 @@ pub(super) fn generate_input_object_definitions(
2928
let normalized_field_type_name = options
3029
.normalization()
3130
.field_type(field_type.id.name(query.schema));
32-
let optional_skip_serializing_none = if *options.skip_serializing_none() && field_type.is_optional() {
33-
Some(quote!(#[serde(skip_serializing_if = "Option::is_none")]))
34-
} else {
35-
None
36-
};
31+
let optional_skip_serializing_none =
32+
if *options.skip_serializing_none() && field_type.is_optional() {
33+
Some(quote!(#[serde(skip_serializing_if = "Option::is_none")]))
34+
} else {
35+
None
36+
};
3737
let type_name = Ident::new(normalized_field_type_name.as_ref(), Span::call_site());
3838
let field_type_tokens = super::decorate_type(&type_name, &field_type.qualifiers);
3939
let field_type = if field_type

graphql_query_derive/src/attributes.rs

Lines changed: 29 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,32 @@ fn path_to_match() -> syn::Path {
1111
syn::parse_str("graphql").expect("`graphql` is a valid path")
1212
}
1313

14+
pub fn ident_exists(ast: &syn::DeriveInput, ident: &str) -> Result<(), syn::Error> {
15+
let graphql_path = path_to_match();
16+
let attribute = ast
17+
.attrs
18+
.iter()
19+
.find(|attr| attr.path == graphql_path)
20+
.ok_or_else(|| syn::Error::new_spanned(ast, "The graphql attribute is missing"))?;
21+
22+
if let syn::Meta::List(items) = &attribute.parse_meta().expect("Attribute is well formatted") {
23+
for item in items.nested.iter() {
24+
if let syn::NestedMeta::Meta(syn::Meta::Path(path)) = item {
25+
if let Some(ident_) = path.get_ident() {
26+
if ident_ == ident {
27+
return Ok(());
28+
}
29+
}
30+
}
31+
}
32+
}
33+
34+
Err(syn::Error::new_spanned(
35+
&ast,
36+
format!("Ident `{}` not found", ident),
37+
))
38+
}
39+
1440
/// Extract an configuration parameter specified in the `graphql` attribute.
1541
pub fn extract_attr(ast: &syn::DeriveInput, attr: &str) -> Result<String, syn::Error> {
1642
let attributes = &ast.attrs;
@@ -104,10 +130,7 @@ pub fn extract_fragments_other_variant(ast: &syn::DeriveInput) -> bool {
104130
}
105131

106132
pub fn extract_skip_serializing_none(ast: &syn::DeriveInput) -> bool {
107-
extract_attr(ast, "skip_serializing_none")
108-
.ok()
109-
.and_then(|s| FromStr::from_str(s.as_str()).ok())
110-
.unwrap_or(false)
133+
ident_exists(ast, "skip_serializing_none").is_ok()
111134
}
112135

113136
#[cfg(test)]
@@ -228,50 +251,20 @@ mod test {
228251
}
229252

230253
#[test]
231-
fn test_skip_serializing_none_set_to_true() {
254+
fn test_skip_serializing_none_set() {
232255
let input = r#"
233256
#[derive(GraphQLQuery)]
234257
#[graphql(
235258
schema_path = "x",
236259
query_path = "x",
237-
skip_serializing_none = "true"
260+
skip_serializing_none
238261
)]
239262
struct MyQuery;
240263
"#;
241264
let parsed = syn::parse_str(input).unwrap();
242265
assert!(extract_skip_serializing_none(&parsed));
243266
}
244267

245-
#[test]
246-
fn test_skip_serializing_none_set_to_false() {
247-
let input = r#"
248-
#[derive(GraphQLQuery)]
249-
#[graphql(
250-
schema_path = "x",
251-
query_path = "x",
252-
skip_serializing_none = "false"
253-
)]
254-
struct MyQuery;
255-
"#;
256-
let parsed = syn::parse_str(input).unwrap();
257-
assert!(!extract_skip_serializing_none(&parsed));
258-
}
259-
260-
#[test]
261-
fn test_skip_serializing_none_set_to_invalid() {
262-
let input = r#"
263-
#[derive(GraphQLQuery)]
264-
#[graphql(
265-
schema_path = "x",
266-
query_path = "x",
267-
skip_serializing_none = "invalid"
268-
)]
269-
struct MyQuery;
270-
"#;
271-
let parsed = syn::parse_str(input).unwrap();
272-
assert!(!extract_skip_serializing_none(&parsed));
273-
}
274-
275268
#[test]
276269
fn test_skip_serializing_none_unset() {
277270
let input = r#"

0 commit comments

Comments
 (0)