From 3634639f33fcc182b7609526fb45220cfd13f515 Mon Sep 17 00:00:00 2001 From: Scott McCammon <78433109+smccammon-amplify@users.noreply.github.com> Date: Fri, 10 Nov 2023 06:56:30 -0800 Subject: [PATCH] fix: use global caching TTL by default in resolver caching config (#614) --- src/__tests__/resolvers.test.ts | 64 +++++++++++++++++++++++++++++++++ src/resources/Resolver.ts | 2 +- 2 files changed, 65 insertions(+), 1 deletion(-) diff --git a/src/__tests__/resolvers.test.ts b/src/__tests__/resolvers.test.ts index e1dff5b4..55439d64 100644 --- a/src/__tests__/resolvers.test.ts +++ b/src/__tests__/resolvers.test.ts @@ -1077,5 +1077,69 @@ describe('Resolvers', () => { } `); }); + + it('should fallback to global caching TTL', () => { + const api = new Api( + given.appSyncConfig({ + caching: { + behavior: 'PER_RESOLVER_CACHING', + ttl: 300, + }, + dataSources: { + myTable: { + name: 'myTable', + type: 'AMAZON_DYNAMODB', + config: { tableName: 'data' }, + }, + }, + }), + plugin, + ); + expect( + api.compileResolver({ + dataSource: 'myTable', + kind: 'UNIT', + type: 'Query', + field: 'user', + caching: { + keys: ['$context.identity.sub', '$context.arguments.id'], + }, + }), + ).toMatchInlineSnapshot(` + Object { + "GraphQlResolverQueryuser": Object { + "DependsOn": Array [ + "GraphQlSchema", + ], + "Properties": Object { + "ApiId": Object { + "Fn::GetAtt": Array [ + "GraphQlApi", + "ApiId", + ], + }, + "CachingConfig": Object { + "CachingKeys": Array [ + "$context.identity.sub", + "$context.arguments.id", + ], + "Ttl": 300, + }, + "DataSourceName": Object { + "Fn::GetAtt": Array [ + "GraphQlDsmyTable", + "Name", + ], + }, + "FieldName": "user", + "Kind": "UNIT", + "MaxBatchSize": undefined, + "TypeName": "Query", + }, + "Type": "AWS::AppSync::Resolver", + }, + } + `); + }); }); }); diff --git a/src/resources/Resolver.ts b/src/resources/Resolver.ts index 1a4039c8..e7f11029 100644 --- a/src/resources/Resolver.ts +++ b/src/resources/Resolver.ts @@ -67,7 +67,7 @@ export class Resolver { } else if (typeof this.config.caching === 'object') { Properties.CachingConfig = { CachingKeys: this.config.caching.keys, - Ttl: this.config.caching.ttl || this.config.caching.ttl || 3600, + Ttl: this.config.caching.ttl || this.api.config.caching?.ttl || 3600, }; } }