Skip to content

Commit 71ade42

Browse files
committed
Feat: Adding 'error_logging: bool (default: true)' optional parameter on the make_operation macro, when the 'log' feature is enabled. The parameter governs weither or not the execute_operation method should log::error!({e}) on a AppsyncError response before turning it into the opaque AppsyncResponse type
1 parent 4a43ad2 commit 71ade42

2 files changed

Lines changed: 42 additions & 12 deletions

File tree

lambda-appsync-proc/src/internal/make_appsync/graphql.rs

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ use quote::{quote, quote_spanned, ToTokens};
66
use syn::Path;
77
use syn::{spanned::Spanned, LitStr};
88

9+
use crate::internal::make_appsync::MakeOperationParameters;
10+
911
use super::super::common::{Name, OperationKind};
1012
use super::overrides::{
1113
FieldTypeOverride, FieldTypeOverrides, OverrideParameters, TypeNameOverride, TypeOverride,
@@ -776,12 +778,13 @@ pub(super) struct GraphQLSchema {
776778
subscriptions: Operations,
777779
structures: Vec<Structure>,
778780
enums: Vec<Enum>,
781+
make_operation_parameters: Option<MakeOperationParameters>,
779782
}
780783
impl GraphQLSchema {
781784
pub(super) fn new(
782785
graphql_schema_path: LitStr,
783786
override_parameters: OverrideParameters,
784-
custom_type_module: Option<Path>,
787+
make_operation_parameters: Option<MakeOperationParameters>,
785788
) -> Result<Self, syn::Error> {
786789
let mut queries = None;
787790
let mut mutations = None;
@@ -853,7 +856,11 @@ impl GraphQLSchema {
853856
// This is an Object defining operations
854857
let type_overrides = type_overrides.remove(&object_type.name);
855858
let mut ops = Operations::from(object_type);
856-
if let Some(ref custom_type_module) = custom_type_module {
859+
if let Some(MakeOperationParameters {
860+
type_module: Some(ref custom_type_module),
861+
..
862+
}) = make_operation_parameters
863+
{
857864
ops.apply_type_module_path(custom_type_module);
858865
}
859866
if let Some(type_overrides) = type_overrides {
@@ -992,6 +999,7 @@ impl GraphQLSchema {
992999
subscriptions: subscriptions.unwrap_or_default(),
9931000
structures,
9941001
enums,
1002+
make_operation_parameters,
9951003
})
9961004
} else {
9971005
Err(errors
@@ -1082,9 +1090,15 @@ impl GraphQLSchema {
10821090
#[allow(unused_mut)]
10831091
let mut log_lines = proc_macro2::TokenStream::new();
10841092
#[cfg(feature = "log")]
1085-
log_lines.extend(quote_spanned! {span=>
1086-
::lambda_appsync::log::error!("{e}");
1087-
});
1093+
if self
1094+
.make_operation_parameters
1095+
.as_ref()
1096+
.is_some_and(|p| p.error_logging)
1097+
{
1098+
log_lines.extend(quote_spanned! {span=>
1099+
::lambda_appsync::log::error!("{e}");
1100+
});
1101+
}
10881102

10891103
tokens.extend(quote_spanned! {span=>
10901104
impl Operation {

lambda-appsync-proc/src/internal/make_appsync/make_operation.rs

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
use proc_macro2::TokenStream as TokenStream2;
22
use quote::ToTokens;
3+
#[cfg(feature = "log")]
4+
use syn::LitBool;
35
use syn::{
46
parse::{Parse, ParseStream},
57
parse_macro_input, LitStr, Path, Result, Token,
@@ -12,27 +14,44 @@ use super::{
1214

1315
pub(super) enum MakeOperationParameter {
1416
TypeModule(Path),
17+
#[cfg(feature = "log")]
18+
ErrorLogging(bool),
1519
}
1620
impl OptionalParameter for MakeOperationParameter {
1721
fn try_parse_parameter(input: ParseStream) -> core::result::Result<Self, ParameterError> {
1822
let ident = Self::parse_ident(input)?;
1923
#[allow(clippy::match_single_binding)]
2024
match ident.to_string().as_str() {
2125
"type_module" => Ok(Self::TypeModule(input.parse()?)),
26+
#[cfg(feature = "log")]
27+
"error_logging" => Ok(Self::ErrorLogging(input.parse::<LitBool>()?.value())),
2228
// Unknown option
2329
_ => ident.unknown(),
2430
}
2531
}
2632
}
2733

28-
#[derive(Default)]
2934
pub(super) struct MakeOperationParameters {
30-
type_module: Option<Path>,
35+
pub(super) type_module: Option<Path>,
36+
#[cfg(feature = "log")]
37+
pub(super) error_logging: bool,
38+
}
39+
impl Default for MakeOperationParameters {
40+
fn default() -> Self {
41+
Self {
42+
type_module: None,
43+
error_logging: true,
44+
}
45+
}
3146
}
3247
impl OptionalParameters<MakeOperationParameter> for MakeOperationParameters {
3348
fn set_param(&mut self, p: MakeOperationParameter) {
3449
match p {
3550
MakeOperationParameter::TypeModule(path) => self.type_module = Some(path),
51+
#[cfg(feature = "log")]
52+
MakeOperationParameter::ErrorLogging(error_logging) => {
53+
self.error_logging = error_logging
54+
}
3655
}
3756
}
3857
}
@@ -69,11 +88,8 @@ impl Parse for MakeOperation {
6988
parameters.try_parse_parameter(input)?;
7089
}
7190

72-
let graphql_schema = GraphQLSchema::new(
73-
graphql_schema_path,
74-
override_parameters,
75-
parameters.type_module,
76-
)?;
91+
let graphql_schema =
92+
GraphQLSchema::new(graphql_schema_path, override_parameters, Some(parameters))?;
7793

7894
Ok(Self { graphql_schema })
7995
}

0 commit comments

Comments
 (0)