0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

AppSyncではundefined === null

Posted at

事象

https://github.com/aws/aws-appsync-community/issues/305 にある通り、AppSync では undefined === null として扱われます。

実際に困ったこと

parameter を指定しない場合、null が指定されたものとなります(以下の例での role )。
このままだと、値として null を検索したい場合と、parameter が指定されていない(つまり検索条件に含めない)場合の区別ができません。

以下では、role という parameter を指定しなかった場合の挙動ですが、undefined, null いずれも true となってしまいます 。

console.log("undefined === null:", undefined === null);
// "undefined === null:" true

console.log("role === undefined:", role === undefined);
// "role === undefined:" true

console.log("role === null:", role === null);
// "role === null:" true

ワークアラウンド

// AppSync では検索のためのパラメータが渡されていない場合、null となる。
// 明示的に null で検索する場合と区別するために roleSpecified を使用する。
const roleSpecified = query && Object.keys(query).includes("role");
if (roleSpecified) { // role が検索条件にある場合
  // ...
} else {
  // ...
}

補足

in や hasOwnProperty(以下、(1), (2))でも同じようなことが実現できないかと思いましたが、AppSync では使えなかったため、以下 (3) の方法を採用しました。

(1) in を使う

const where = {};
      
// Check if the parameter was included in the query
if ('name' in query) {
  where.role = query.role;  // This can be null
}

(2) hasOwnProperty を使う

if (query.hasOwnProperty('role')) {

(3) Object.keys + includes を使う

if (Object.keys(query).includes('role')) {
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?