aws / aws-appsync-community

The AWS AppSync community
https://aws.amazon.com/appsync
Apache License 2.0
506 stars 32 forks source link

[Bug] Error in JavaScript Resolver with default initialization using the 'let' keyword #279

Open danrivett opened 1 year ago

danrivett commented 1 year ago

In an AppSync JavaScript resolver there is a strange bug where if I use the let keyword to define a default value and then conditonally change it, deployment fails (with a BadRequestException):

export function request(ctx) {
  const { productCode } = ctx.args;

  // We can look at the fields selected in the query and scope our DynamoDB query narrower if items are not requested
  const { selectionSetList } = ctx.info;

  // Default to the wider query (using ge) including items
  let SK = { ge: `#PRODUCT#${productCode}` };

  // And only narrow (using eq) to exclude items if the selectionSetList explicitly doesn't include itemSummaries
  if (selectionSetList != null && !selectionSetList.includes('items')) {
    SK = { eq: `#PRODUCT#${productCode}` };
  }

  // ...
}

Whereas if I use an if...else and avoid default initialization, it succeeds:

export function request(ctx) {
  const { productCode } = ctx.args;

  // We can look at the fields selected in the query and scope our DynamoDB query narrower if items are not requested
  const { selectionSetList } = ctx.info;

  let SK;

  // Only narrow (using eq) to exclude items if the selectionSetList explicitly doesn't include itemSummaries
  if (selectionSetList != null && !selectionSetList.includes('items')) {
    SK = { eq: `#PRODUCT#${productCode}` };
  } else {
    // Otherwise default to the wider query (using ge) including items
    SK = { ge: `#PRODUCT#${productCode}` };
  }

  // ...
}
gilbertw1 commented 10 months ago

Thanks for reporting this. We are currently investigating the issue and will follow up when we have an update.