diff --git a/packages/@aws-cdk/aws-appsync/lib/resolver.ts b/packages/@aws-cdk/aws-appsync/lib/resolver.ts index e2e2a48d8a5db..3df7f44a068c5 100644 --- a/packages/@aws-cdk/aws-appsync/lib/resolver.ts +++ b/packages/@aws-cdk/aws-appsync/lib/resolver.ts @@ -45,6 +45,12 @@ export interface BaseResolverProps { * @default - No caching configuration */ readonly cachingConfig?: CachingConfig; + /** + * The maximum number of elements per batch, when using batch invoke + * + * @default - No max batch size + */ + readonly maxBatchSize?: number; } /** @@ -112,6 +118,7 @@ export class Resolver extends Construct { requestMappingTemplate: props.requestMappingTemplate ? props.requestMappingTemplate.renderTemplate() : undefined, responseMappingTemplate: props.responseMappingTemplate ? props.responseMappingTemplate.renderTemplate() : undefined, cachingConfig: this.createCachingConfig(props.cachingConfig), + maxBatchSize: props.maxBatchSize, }); props.api.addSchemaDependency(this.resolver); if (props.dataSource) { diff --git a/packages/@aws-cdk/aws-appsync/test/appsync-mapping-template.test.ts b/packages/@aws-cdk/aws-appsync/test/appsync-mapping-template.test.ts index 5617c16255b71..b01403a7ba9fc 100644 --- a/packages/@aws-cdk/aws-appsync/test/appsync-mapping-template.test.ts +++ b/packages/@aws-cdk/aws-appsync/test/appsync-mapping-template.test.ts @@ -57,12 +57,14 @@ describe('Lambda Mapping Templates', () => { fieldName: 'relatedPosts', requestMappingTemplate: appsync.MappingTemplate.lambdaRequest('$util.toJson($ctx)', 'BatchInvoke'), responseMappingTemplate: appsync.MappingTemplate.lambdaResult(), + maxBatchSize: 10, }); // THEN Template.fromStack(stack).hasResourceProperties('AWS::AppSync::Resolver', { FieldName: 'relatedPosts', RequestMappingTemplate: batchMT, + MaxBatchSize: 10, }); }); }); \ No newline at end of file diff --git a/packages/@aws-cdk/aws-appsync/test/appsync.lambda.graphql b/packages/@aws-cdk/aws-appsync/test/appsync.lambda.graphql index 0d7af74efd581..089553dfc913b 100644 --- a/packages/@aws-cdk/aws-appsync/test/appsync.lambda.graphql +++ b/packages/@aws-cdk/aws-appsync/test/appsync.lambda.graphql @@ -21,4 +21,5 @@ type Post { ups: Int downs: Int relatedPosts: [Post] + relatedPostsMaxBatchSize: [Post] } \ No newline at end of file diff --git a/packages/@aws-cdk/aws-appsync/test/integ.appsync-lambda.ts b/packages/@aws-cdk/aws-appsync/test/integ.appsync-lambda.ts index 100ddba5e0871..b60dd5e23223d 100644 --- a/packages/@aws-cdk/aws-appsync/test/integ.appsync-lambda.ts +++ b/packages/@aws-cdk/aws-appsync/test/integ.appsync-lambda.ts @@ -67,6 +67,7 @@ lambdaDS.createResolver({ requestMappingTemplate: appsync.MappingTemplate.lambdaRequest(requestPayload('addPost', { withArgs: true })), responseMappingTemplate, }); + lambdaDS.createResolver({ typeName: 'Post', fieldName: 'relatedPosts', @@ -74,5 +75,12 @@ lambdaDS.createResolver({ responseMappingTemplate, }); +lambdaDS.createResolver({ + typeName: 'Post', + fieldName: 'relatedPostsMaxBatchSize', + requestMappingTemplate: appsync.MappingTemplate.lambdaRequest(requestPayload('relatedPostsMaxBatchSize', { withSource: true }), 'BatchInvoke'), + responseMappingTemplate, + maxBatchSize: 2, +}); app.synth(); \ No newline at end of file diff --git a/packages/@aws-cdk/aws-appsync/test/verify.integ.appsync-lambda.sh b/packages/@aws-cdk/aws-appsync/test/verify.integ.appsync-lambda.sh index b21e88e8b1ef6..fa116371fcb34 100644 --- a/packages/@aws-cdk/aws-appsync/test/verify.integ.appsync-lambda.sh +++ b/packages/@aws-cdk/aws-appsync/test/verify.integ.appsync-lambda.sh @@ -21,7 +21,7 @@ elif [[ "$1" == "--check" ]]; then exit 1 fi echo THIS TEST SHOULD PRODUCE A LIST OF BOOKS - curl -XPOST -H "Content-Type:application/graphql" -H "x-api-key:$2" -d '{ "query": "query { allPosts { id author title relatedPosts { id title } } }" }" }' $3 | json_pp + curl -XPOST -H "Content-Type:application/graphql" -H "x-api-key:$2" -d '{ "query": "query { allPosts { id author title relatedPosts { id title } relatedPostsMaxBatchSize { id title } } }" }" }' $3 | json_pp echo "" elif [[ "$1" == "--clean" ]];then cdk destroy --app "node integ.appsync-lambda.js" diff --git a/packages/@aws-cdk/aws-appsync/test/verify/lambda-tutorial/lambda-tutorial.js b/packages/@aws-cdk/aws-appsync/test/verify/lambda-tutorial/lambda-tutorial.js index 15f072857fc7e..5a08f83ef4f0a 100644 --- a/packages/@aws-cdk/aws-appsync/test/verify/lambda-tutorial/lambda-tutorial.js +++ b/packages/@aws-cdk/aws-appsync/test/verify/lambda-tutorial/lambda-tutorial.js @@ -19,6 +19,7 @@ exports.handler = (event, context, callback) => { console.log("Got an BatchInvoke Request. The payload has %d items to resolve.", event.length); const field = event[0].field; switch(field) { + case "relatedPostsMaxBatchSize": case "relatedPosts": var results = []; // the response MUST contain the same number