Appsync VTL foreach 循环限制为 101

Appsync VTL foreach loop limit of 101

当我尝试 return VTL 响应模板中的 #foreach 循环时,即使我有 116 个项目,它也不会 return 超过 101 个。为了测试,我创建了两个字段 itemsitemCount 和 运行 相同的 ES 查询。

items 的 VTL 响应映射:

[
  #foreach($entry in $context.result)
    #if( $velocityCount > 1 ) , #end
    $util.toJson($entry.get("_source"))
  #end
]

itemCount 的 VTL 响应映射:

$context.result.size()

appsync 似乎对 foreach 循环设置了限制(参考:http://people.apache.org/~henning/velocity/html/ch05s04.html)。

我们刚刚将此限制更新为 1000,该限制在 AppSync limits page 中更新。

绕过 AppSync 和 API 网关设置的任意速度 directive.foreach.maxloops = 1000 限制的一种方法是通过以 1000 个为单位进行分区来打破您的 foreach 循环。这是一个工作示例:

## Partition to get around foreach iteration limit
#set($partition_size = 1000)
#set($max_partition_index = $list.size()/$partition_size)
#foreach($partition_index in [0..$max_partition_index])
  #set($start_index = $partition_index * $partition_size)
  #if($partition_index == $max_partition_index)
    ## Last partition
    #set($end_index = $list.size() - 1)
  #else
    #set($end_index = (($partition_index + 1) * $partition_size) - 1)
  #end

  #foreach($index in [$start_index..$end_index])
    #if($index != 0),#end
    "$list[$index].S"
  #end
#end