Skip to content

Commit dcdd2c4

Browse files
committed
BREAKING/Feat: The 'input' types of the GraphQL schema now also get the serde::Serialize derivation instead of only having Deserialize. The original choice was poor, based on the illusion that input only needs to be deser; but indeed it also need to be ser when writting a client lib for the GraphQL API.
1 parent bfcbb2f commit dcdd2c4

2 files changed

Lines changed: 13 additions & 32 deletions

File tree

.hooks/pre-commit

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,21 +20,28 @@ git stash push -q --include-untracked --keep-index
2020

2121
# Run checks in a subshell to ensure we always restore the working tree
2222
if (
23+
total_errors=0
2324
# Check formatting only on staged Rust files
2425
echo "Checking formatting..."
2526
cargo fmt --check
27+
((total_errors += $?))
2628

2729
# Run clippy
2830
echo "Running clippy..."
2931
cargo clippy --all-targets --all-features -- -D warnings
32+
((total_errors += $?))
3033

3134
# Check documentation for modified files
3235
echo "Checking documentation..."
3336
RUSTDOCFLAGS="-D warnings" cargo doc --all-features --no-deps --document-private-items
37+
((total_errors += $?))
3438

3539
# Run tests that could be affected by the changes
3640
echo "Running tests..."
3741
cargo test --all-features
42+
((total_errors += $?))
43+
44+
[ $total_errors -eq 0 ]
3845
) ; then
3946
CHECK_STATUS=0
4047
else

lambda-appsync-proc/src/appsync_lambda_main/graphql.rs

Lines changed: 6 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -172,14 +172,10 @@ impl From<graphql_parser::schema::InputValue<'_, String>> for Field {
172172

173173
struct FieldContext<'a> {
174174
field: &'a Field,
175-
deserialize_only: bool,
176175
}
177176
impl<'a> FieldContext<'a> {
178-
fn new(field: &'a Field, deserialize_only: bool) -> Self {
179-
Self {
180-
field,
181-
deserialize_only,
182-
}
177+
fn new(field: &'a Field) -> Self {
178+
Self { field }
183179
}
184180
}
185181
impl ToTokens for FieldContext<'_> {
@@ -201,11 +197,6 @@ impl ToTokens for FieldContext<'_> {
201197
serde_options.push(quote_spanned! {span=>
202198
default
203199
});
204-
if !self.deserialize_only {
205-
serde_options.push(quote_spanned! {span=>
206-
skip_serializing_if = "Option::is_none"
207-
});
208-
}
209200
}
210201
if !serde_options.is_empty() {
211202
tokens.extend(quote_spanned! {span=>
@@ -221,7 +212,6 @@ impl ToTokens for FieldContext<'_> {
221212
struct Structure {
222213
name: Name,
223214
fields: Vec<Field>,
224-
deserialize_only: bool,
225215
}
226216
impl Structure {
227217
fn apply_type_overrides(
@@ -315,39 +305,23 @@ impl From<graphql_parser::schema::ObjectType<'_, String>> for Structure {
315305
fn from(value: graphql_parser::schema::ObjectType<'_, String>) -> Self {
316306
let name = Name::from((value.name, current_span()));
317307
let fields = value.fields.into_iter().map(Field::from).collect();
318-
Self {
319-
name,
320-
fields,
321-
deserialize_only: false,
322-
}
308+
Self { name, fields }
323309
}
324310
}
325311
impl From<graphql_parser::schema::InputObjectType<'_, String>> for Structure {
326312
fn from(value: graphql_parser::schema::InputObjectType<'_, String>) -> Self {
327313
let name = Name::from(value.name);
328314
let fields = value.fields.into_iter().map(Field::from).collect();
329-
Self {
330-
name,
331-
fields,
332-
deserialize_only: true,
333-
}
315+
Self { name, fields }
334316
}
335317
}
336318
impl ToTokens for Structure {
337319
fn to_tokens(&self, tokens: &mut proc_macro2::TokenStream) {
338320
let span = current_span();
339321
let struct_name = self.name.to_type_ident();
340-
let fields = self
341-
.fields
342-
.iter()
343-
.map(|f| FieldContext::new(f, self.deserialize_only));
344-
let serde_derive = if self.deserialize_only {
345-
quote_spanned! {span=>::lambda_appsync::serde::Deserialize}
346-
} else {
347-
quote_spanned! {span=>::lambda_appsync::serde::Serialize, ::lambda_appsync::serde::Deserialize}
348-
};
322+
let fields = self.fields.iter().map(FieldContext::new);
349323
tokens.extend(quote_spanned! {span=>
350-
#[derive(Debug, Clone, #serde_derive)]
324+
#[derive(Debug, Clone, ::lambda_appsync::serde::Serialize, ::lambda_appsync::serde::Deserialize)]
351325
pub struct #struct_name {
352326
#(#fields,)*
353327
}

0 commit comments

Comments
 (0)