考虑以下 GraphQL 模板:
type Foo {
id: ID!
bars: Bars
}
type Bar {
id: ID!
name: String!
}
type Bars {
items: [Bar]!
nextToken: String
}
Foo 类型中字段的映射模板bars
如下所示:
#set($ids = [])
#foreach($id in $context.source.bars)
#set($map = {})
$util.qr($map.put("id", $util.dynamodb.toString($id)))
$util.qr($ids.add($map))
#end
{
"version" : "2018-05-29",
"operation" : "BatchGetItem",
"tables" : {
"barsTable" : {
"keys": $util.toJson($ids),
"consistentRead": true
}
}
}
这很好用。但是如果该bars
字段包含空数组[]
,模板显然会崩溃并出现以下错误:
"errors": [
{
"path": [
"getFoo",
"bars"
],
"data": null,
"errorType": "MappingTemplate",
"errorInfo": null,
"locations": [
{
"line": 59,
"column": 7,
"sourceName": null
}
],
"message": "RequestItem keys '$[tables][barsTable]' can't be empty"
}
]
所以我的问题是:
如何防止查询被执行,并在$context.source.bars
为空时将一个空数组返回到响应模板?