amazon-web-services - 如何使用 AppSync Resolvers 和 Aurora 将可选字段插入为空?

标签 amazon-web-services graphql aws-appsync resolver amazon-aurora

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

这是我的解析器 -

{
    "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"
    }
  }
}

这就是我想要的。

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

最佳答案

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) 替换,那么它会返回一个 null 字符串。但是,VTL 将打印 $notes,因为这是它处理 null 引用的方式。为了打印 null ,这是 null 的有效 JSON 表示,我们必须将其序列化为 JSON。所以正确的说法是:
#set($notes = $util.toJson($util.defaultIfNullOrEmpty($context.arguments.notes, null)))

完整测试:

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

关于amazon-web-services - 如何使用 AppSync Resolvers 和 Aurora 将可选字段插入为空?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53396717/

相关文章:

amazon-web-services - 如果删除了 cloudformation 堆栈且相同的安全组已存在,请重新创建 SecurityGroup

sql - 在 SQL 表中有一个专用的计数字段是否可取?

java - graphql-spqr无法查询父类字段

javascript - 如何在没有 'id' 的情况下获取表的行信息?

amazon-web-services - 如何获取映射模板中的日期/请求时间/时间戳

ssl - Amazon AWS 上传服务器证书格式

amazon-ec2 - 为什么 Windows 快照需要很长时间?

amazon-web-services - AWS中两种不同类型的VPC链接之间的主要区别

ios - Graphql 和往返。这只是一个 ios 应用程序问题吗?

amazon-web-services - AWS AppSync - 公共(public)和私有(private)查询