AWS Appsync Multi-table BatchPutItem Returns 空
AWS Appsync Multi-table BatchPutItem Returns Null
我一直在按照教程进行批量操作:
https://docs.aws.amazon.com/appsync/latest/devguide/tutorial-dynamodb-batch.html#multi-table-batch
当我尝试进行多 table 批处理时,即使对象已成功提交到 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
}
}
}
问题是响应returns 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 方面存在错误。任何人都可以重现这个或在执行 multi-table BatchPutItem 时能够得到响应吗?
感谢您提出非常详细的问题,它确实帮助我调试了您的问题。因此,为了解决您的问题,您需要调整您的响应映射模板。最后,我能够用空响应重现该问题。原因是 multi-table 批量放置响应的形状是这种格式:
"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
将为您 return 一个 object,其字段为 TemperatureReadingTable
和 LocationReadingTable
。但是,您正在应用 $util.toJson
来解析类型 RecordResult
,其中 temperatureReadings
和 locationReadings
作为 child 字段。
请使用以下定义更新您的响应映射模板,它将适用于您:
#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
。这将记录 Request/Response headers、已解决的 Request/Response 模板、跟踪信息等。因此它将帮助您通过查看 [=32= 的内容来快速识别此类问题] 模板。
感谢您提出这个问题,如果没有提到这个,我们会更新文档。
我一直在按照教程进行批量操作: https://docs.aws.amazon.com/appsync/latest/devguide/tutorial-dynamodb-batch.html#multi-table-batch
当我尝试进行多 table 批处理时,即使对象已成功提交到 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
}
}
}
问题是响应returns 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 方面存在错误。任何人都可以重现这个或在执行 multi-table BatchPutItem 时能够得到响应吗?
感谢您提出非常详细的问题,它确实帮助我调试了您的问题。因此,为了解决您的问题,您需要调整您的响应映射模板。最后,我能够用空响应重现该问题。原因是 multi-table 批量放置响应的形状是这种格式:
"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
将为您 return 一个 object,其字段为 TemperatureReadingTable
和 LocationReadingTable
。但是,您正在应用 $util.toJson
来解析类型 RecordResult
,其中 temperatureReadings
和 locationReadings
作为 child 字段。
请使用以下定义更新您的响应映射模板,它将适用于您:
#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
。这将记录 Request/Response headers、已解决的 Request/Response 模板、跟踪信息等。因此它将帮助您通过查看 [=32= 的内容来快速识别此类问题] 模板。
感谢您提出这个问题,如果没有提到这个,我们会更新文档。