Skip to content

Commit

Permalink
fix(appsync): empty caching config is created when not provided (#17947)
Browse files Browse the repository at this point in the history
If the `cachingConfig` property is not provided, the library is generating an empty config.

Change this to not add any config to the template.

Related to #17925.

----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
  • Loading branch information
otaviomacedo committed Dec 10, 2021
1 parent f02fcb4 commit 3a9f206
Show file tree
Hide file tree
Showing 9 changed files with 27 additions and 44 deletions.
13 changes: 9 additions & 4 deletions packages/@aws-cdk/aws-appsync/lib/resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,15 +114,20 @@ export class Resolver extends CoreConstruct {
pipelineConfig: pipelineConfig,
requestMappingTemplate: props.requestMappingTemplate ? props.requestMappingTemplate.renderTemplate() : undefined,
responseMappingTemplate: props.responseMappingTemplate ? props.responseMappingTemplate.renderTemplate() : undefined,
cachingConfig: {
cachingKeys: props.cachingConfig?.cachingKeys,
ttl: props.cachingConfig?.ttl?.toSeconds(),
},
cachingConfig: this.createCachingConfig(props.cachingConfig),
});
props.api.addSchemaDependency(this.resolver);
if (props.dataSource) {
this.resolver.addDependsOn(props.dataSource.ds);
}
this.arn = this.resolver.attrResolverArn;
}

private createCachingConfig(config?: CachingConfig) {
return config ? {
cachingKeys: config.cachingKeys,
ttl: config.ttl?.toSeconds(),
} : undefined;
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as path from 'path';
import { Template } from '@aws-cdk/assertions';
import { Match, Template } from '@aws-cdk/assertions';
import * as lambda from '@aws-cdk/aws-lambda';
import * as cdk from '@aws-cdk/core';
import { Duration } from '@aws-cdk/core';
Expand Down Expand Up @@ -29,6 +29,23 @@ describe('Lambda caching config', () => {
});
});

test('Lambda resolver can be created without caching config', () => {
// WHEN
const lambdaDS = api.addLambdaDataSource('LambdaDS', func);

lambdaDS.createResolver({
typeName: 'Query',
fieldName: 'allPosts',
});

// THEN
Template.fromStack(stack).hasResourceProperties('AWS::AppSync::Resolver', {
TypeName: 'Query',
FieldName: 'allPosts',
CachingConfig: Match.absent(),
});
});

