amazon-web-services - 如何将 Hal+Json 与 AWS AppSync 结合使用
问题描述
我的后端使用媒体类型 hal+json(rest api) 返回 json。这里的问题是 AWS AppSync 的速度解析器返回:
词法错误,遇到:\"_\" (95),之后:\".\" at unset [第 8 行,第 28 列]"
因为媒体类型节点以下划线开头,例如:
{
"_embedded":{
"vehicle-assemblers":[...]
}
}
我相信 AppSync 使用的引擎抱怨以下划线开头的变量。
“配置请求映射模板”(AWS 编辑解析程序)
## Raise a GraphQL field error in case of a datasource
#if($ctx.error)
$util.error($ctx.error.message, $ctx.error.type)
#end
## If the response is not 200 then return an error.
#if($ctx.result.statusCode == 200)
#set($response = $util.parseJson($ctx.result.body))
$util.toJson($response._embedded.vehicle-assemblers) ##line 8
#else
$utils.appendError($ctx.result.body, $ctx.result.statusCode)
#end
有谁知道如何解决这个问题?
解决方案
我能够解决
#if($ctx.error)
$util.error($ctx.error.message, $ctx.error.type)
#end
#if($ctx.result.statusCode == 200)
#set($response = $util.parseJson($ctx.result.body))
#set($vehicles = $util.toJson($response["_embedded"]["vehicle-assemblers"]))
{
"items": $vehicles
}
#else
$utils.appendError($ctx.result.body, $ctx.result.statusCode)
#end
推荐阅读
- scala - 为什么 SBT 在编译时会找到导入,但在构建项目时却找不到?
- javascript - 如果通过 html 属性订阅,为什么我的 Web 组件的 CustomEvent 事件处理程序没有被调用?
- javascript - 从php中检索数据
- kubernetes - google PD 的 kubernetes 卷挂载问题(已安装 kubeadm)
- sql - 连接多个表时,总和是它应该是的倍数
- vb.net - 使用 Runspace 在 Visual Basic 中调用 powershell 脚本时出现 System.NullReferenceException
- ruby - 大型 JSON 字符串中的字符串插值
- python - 当python中的一行中有多个最大值时,在2维向量的每一行中查找最大值
- java - 只有在其他片段被销毁时才启动一个方法
- javascript - 对于 JavaScript 的 toLocaleDateString,是否有替代 dateStyle 和 timeStyle 的新标准?