-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(appsync): add caching config to AppSync resolvers (#17815)
While trying to add caching config to some of my application's resolvers, I discovered that the BaseResolverProps do not include caching configuration like the CfnResolver does. This PR adds this missing caching configuration to the BaseResolverProps and adds the configuration as part of the creation of the CfnResolver. ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
- Loading branch information
1 parent
5737c33
commit 52b535b
Showing
12 changed files
with
200 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { Duration } from '@aws-cdk/core'; | ||
|
||
/** | ||
* CachingConfig for AppSync resolvers | ||
*/ | ||
export interface CachingConfig { | ||
/** | ||
* The caching keys for a resolver that has caching enabled. | ||
* Valid values are entries from the $context.arguments, $context.source, and $context.identity maps. | ||
* | ||
* @default - No caching keys | ||
*/ | ||
readonly cachingKeys?: string[]; | ||
|
||
/** | ||
* The TTL in seconds for a resolver that has caching enabled. | ||
* Valid values are between 1 and 3600 seconds. | ||
* | ||
* @default - No TTL | ||
*/ | ||
readonly ttl?: Duration; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export const CONTEXT_ARGUMENTS_CACHING_KEY = '$context.arguments'; | ||
export const CONTEXT_SOURCE_CACHING_KEY = '$context.source'; | ||
export const CONTEXT_IDENTITY_CACHING_KEY = '$context.identity'; | ||
export const BASE_CACHING_KEYS = [CONTEXT_ARGUMENTS_CACHING_KEY, CONTEXT_SOURCE_CACHING_KEY, CONTEXT_IDENTITY_CACHING_KEY]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
109 changes: 109 additions & 0 deletions
109
packages/@aws-cdk/aws-appsync/test/appsync-caching-config.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
import * as path from 'path'; | ||
import { 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'; | ||
import * as appsync from '../lib'; | ||
|
||
let stack: cdk.Stack; | ||
let api: appsync.GraphqlApi; | ||
|
||
beforeEach(() => { | ||
// GIVEN | ||
stack = new cdk.Stack(); | ||
api = new appsync.GraphqlApi(stack, 'api', { | ||
name: 'api', | ||
schema: appsync.Schema.fromAsset(path.join(__dirname, 'appsync.lambda.graphql')), | ||
}); | ||
}); | ||
|
||
describe('Lambda caching config', () => { | ||
// GIVEN | ||
let func: lambda.Function; | ||
|
||
beforeEach(() => { | ||
func = new lambda.Function(stack, 'func', { | ||
code: lambda.Code.fromAsset(path.join(__dirname, 'verify/lambda-tutorial')), | ||
handler: 'lambda-tutorial.handler', | ||
runtime: lambda.Runtime.NODEJS_12_X, | ||
}); | ||
}); | ||
|
||
test('Lambda resolver contains caching config with caching key and TTL', () => { | ||
// WHEN | ||
const lambdaDS = api.addLambdaDataSource('LambdaDS', func); | ||
|
||
lambdaDS.createResolver({ | ||
typeName: 'Query', | ||
fieldName: 'allPosts', | ||
cachingConfig: { | ||
cachingKeys: ['$context.arguments', '$context.source', '$context.identity'], | ||
ttl: Duration.seconds(300), | ||
}, | ||
}); | ||
|
||
// THEN | ||
Template.fromStack(stack).hasResourceProperties('AWS::AppSync::Resolver', { | ||
TypeName: 'Query', | ||
FieldName: 'allPosts', | ||
CachingConfig: { | ||
CachingKeys: ['$context.arguments', '$context.source', '$context.identity'], | ||
Ttl: 300, | ||
}, | ||
}); | ||
}); | ||
|
||
test('Lambda resolver throws error when caching config with TTL is less than 1 second', () => { | ||
// WHEN | ||
const ttlInSconds = 0; | ||
const lambdaDS = api.addLambdaDataSource('LambdaDS', func); | ||
|
||
// THEN | ||
expect(() => { | ||
lambdaDS.createResolver({ | ||
typeName: 'Query', | ||
fieldName: 'allPosts', | ||
cachingConfig: { | ||
cachingKeys: ['$context.identity'], | ||
ttl: Duration.seconds(0), | ||
}, | ||
}); | ||
}).toThrowError(`Caching config TTL must be between 1 and 3600 seconds. Received: ${ttlInSconds}`); | ||
}); | ||
|
||
test('Lambda resolver throws error when caching config with TTL is greater than 3600 seconds', () => { | ||
// WHEN | ||
const ttlInSconds = 4200; | ||
const lambdaDS = api.addLambdaDataSource('LambdaDS', func); | ||
|
||
// THEN | ||
expect(() => { | ||
lambdaDS.createResolver({ | ||
typeName: 'Query', | ||
fieldName: 'allPosts', | ||
cachingConfig: { | ||
cachingKeys: ['$context.identity'], | ||
ttl: Duration.seconds(ttlInSconds), | ||
}, | ||
}); | ||
}).toThrowError(`Caching config TTL must be between 1 and 3600 seconds. Received: ${ttlInSconds}`); | ||
}); | ||
|
||
test('Lambda resolver throws error when caching config has invalid caching keys', () => { | ||
// WHEN | ||
const invalidCachingKeys = ['$context.metadata']; | ||
const lambdaDS = api.addLambdaDataSource('LambdaDS', func); | ||
|
||
// THEN | ||
expect(() => { | ||
lambdaDS.createResolver({ | ||
typeName: 'Query', | ||
fieldName: 'allPosts', | ||
cachingConfig: { | ||
cachingKeys: invalidCachingKeys, | ||
ttl: Duration.seconds(300), | ||
}, | ||
}); | ||
}).toThrowError(`Caching config keys must begin with $context.arguments, $context.source or $context.identity. Received: ${invalidCachingKeys}`); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.