test('Lambda resolver contains caching config with caching key and TTL', () => {
// WHEN
const lambdaDS = api.addLambdaDataSource('LambdaDS', func);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,6 @@
},
"FieldName": "getTests",
"TypeName": "Query",
"CachingConfig": {},
"DataSourceName": "ds",
"Kind": "UNIT",
"RequestMappingTemplate": "{\"version\" : \"2017-02-28\", \"operation\" : \"Scan\"}",
Expand All @@ -161,7 +160,6 @@
},
"FieldName": "addTest",
"TypeName": "Mutation",
"CachingConfig": {},
"DataSourceName": "ds",
"Kind": "UNIT",
"RequestMappingTemplate": "\n #set($input = $ctx.args.test)\n \n {\n \"version\": \"2017-02-28\",\n \"operation\": \"PutItem\",\n \"key\" : {\n \"id\" : $util.dynamodb.toDynamoDBJson($util.autoId())\n },\n \"attributeValues\": $util.dynamodb.toMapValuesJson($input)\n }",
Expand Down Expand Up @@ -225,7 +223,6 @@
},
"FieldName": "version",
"TypeName": "test",
"CachingConfig": {},
"Kind": "PIPELINE",
"PipelineConfig": {
"Functions": [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,6 @@
},
"FieldName": "getPost",
"TypeName": "Query",
"CachingConfig": {},
"DataSourceName": "LambdaDS",
"Kind": "UNIT",
"RequestMappingTemplate": "{\"version\": \"2017-02-28\", \"operation\": \"Invoke\", \"payload\": { \"field\": \"getPost\", \"arguments\": $utils.toJson($context.arguments)}}",
Expand All @@ -136,7 +135,6 @@
},
"FieldName": "allPosts",
"TypeName": "Query",
"CachingConfig": {},
"DataSourceName": "LambdaDS",
"Kind": "UNIT",
"RequestMappingTemplate": "{\"version\": \"2017-02-28\", \"operation\": \"Invoke\", \"payload\": { \"field\": \"allPosts\"}}",
Expand All @@ -158,7 +156,6 @@
},
"FieldName": "addPost",
"TypeName": "Mutation",
"CachingConfig": {},
"DataSourceName": "LambdaDS",
"Kind": "UNIT",
"RequestMappingTemplate": "{\"version\": \"2017-02-28\", \"operation\": \"Invoke\", \"payload\": { \"field\": \"addPost\", \"arguments\": $utils.toJson($context.arguments)}}",
Expand All @@ -180,7 +177,6 @@
},
"FieldName": "relatedPosts",
"TypeName": "Post",
"CachingConfig": {},
"DataSourceName": "LambdaDS",
"Kind": "UNIT",
"RequestMappingTemplate": "{\"version\": \"2017-02-28\", \"operation\": \"BatchInvoke\", \"payload\": { \"field\": \"relatedPosts\", \"source\": $utils.toJson($context.source)}}",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,6 @@
},
"FieldName": "getTests",
"TypeName": "Query",
"CachingConfig": {},
"DataSourceName": "testDataSource",
"Kind": "UNIT",
"RequestMappingTemplate": "{\"version\" : \"2017-02-28\", \"operation\" : \"Scan\"}",
Expand All @@ -154,7 +153,6 @@
},
"FieldName": "addTest",
"TypeName": "Mutation",
"CachingConfig": {},
"DataSourceName": "testDataSource",
"Kind": "UNIT",
"RequestMappingTemplate": "\n #set($input = $ctx.args.test)\n \n {\n \"version\": \"2017-02-28\",\n \"operation\": \"PutItem\",\n \"key\" : {\n \"id\" : $util.dynamodb.toDynamoDBJson($util.autoId())\n },\n \"attributeValues\": $util.dynamodb.toMapValuesJson($input)\n }",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,6 @@
},
"FieldName": "getTests",
"TypeName": "Query",
"CachingConfig": {},
"DataSourceName": "ds",
"Kind": "UNIT",
"RequestMappingTemplate": "{\"version\":\"2017-02-28\",\"operation\":\"GET\",\"path\":\"/id/post/_search\",\"params\":{\"headers\":{},\"queryString\":{},\"body\":{\"from\":0,\"size\":50}}}",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,6 @@
},
"FieldName": "getTest",
"TypeName": "Query",
"CachingConfig": {},
"DataSourceName": "testDataSource",
"Kind": "UNIT",
"RequestMappingTemplate": "{\"version\": \"2017-02-28\", \"operation\": \"GetItem\", \"key\": {\"id\": $util.dynamodb.toDynamoDBJson($ctx.args.id)}}",
Expand All @@ -185,7 +184,6 @@
},
"FieldName": "getTests",
"TypeName": "Query",
"CachingConfig": {},
"DataSourceName": "testDataSource",
"Kind": "UNIT",
"RequestMappingTemplate": "{\"version\" : \"2017-02-28\", \"operation\" : \"Scan\"}",
Expand All @@ -207,7 +205,6 @@
},
"FieldName": "addTest",
"TypeName": "Mutation",
"CachingConfig": {},
"DataSourceName": "testDataSource",
"Kind": "UNIT",
"RequestMappingTemplate": "\n #set($input = $ctx.args.test)\n \n {\n \"version\": \"2017-02-28\",\n \"operation\": \"PutItem\",\n \"key\" : {\n \"id\" : $util.dynamodb.toDynamoDBJson($util.autoId())\n },\n \"attributeValues\": $util.dynamodb.toMapValuesJson($input)\n }",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,6 @@
},
"FieldName": "getPlanets",
"TypeName": "Query",
"CachingConfig": {},
"DataSourceName": "planets",
"Kind": "UNIT",
"RequestMappingTemplate": "{\"version\" : \"2017-02-28\", \"operation\" : \"Scan\"}",
Expand All @@ -153,7 +152,6 @@
},
"FieldName": "addPlanet",
"TypeName": "Mutation",
"CachingConfig": {},
"DataSourceName": "planets",
"Kind": "UNIT",
"RequestMappingTemplate": "\n #set($input = $context.arguments)\n $util.qr($input.put(\"name\", $context.arguments.name))\n$util.qr($input.put(\"diameter\", $context.arguments.diameter))\n$util.qr($input.put(\"rotationPeriod\", $context.arguments.rotationPeriod))\n$util.qr($input.put(\"orbitalPeriod\", $context.arguments.orbitalPeriod))\n$util.qr($input.put(\"gravityPeriod\", $context.arguments.gravity))\n$util.qr($input.put(\"population\", $context.arguments.population))\n$util.qr($input.put(\"climates\", $context.arguments.climates))\n$util.qr($input.put(\"terrains\", $context.arguments.terrains))\n$util.qr($input.put(\"surfaceWater\", $context.arguments.surfaceWater))\n {\n \"version\": \"2017-02-28\",\n \"operation\": \"PutItem\",\n \"key\" : {\n \"id\" : $util.dynamodb.toDynamoDBJson($util.autoId())\n },\n \"attributeValues\": $util.dynamodb.toMapValuesJson($input)\n }",
Expand Down
Loading

0 comments on commit 3a9f206

Please sign in to comment.