问题描述
我正在使用无服务器Aurora作为数据库来构建AppSync项目,并偶然发现了这个奇怪的错误:
I'm building an AppSync project using serverless Aurora as my db, and stumbled across this strange error:
"Can't serialize value (/getUsers/created_at) : Unable to serialize `2019-09-28 07:36:13` as a valid DateTime Object."
当我得到一个看起来像这样的User对象时,就会发生这种情况:
This happens when I get a User object which looks like this:
type Users {
id: String!
name: String!
description: String
created_at: AWSDateTime
updated_at: AWSDateTime
logged_in: AWSDateTime
}
该错误似乎正在发生,因为$utils.rds.toJsonObject($ctx.result)[0][0]
无法解析AWSDateTime.这样就无法从数据库中提供带有日期的任何内容.
The error seems to be happening because $utils.rds.toJsonObject($ctx.result)[0][0]
can't parse an AWSDateTime. Which makes anything with a date impossible to serve from the database.
如果我只是选择不带日期的对象["SELECT id,name,description FROM Users WHERE id='$ctx.args.id'"]
,则效果很好.
If I simply select the object without dates ["SELECT id,name,description FROM Users WHERE id='$ctx.args.id'"]
it works fine.
那么应该如何在AWS AppSync和Aurora中处理日期?我无法在文档中找到任何示例或引用来处理日期. :(
So how should dates be handled in AWS AppSync and Aurora? I have been unable to find any example or reference to handling dates in the documentation. :(
推荐答案
我在与此问题相关的回购中意外地找到了答案
I accidentally found the answer in a repo connected to this question Use AppSync and Amazon RDS with serverless-graphql
事实证明,与用户 dev1702 一样,$utils.rds.toJsonObject($ctx.result)[0][0]
无法将RDS时间戳转换为GraphQl AWSDate格式
It turns out, like user dev1702 discovered, that $utils.rds.toJsonObject($ctx.result)[0][0]
cannot parse a RDS Timestampt INTO an GraphQl AWSDate format.
因此,只需将graphql模式类型从以下更改即可:
So simply changing the graphql schema type from:
created_at: AWSDateTime
收件人:
created_at: String
解决了这个问题.
这篇关于无法在AppSync响应映射模板中从RDS序列化AWSDate的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!