无法转换为对象 - AppSync 速度模板
Unable to Convert to Object - AppSync Velocity Template
我的地图与架构匹配。我显然只是不明白什么。我是 appsync、graphql 和 velocity 的新手,所以我确定这是一个简单的错误,但我一直在兜圈子几个小时,试图找出我做错了什么。
查询
query MyQuery {
searchUSDAFoundationFoods(terms: "apple") {
uid
description
}
}
回应
{
"data": {
"searchUSDAFoundationFoods": null
},
"errors": [
{
"path": [
"searchUSDAFoundationFoods"
],
"data": null,
"errorType": "MappingTemplate",
"errorInfo": null,
"locations": [
{
"line": 2,
"column": 3,
"sourceName": null
}
],
"message": "Unable to convert \n[\n \n \n {\"uid\":\"usda-foundation-food-1105664\",\"description\":\"Apples, granny smith, with skin, raw\"}\n \n \n {\"uid\":\"usda-foundation-food-1105781\",\"description\":\"Apples, gala, with skin, raw\"}\n \n \n {\"uid\":\"usda-foundation-food-1105547\",\"description\":\"Apples, honeycrisp, with skin, raw\"}\n \n \n {\"uid\":\"usda-foundation-food-1105430\",\"description\":\"Apples, red delicious, with skin, raw\"}\n \n \n {\"uid\":\"usda-foundation-food-1105897\",\"description\":\"Apples, fuji, with skin, raw\"}\n] to Object."
}
]
}
响应模板
[
#foreach($entry in $context.result.hits.hits)
#set($myMap = {})
$util.qr($myMap.put("uid", $entry.get("_id")))
$util.qr($myMap.put("description", $entry.get("_source").get("description")))
$util.toJson($myMap)
#end
]
架构
type FoodSearchResult {
uid: String
description: String
}
type Query {
searchUSDAFoundationFoods(terms: String): [FoodSearchResult]
}
当我将响应模板更改为以下内容时它开始工作:
[
#foreach($entry in $context.result.hits.hits)
#set($myMap = {})
$util.qr($myMap.put("uid", $entry.get("_id")))
$util.qr($myMap.put("description", $entry.get("_source").get("description")))
#if( $velocityCount > 1 ) , #end
$util.toJson($myMap)
#end
]
唯一的变化是添加了以下行:
#if( $velocityCount > 1 ) , #end
我在一个示例中注意到了它,但在亚马逊网站上的 Velocity 模板编程指南或其他任何地方都没有找到关于它是什么、它如何工作或为什么需要它的明确解释。
更新:
该行正在添加必要的逗号以使结果有效 JSON。
我的地图与架构匹配。我显然只是不明白什么。我是 appsync、graphql 和 velocity 的新手,所以我确定这是一个简单的错误,但我一直在兜圈子几个小时,试图找出我做错了什么。
查询
query MyQuery {
searchUSDAFoundationFoods(terms: "apple") {
uid
description
}
}
回应
{
"data": {
"searchUSDAFoundationFoods": null
},
"errors": [
{
"path": [
"searchUSDAFoundationFoods"
],
"data": null,
"errorType": "MappingTemplate",
"errorInfo": null,
"locations": [
{
"line": 2,
"column": 3,
"sourceName": null
}
],
"message": "Unable to convert \n[\n \n \n {\"uid\":\"usda-foundation-food-1105664\",\"description\":\"Apples, granny smith, with skin, raw\"}\n \n \n {\"uid\":\"usda-foundation-food-1105781\",\"description\":\"Apples, gala, with skin, raw\"}\n \n \n {\"uid\":\"usda-foundation-food-1105547\",\"description\":\"Apples, honeycrisp, with skin, raw\"}\n \n \n {\"uid\":\"usda-foundation-food-1105430\",\"description\":\"Apples, red delicious, with skin, raw\"}\n \n \n {\"uid\":\"usda-foundation-food-1105897\",\"description\":\"Apples, fuji, with skin, raw\"}\n] to Object."
}
]
}
响应模板
[
#foreach($entry in $context.result.hits.hits)
#set($myMap = {})
$util.qr($myMap.put("uid", $entry.get("_id")))
$util.qr($myMap.put("description", $entry.get("_source").get("description")))
$util.toJson($myMap)
#end
]
架构
type FoodSearchResult {
uid: String
description: String
}
type Query {
searchUSDAFoundationFoods(terms: String): [FoodSearchResult]
}
当我将响应模板更改为以下内容时它开始工作:
[
#foreach($entry in $context.result.hits.hits)
#set($myMap = {})
$util.qr($myMap.put("uid", $entry.get("_id")))
$util.qr($myMap.put("description", $entry.get("_source").get("description")))
#if( $velocityCount > 1 ) , #end
$util.toJson($myMap)
#end
]
唯一的变化是添加了以下行:
#if( $velocityCount > 1 ) , #end
我在一个示例中注意到了它,但在亚马逊网站上的 Velocity 模板编程指南或其他任何地方都没有找到关于它是什么、它如何工作或为什么需要它的明确解释。
更新:
该行正在添加必要的逗号以使结果有效 JSON。