aws / aws/aws-appsync-community

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

Open
#279 1 comment 0 reactions 0 assignees View on GitHub
bug
Dominant language
HTML
Stars
507
Forks
37
PR merge metrics
No merged PRs in 30d

Description

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`):

```js
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:

```js
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}` };
}

// ...
}
```

Contributor guide

Open the contributing guide

Research direction

The issue provides two AppSync JavaScript resolver variants but names no repository files or tests. Reproduce both snippets as AppSync resolvers and compare deployment results; done means identifying and documenting or correcting the behavior so the intended conditional DynamoDB query deploys successfully.

Written by the indexing model from the issue text.

Assessment

Tech stack
aws, graphql, javascript
Domain
api, backend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.