Skip to content

Commit

Permalink
feat(appsync): set max batch size when using batch invoke (#20995)
Browse files Browse the repository at this point in the history
closes #20467

----

### All Submissions:

* [x] Have you followed the guidelines in our [Contributing guide?](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md)

### Adding new Unconventional Dependencies:

* [ ] This PR adds new unconventional dependencies following the process described [here](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md/#adding-new-unconventional-dependencies)

### New Features

* [x] Have you added the new feature to an [integration test](https://github.com/aws/aws-cdk/blob/main/INTEGRATION_TESTS.md)?
	* [x] Did you use `yarn integ` to deploy the infrastructure and generate the snapshot (i.e. `yarn integ` without `--dry-run`)?

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
  • Loading branch information
cponfick committed Jul 13, 2022
1 parent 5f0eff2 commit 69d25a6
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 1 deletion.
7 changes: 7 additions & 0 deletions packages/@aws-cdk/aws-appsync/lib/resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
});
});
});
1 change: 1 addition & 0 deletions packages/@aws-cdk/aws-appsync/test/appsync.lambda.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,5 @@ type Post {
ups: Int
downs: Int
relatedPosts: [Post]
relatedPostsMaxBatchSize: [Post]
}
8 changes: 8 additions & 0 deletions packages/@aws-cdk/aws-appsync/test/integ.appsync-lambda.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +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,
});

lambdaDS.createResolver({
typeName: 'Post',
fieldName: 'relatedPostsMaxBatchSize',
requestMappingTemplate: appsync.MappingTemplate.lambdaRequest(requestPayload('relatedPostsMaxBatchSize', { withSource: true }), 'BatchInvoke'),
responseMappingTemplate,
maxBatchSize: 2,
});

app.synth();
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 69d25a6

Please sign in to comment.