From debbce987a7fca1f39ce82b1d6edbf98912a20bf Mon Sep 17 00:00:00 2001 From: Constantin Ponfick Date: Tue, 5 Jul 2022 12:18:54 +0200 Subject: [PATCH 1/4] added maxBatchSize to BaseResolverProps --- packages/@aws-cdk/aws-appsync/lib/resolver.ts | 7 +++++++ .../aws-appsync/test/appsync-mapping-template.test.ts | 2 ++ packages/@aws-cdk/aws-appsync/test/integ.appsync-lambda.ts | 1 + 3 files changed, 10 insertions(+) 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/integ.appsync-lambda.ts b/packages/@aws-cdk/aws-appsync/test/integ.appsync-lambda.ts index 100ddba5e0871..ccb39f339c01b 100644 --- a/packages/@aws-cdk/aws-appsync/test/integ.appsync-lambda.ts +++ b/packages/@aws-cdk/aws-appsync/test/integ.appsync-lambda.ts @@ -72,6 +72,7 @@ lambdaDS.createResolver({ fieldName: 'relatedPosts', requestMappingTemplate: appsync.MappingTemplate.lambdaRequest(requestPayload('relatedPosts', { withSource: true }), 'BatchInvoke'), responseMappingTemplate, + maxBatchSize: 5, }); From db2620acc6a2285fb79f0c5e1e44aadf3aef089f Mon Sep 17 00:00:00 2001 From: Constantin Ponfick Date: Tue, 5 Jul 2022 13:20:52 +0200 Subject: [PATCH 2/4] added additional test case --- packages/@aws-cdk/aws-appsync/lib/resolver.ts | 2 +- .../@aws-cdk/aws-appsync/test/integ.appsync-lambda.ts | 9 ++++++++- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/packages/@aws-cdk/aws-appsync/lib/resolver.ts b/packages/@aws-cdk/aws-appsync/lib/resolver.ts index 3df7f44a068c5..a2430d8a5e8e6 100644 --- a/packages/@aws-cdk/aws-appsync/lib/resolver.ts +++ b/packages/@aws-cdk/aws-appsync/lib/resolver.ts @@ -118,7 +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, + maxBatchSize: props.maxBatchSize ? props.maxBatchSize : undefined, }); props.api.addSchemaDependency(this.resolver); if (props.dataSource) { 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 ccb39f339c01b..5e15593240de8 100644 --- a/packages/@aws-cdk/aws-appsync/test/integ.appsync-lambda.ts +++ b/packages/@aws-cdk/aws-appsync/test/integ.appsync-lambda.ts @@ -67,13 +67,20 @@ lambdaDS.createResolver({ requestMappingTemplate: appsync.MappingTemplate.lambdaRequest(requestPayload('addPost', { withArgs: true })), responseMappingTemplate, }); + lambdaDS.createResolver({ typeName: 'Post', fieldName: 'relatedPosts', requestMappingTemplate: appsync.MappingTemplate.lambdaRequest(requestPayload('relatedPosts', { withSource: true }), 'BatchInvoke'), responseMappingTemplate, - maxBatchSize: 5, }); +lambdaDS.createResolver({ + typeName: 'Comment', + fieldName: 'relatedComments', + requestMappingTemplate: appsync.MappingTemplate.lambdaRequest(requestPayload('relatedPosts', { withSource: true }), 'BatchInvoke'), + responseMappingTemplate, + maxBatchSize: 5, +}); app.synth(); \ No newline at end of file From abbb4fba842711335fc29d07d0064eef5704b994 Mon Sep 17 00:00:00 2001 From: Constantin Ponfick Date: Tue, 5 Jul 2022 14:50:26 +0200 Subject: [PATCH 3/4] added a full integration test for maxBatchSize --- packages/@aws-cdk/aws-appsync/test/appsync.lambda.graphql | 1 + .../@aws-cdk/aws-appsync/test/integ.appsync-lambda.ts | 8 ++++---- .../aws-appsync/test/verify.integ.appsync-lambda.sh | 2 +- .../test/verify/lambda-tutorial/lambda-tutorial.js | 1 + 4 files changed, 7 insertions(+), 5 deletions(-) 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 5e15593240de8..b60dd5e23223d 100644 --- a/packages/@aws-cdk/aws-appsync/test/integ.appsync-lambda.ts +++ b/packages/@aws-cdk/aws-appsync/test/integ.appsync-lambda.ts @@ -76,11 +76,11 @@ lambdaDS.createResolver({ }); lambdaDS.createResolver({ - typeName: 'Comment', - fieldName: 'relatedComments', - requestMappingTemplate: appsync.MappingTemplate.lambdaRequest(requestPayload('relatedPosts', { withSource: true }), 'BatchInvoke'), + typeName: 'Post', + fieldName: 'relatedPostsMaxBatchSize', + requestMappingTemplate: appsync.MappingTemplate.lambdaRequest(requestPayload('relatedPostsMaxBatchSize', { withSource: true }), 'BatchInvoke'), responseMappingTemplate, - maxBatchSize: 5, + 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 From 87e8609927b9da94fa2114040b6dd2e58fd044e5 Mon Sep 17 00:00:00 2001 From: Constantin Ponfick Date: Tue, 12 Jul 2022 21:20:29 +0200 Subject: [PATCH 4/4] removed redundant call --- packages/@aws-cdk/aws-appsync/lib/resolver.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/@aws-cdk/aws-appsync/lib/resolver.ts b/packages/@aws-cdk/aws-appsync/lib/resolver.ts index a2430d8a5e8e6..3df7f44a068c5 100644 --- a/packages/@aws-cdk/aws-appsync/lib/resolver.ts +++ b/packages/@aws-cdk/aws-appsync/lib/resolver.ts @@ -118,7 +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.maxBatchSize : undefined, + maxBatchSize: props.maxBatchSize, }); props.api.addSchemaDependency(this.resolver); if (props.dataSource) {