Reactjs 用于计算DynamoDB项的AppSync自定义解析程序
我有一个React-AppSync应用程序,其中包含使用Amplify CLI配置的AWS资源。我需要在一个包含数千个条目的表中获取实体的数量,而无需获取条目本身。除了获取数千个实体来计算它们的愚蠢之外,我还遇到了一个问题,我甚至不能在一个请求中获取所有这些记录。这样一个简单的调用:Reactjs 用于计算DynamoDB项的AppSync自定义解析程序,reactjs,amazon-web-services,graphql,amazon-dynamodb,aws-appsync,Reactjs,Amazon Web Services,Graphql,Amazon Dynamodb,Aws Appsync,我有一个React-AppSync应用程序,其中包含使用Amplify CLI配置的AWS资源。我需要在一个包含数千个条目的表中获取实体的数量,而无需获取条目本身。除了获取数千个实体来计算它们的愚蠢之外,我还遇到了一个问题,我甚至不能在一个请求中获取所有这些记录。这样一个简单的调用: import { API, graphqlOperation } from "aws-amplify"; const nodeData = await API.graphql( graphqlOperatio
import { API, graphqlOperation } from "aws-amplify";
const nodeData = await API.graphql(
graphqlOperation(
`query allNodeIds {
listNodes(limit: 10000) {
items {
id
}
}
}`,
{}
)
);
const nodeCount = nodeData.data.listNodes.items.length;
导致以下控制台错误:
...
errorType: "MappingTemplate"
message: "List size cannot exceed 1000"
似乎我需要编写一个自定义查询和解析程序来计算并返回表中的条目数,但不知道从哪里开始编写解析程序(在VTL中?),在项目中放置它们,等等。有什么帮助吗
编辑:基于我的另一个生成的冲突解决程序编写了一个冲突解决程序,但它似乎仍然限制在1001条记录,尽管我将限制设置为9999条
模式
...
countResponses(filter: ModelResponseFilterInput, limit: Int, nextToken: String): Int
请求映射模板
#set( $limit = $util.defaultIfNull($context.args.limit, 9999) )
#set( $ListRequest = {
"version": "2017-02-28",
"limit": $limit
} )
#if( $context.args.nextToken )
#set( $ListRequest.nextToken = "$context.args.nextToken" )
#end
#if( $context.args.filter )
#set( $ListRequest.filter = $util.parseJson("$util.transform.toDynamoDBFilterExpression($ctx.args.filter)") )
#end
#if( !$util.isNull($modelQueryExpression)
&& !$util.isNullOrEmpty($modelQueryExpression.expression) )
$util.qr($ListRequest.put("operation", "Query"))
$util.qr($ListRequest.put("query", $modelQueryExpression))
#if( !$util.isNull($ctx.args.sortDirection) && $ctx.args.sortDirection == "DESC" )
#set( $ListRequest.scanIndexForward = false )
#else
#set( $ListRequest.scanIndexForward = true )
#end
#else
$util.qr($ListRequest.put("operation", "Scan"))
#end
$util.toJson($ListRequest)
## [Start] Determine request authentication mode **
#if( $util.isNullOrEmpty($authMode) && !$util.isNull($ctx.identity) && !$util.isNull($ctx.identity.sub) && !$util.isNull($ctx.identity.issuer) && !$util.isNull($ctx.identity.username) && !$util.isNull($ctx.identity.claims) && !$util.isNull($ctx.identity.sourceIp) && !$util.isNull($ctx.identity.defaultAuthStrategy) )
#set( $authMode = "userPools" )
#end
## [End] Determine request authentication mode **
## [Start] Check authMode and execute owner/group checks **
#if( $authMode == "userPools" )
## No Static Group Authorization Rules **
## [Start] If not static group authorized, filter items **
#if( !$isStaticGroupAuthorized )
#set( $count = 0 )
#foreach( $item in $ctx.result.items )
## No Dynamic Group Authorization Rules **
## [Start] Owner Authorization Checks **
#set( $isLocalOwnerAuthorized = false )
## Authorization rule: { allow: owner, ownerField: "owner", identityClaim: "cognito:username" } **
#set( $allowedOwners0 = $item.owner )
#set( $identityValue = $util.defaultIfNull($ctx.identity.claims.get("username"), $util.defaultIfNull($ctx.identity.claims.get("cognito:username"), "___xamznone____")) )
#if( $util.isList($allowedOwners0) )
#foreach( $allowedOwner in $allowedOwners0 )
#if( $allowedOwner == $identityValue )
#set( $isLocalOwnerAuthorized = true )
#end
#end
#end
#if( $util.isString($allowedOwners0) )
#if( $allowedOwners0 == $identityValue )
#set( $isLocalOwnerAuthorized = true )
#end
#end
## [End] Owner Authorization Checks **
#if( ($isLocalDynamicGroupAuthorized == true || $isLocalOwnerAuthorized == true) )
#set( $count = $count + 1)
#end
#end
#set( $ctx = $count )
#end
## [End] If not static group authorized, filter items **
#end
## [End] Check authMode and execute owner/group checks **
$util.toJson($ctx)
响应映射模板
#set( $limit = $util.defaultIfNull($context.args.limit, 9999) )
#set( $ListRequest = {
"version": "2017-02-28",
"limit": $limit
} )
#if( $context.args.nextToken )
#set( $ListRequest.nextToken = "$context.args.nextToken" )
#end
#if( $context.args.filter )
#set( $ListRequest.filter = $util.parseJson("$util.transform.toDynamoDBFilterExpression($ctx.args.filter)") )
#end
#if( !$util.isNull($modelQueryExpression)
&& !$util.isNullOrEmpty($modelQueryExpression.expression) )
$util.qr($ListRequest.put("operation", "Query"))
$util.qr($ListRequest.put("query", $modelQueryExpression))
#if( !$util.isNull($ctx.args.sortDirection) && $ctx.args.sortDirection == "DESC" )
#set( $ListRequest.scanIndexForward = false )
#else
#set( $ListRequest.scanIndexForward = true )
#end
#else
$util.qr($ListRequest.put("operation", "Scan"))
#end
$util.toJson($ListRequest)
## [Start] Determine request authentication mode **
#if( $util.isNullOrEmpty($authMode) && !$util.isNull($ctx.identity) && !$util.isNull($ctx.identity.sub) && !$util.isNull($ctx.identity.issuer) && !$util.isNull($ctx.identity.username) && !$util.isNull($ctx.identity.claims) && !$util.isNull($ctx.identity.sourceIp) && !$util.isNull($ctx.identity.defaultAuthStrategy) )
#set( $authMode = "userPools" )
#end
## [End] Determine request authentication mode **
## [Start] Check authMode and execute owner/group checks **
#if( $authMode == "userPools" )
## No Static Group Authorization Rules **
## [Start] If not static group authorized, filter items **
#if( !$isStaticGroupAuthorized )
#set( $count = 0 )
#foreach( $item in $ctx.result.items )
## No Dynamic Group Authorization Rules **
## [Start] Owner Authorization Checks **
#set( $isLocalOwnerAuthorized = false )
## Authorization rule: { allow: owner, ownerField: "owner", identityClaim: "cognito:username" } **
#set( $allowedOwners0 = $item.owner )
#set( $identityValue = $util.defaultIfNull($ctx.identity.claims.get("username"), $util.defaultIfNull($ctx.identity.claims.get("cognito:username"), "___xamznone____")) )
#if( $util.isList($allowedOwners0) )
#foreach( $allowedOwner in $allowedOwners0 )
#if( $allowedOwner == $identityValue )
#set( $isLocalOwnerAuthorized = true )
#end
#end
#end
#if( $util.isString($allowedOwners0) )
#if( $allowedOwners0 == $identityValue )
#set( $isLocalOwnerAuthorized = true )
#end
#end
## [End] Owner Authorization Checks **
#if( ($isLocalDynamicGroupAuthorized == true || $isLocalOwnerAuthorized == true) )
#set( $count = $count + 1)
#end
#end
#set( $ctx = $count )
#end
## [End] If not static group authorized, filter items **
#end
## [End] Check authMode and execute owner/group checks **
$util.toJson($ctx)
首先在模式中编写一个GraphQL查询。下一个要继续的位置取决于AppSync解析器的类型。最流行的两种类型是DynamoDB和Lambda。只需在此GraphQL transform网页上选择一种自定义解析器类型:例如,对于这两种解析器类型,DynamoDB或Lambda,都可以使用自定义解析器。仅供参考,DynamoDB也可能受益于次级指数。使用@key在GraphQL架构中定义索引。@starpebble这并不能解决我无法在服务器端执行计数操作的问题,因此必须使用
nextToken
链接请求,直到我获取了所有结果——然后对它们进行计数。我不想一次获得1000条记录。我希望服务器对它们进行计数并返回一个整数。我尝试在控制台中编写自定义解析程序(我使用的是DynamoDB btw),并且可以成功返回一个整数,但它的上限为1001条记录,尽管我可以看到我还有更多记录。我想这与DynamoDB的“扫描”操作限制有关。在上面发布我的解析程序…这是一个有趣的限制,1001。为了计算和列出所有ID,我有点同意自定义解析器必须有一个循环。我不认为张贴的vtl将在某些情况下工作,因为限制没有一个循环。试试lambda解析器。在lambda解析器中放置一个循环以检索所有id列表。由于lambda函数响应负载上的字节大小限制,lambda函数可以返回多少项id有一些限制。如果列表字节大小必须超出有效载荷限制,则考虑缓存文件。感觉缩放对这个查询很重要。@starpebble将尝试一下。实际上不需要ID,这是我第一次尝试使用现有查询来避免编写自定义解析器。第一次使用AWS任何东西。首先在模式中编写一个GraphQL查询。下一个要继续的位置取决于AppSync解析器的类型。最流行的两种类型是DynamoDB和Lambda。只需在此GraphQL transform网页上选择一种自定义解析器类型:例如,对于这两种解析器类型,DynamoDB或Lambda,都可以使用自定义解析器。仅供参考,DynamoDB也可能受益于次级指数。使用@key在GraphQL架构中定义索引。@starpebble这并不能解决我无法在服务器端执行计数操作的问题,因此必须使用nextToken
链接请求,直到我获取了所有结果——然后对它们进行计数。我不想一次获得1000条记录。我希望服务器对它们进行计数并返回一个整数。我尝试在控制台中编写自定义解析程序(我使用的是DynamoDB btw),并且可以成功返回一个整数,但它的上限为1001条记录,尽管我可以看到我还有更多记录。我想这与DynamoDB的“扫描”操作限制有关。在上面发布我的解析程序…这是一个有趣的限制,1001。为了计算和列出所有ID,我有点同意自定义解析器必须有一个循环。我不认为张贴的vtl将在某些情况下工作,因为限制没有一个循环。试试lambda解析器。在lambda解析器中放置一个循环以检索所有id列表。由于lambda函数响应负载上的字节大小限制,lambda函数可以返回多少项id有一些限制。如果列表字节大小必须超出有效载荷限制,则考虑缓存文件。感觉缩放对这个查询很重要。@starpebble将尝试一下。实际上不需要ID,这是我第一次尝试使用现有查询来避免编写自定义解析器。第一次与AWS合作。