amazon-web-services - 如何使用 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 和带引号的字符串放入同一字段?
解决方案
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
}
}
}
推荐阅读
- python - 为独立条目填充 NumPy 数组的有效方法?
- excel - Excel VBA - 每个唯一组的 N 个随机记录
- postgresql - 如何编写 PostgreSQL 存储过程来查找和终止空闲查询?
- javascript - 使用 chart.js 的实时滚动图表
- c# - 在 .NET 5 WinForms 应用程序中使用非托管 32/64 位 DLL
- vue.js - 如何在 Vue.js 中创建弹出按钮?
- odoo - 错误:无法读取未定义的属性(读取“道具”)
- flutter - Keycloak 给了我 XMLHttpRequest 错误。有颤振但不要有邮递员
- salesforce - 创建一个转换列表的方法
到地图 以 SObjectField 作为方法参数,有什么建议吗? - next.js - 基于主机的路由