amplify-category-api icon indicating copy to clipboard operation
amplify-category-api copied to clipboard

The implementation of the nullish coalescing operator (??) in AppSync_JS resolvers is not consistent with JS (Gen2)

Open naedx opened this issue 8 months ago • 4 comments

How did you install the Amplify CLI?

npm

If applicable, what version of Node.js are you using?

v20.9.0

Amplify CLI Version

1.4.9

What operating system are you using?

Mac

Did you make any manual changes to the cloud resources managed by Amplify? Please describe the changes made.

No manual changes made

Describe the bug

The ?? operator triggers the right-hand expression even when the left-hand expression is not nullish.

Expected behavior

The behavior should be consistent with JS or disallowed via a lint rule and the syntax checker in the AWS Console.

Reproduction steps

  1. Define the following resolver in the Amplify console.
  2. Create the default test context and run the test.
  3. Observe that the request and response resolvers return different results.
  4. (Optional) Uncomment return data.foo ?? "some other value" and observe that it works as expected

// APPSYNC_JS

import { util } from "@aws-appsync/utils";

const data = {foo: "bar"};
  
export function request(ctx) {
  // return data.foo ?? "some other value";  // << returns ""some other value" (when uncommented)
  return data.foo ?? error('Error occurred') ; // << returns "Error occurred" 
}

export function response(ctx) {
  return data.foo ? data.foo : error('Error occurred') ; // << returns "bar"
}

function error(msg) {
  return util.error(msg);
}
  1. (Optional) Observe that the the following code returns "bar" from both functions as expected if run in the browser.

const data = {foo: "bar"};

function request(ctx) {
  // return data.foo ?? "some other value";  // << returns ""some other value" (when uncommented)
  return data.foo ?? error('Error occurred') ; // << returns "Error occurred" 
}

function response(ctx) {
  return data.foo ? data.foo : error('Error occurred') ; // << returns "bar"
}

function error(msg) {
  throw Error(msg);
}

[request(), response()]

Project Identifier

No response

Log output

No response

Additional information

No response

Before submitting, please confirm:

  • [x] I have done my best to include a minimal, self-contained set of instructions for consistently reproducing the issue.
  • [x] I have removed any sensitive information from my code snippets and submission.

naedx avatar Feb 14 '25 19:02 naedx