首页 > 解决方案 > 如何使用 AppSync 解析器和 Aurora 将可选字段插入为空?

问题描述

我有一个可选的字符串字段notes,有时为空。如果它是空的,我想插入null,否则我想插入字符串。

这是我的解析器-

{
    "version" : "2017-02-28",
    "operation": "Invoke",
        #set($id = $util.autoId())
        #set($notes = $util.defaultIfNullOrEmpty($context.arguments.notes, 'null'))

        "payload": {
          "sql":"INSERT INTO things VALUES ('$id', :NOTES)",
          "variableMapping": {
            ":NOTES" : $notes
          },
          "responseSQL": "SELECT * FROM things WHERE id = '$id'"
        }

}

有了这个graphql

mutation CreateThing{
  createThing() {
    id
    notes
  }
}

我得到 -

{
  "data": {
    "createRoll": {
      "id": "6af68989-0bdc-44e2-8558-aeb4c8418e93",
     "notes": "null"
   }
 }

}

当我真的想要不带引号的null时。

有了这个graphql -

mutation CreateThing{
  createThing(notes: "Here are some notes") {
    id
    notes
  }
}

我得到 -

{
  "data": {
    "createThing": {
      "id": "6af68989-0bdc-44e2-8558-aeb4c8418e93",
      "notes": "Here are some notes"
    }
  }
}

这就是我想要的。

如何将无引号的 null 和带引号的字符串放入同一字段?

标签: amazon-web-servicesgraphqlaws-appsyncresolveramazon-aurora

解决方案


TL; DR您应该使用它$util.toJson()来正确打印$context.arguments.notes。将您的$notes作业替换为

#set($notes = $util.toJson($util.defaultIfNullOrEmpty($context.arguments.notes, null)))

解释:

原因是 VTL 会打印toString()方法返回的任何内容,而您的调用 $util.defaultIfNullOrEmpty($context.arguments.notes, 'null')将返回字符串"null",该字符串将打印为"null".

如果替换为$util.defaultIfNullOrEmpty($context.arguments.notes, null)then 它将返回一个null字符串。但是,VTL 将打印$notes,因为这是它处理null引用的方式。为了打印null,这是 的有效 JSON 表示null,我们必须将其序列化为 JSON。所以正确的说法是:

#set($notes = $util.toJson($util.defaultIfNullOrEmpty($context.arguments.notes, null)))

全面测试:

我假设您从 AWS AppSync 控制台中提供的 RDS 示例开始并对其进行了修改。为了重现,我content将 Schema 中的字段更新为可为空:

type Mutation {
   ...
   createPost(author: String!, content: String): Post
   ...
}
type Post {
    id: ID!
    author: String!
    content: String
    views: Int
    comments: [Comment]
}

我修改了posts表架构,所以content那里也可以为空:(在 Lambda 函数内部)

function conditionallyCreatePostsTable(connection) {
  const createTableSQL = `CREATE TABLE IF NOT EXISTS posts (
    id        VARCHAR(64) NOT NULL,
    author    VARCHAR(64) NOT NULL,
    content   VARCHAR(2048),
    views     INT NOT NULL,
    PRIMARY KEY(id))`;
  return executeSQL(connection, createTableSQL);
}

这是createPost突变的请求模板:

    {
    "version" : "2017-02-28",
    "operation": "Invoke",
    #set($id = $util.autoId())   
    "payload": {
      "sql":"INSERT INTO posts VALUES ('$id', :AUTHOR, :CONTENT, 1)",
      "variableMapping": {
        ":AUTHOR" : "$context.arguments.author",
        ":CONTENT" : $util.toJson($util.defaultIfNullOrEmpty($context.arguments.content, null))
      },
      "responseSQL": "SELECT id, author, content, views FROM posts WHERE id = '$id'"
    }
}

和响应模板:

$util.toJson($context.result[0])

以下查询:

mutation CreatePost {
  createPost(author: "Me") {
    id
    author
    content
    views
  }
}

返回:

{
  "data": {
    "createPost": {
      "id": "b42ee08c-956d-4b89-afda-60fe231e86d7",
      "author": "Me",
      "content": null,
      "views": 1
    }
  }
}

mutation CreatePost {
  createPost(author: "Me", content: "content") {
    id
    author
    content
    views
  }
}  

返回

{
  "data": {
    "createPost": {
      "id": "c6af0cbf-cf05-4110-8bc2-833bf9fca9f5",
      "author": "Me",
      "content": "content",
      "views": 1
    }
  }
}

推荐阅读