无法在 AppSync 响应映射模板中从 RDS 序列化 AWSDate [英] Unable to serialise AWSDate from RDS in AppSync response mapping template

查看:16
本文介绍了无法在 AppSync 响应映射模板中从 RDS 序列化 AWSDate的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用无服务器 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. :(

推荐答案

我偶然在与此问题相关的存储库中找到了答案 将 AppSync 和 Amazon RDS 与 serverless-graphql 结合使用

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.结果)[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

解决了这个问题.

注意:如果您在将方案列类型更改为 AWSDateTime 后遇到此错误,只需访问 DynamoDB 控制台并更新不是ISO 8601.

A Note: If you are facing this error after changing your scheme column type to AWSDateTime, just visit the DynamoDB console and update records that do not in the format of ISO 8601.

这篇关于无法在 AppSync 响应映射模板中从 RDS 序列化 AWSDate的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