我一直在按照本教程进行批处理操作: https://docs.aws.amazon.com/appsync/latest/devguide/tutorial-dynamodb-batch.html#multi-table-batch
当我尝试进行多表批处理时,即使对象已成功提交给dynamodb,我也得到了空响应。
这是我的突变:
mutation sendReadings {
recordReadings(
tempReadings: [
{sensorId: 1, value: 85.5, timestamp: "2018-02-01T17:21:05.000+08:00"},
{sensorId: 2, value: 85.7, timestamp: "2018-02-01T17:21:06.000+08:00"},
{sensorId: 3, value: 85.8, timestamp: "2018-02-01T17:21:07.000+08:00"},
{sensorId: 4, value: 84.2, timestamp: "2018-02-01T17:21:08.000+08:00"},
{sensorId: 5, value: 81.5, timestamp: "2018-02-01T17:21:09.000+08:00"}
]
locReadings: [
{sensorId: 1, lat: 47.615063, long: -122.333551, timestamp: "2018-02-01T17:21:05.000+08:00"},
{sensorId: 2, lat: 47.615163, long: -122.333552, timestamp: "2018-02-01T17:21:06.000+08:00"}
{sensorId: 3, lat: 47.615263, long: -122.333553, timestamp: "2018-02-01T17:21:07.000+08:00"}
{sensorId: 4, lat: 47.615363, long: -122.333554, timestamp: "2018-02-01T17:21:08.000+08:00"}
{sensorId: 5, lat: 47.615463, long: -122.333555, timestamp: "2018-02-01T17:21:09.000+08:00"}
]) {
locationReadings {
sensorId
timestamp
lat
long
}
temperatureReadings {
sensorId
timestamp
value
}
}
}
问题是响应返回null:
{ "data": { "recordReadings": { "locationReadings": null, "temperatureReadings": null } } }
请求映射模板:
## Convert tempReadings arguments to DynamoDB objects #set($tempReadings = []) #foreach($reading in ${ctx.args.tempReadings}) $util.qr($tempReadings.add($util.dynamodb.toMapValues($reading))) #end ## Convert locReadings arguments to DynamoDB objects #set($locReadings = []) #foreach($reading in ${ctx.args.locReadings}) $util.qr($locReadings.add($util.dynamodb.toMapValues($reading))) #end { "version" : "2018-05-29", "operation" : "BatchPutItem", "tables" : { "LocationReadingTable": $utils.toJson($locReadings), "TemperatureReadingTable": $utils.toJson($tempReadings) } }
响应映射模板:
## If there was an error with the invocation ## there might have been partial results #if($ctx.error) ## Append a GraphQL error for that field in the GraphQL response $utils.appendError($ctx.error.message, $ctx.error.message) #end ## Also returns data for the field in the GraphQL response $utils.toJson($context.result.data)
代码与教程几乎相同。我检查了日志并发送了响应,但未捕获到响应,因此我的代码已关闭或AWS侧存在错误。任何人都可以在做多表BatchPutItem时重现此错误或能够获得响应吗?
答案 0 :(得分:2)
感谢您提出一个非常详细的问题,它无疑帮助我调试了您的问题。因此,为了解决您的问题,您需要调整响应映射模板。我能够重现该问题,但最终却没有响应。原因是多表批处理响应的形状具有以下格式:
"result": {
"data": {
"TemperatureReadingTable": [
{
"value": 85.5,
"sensorId": "1",
"timestamp": "2018-02-01T17:21:05.000+08:00"
},
{
"value": 85.7,
"sensorId": "2",
"timestamp": "2018-02-01T17:21:06.000+08:00"
},
{
"value": 85.8,
"sensorId": "3",
"timestamp": "2018-02-01T17:21:07.000+08:00"
},
{
"value": 84.2,
"sensorId": "4",
"timestamp": "2018-02-01T17:21:08.000+08:00"
},
{
"value": 81.5,
"sensorId": "5",
"timestamp": "2018-02-01T17:21:09.000+08:00"
}
],
"LocationReadingTable": [
{
"lat": 47.615063,
"long": -122.333551,
"sensorId": "1",
"timestamp": "2018-02-01T17:21:05.000+08:00"
},
{
"lat": 47.615163,
"long": -122.333552,
"sensorId": "2",
"timestamp": "2018-02-01T17:21:06.000+08:00"
},
{
"lat": 47.615263,
"long": -122.333553,
"sensorId": "3",
"timestamp": "2018-02-01T17:21:07.000+08:00"
},
{
"lat": 47.615363,
"long": -122.333554,
"sensorId": "4",
"timestamp": "2018-02-01T17:21:08.000+08:00"
},
{
"lat": 47.615463,
"long": -122.333555,
"sensorId": "5",
"timestamp": "2018-02-01T17:21:09.000+08:00"
}
]
},
因此,$context.result.data
将向您返回一个对象,其对象为TemperatureReadingTable
和LocationReadingTable
。但是,您正在应用$util.toJson
来解析类型RecordResult
,该类型具有temperatureReadings
和locationReadings
作为子字段。
请使用以下定义更新您的“响应映射”模板,它将对您有用:
#if($ctx.error)
## Append a GraphQL error for that field in the GraphQL response
$utils.appendError($ctx.error.message, $ctx.error.message)
#end
{
"temperatureReadings": $util.toJson(${ctx.result.data.TemperatureReadingTable}),
"locationReadings": $util.toJson(${ctx.result.data.LocationReadingTable})
}
此外,我建议您从控制台的“设置”页面启用CloudWatch日志,并选择ALL
。这将记录请求/响应标头,已解析的请求/响应模板,跟踪信息等。因此,通过查看请求/响应模板的内容,它将帮助您快速识别此类问题。
感谢您带来此问题,如果文档中未提及,我们将对其进行更新。