/
appsync-caching-config.test.ts
126 lines (110 loc) · 3.77 KB
/
appsync-caching-config.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
import * as path from 'path';
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';
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.SchemaFile.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_14_X,
});
});
test('Lambda resolver can be created without caching config', () => {
// WHEN
const lambdaDS = api.addLambdaDataSource('LambdaDS', func);
lambdaDS.createResolver('QueryAllPosts', {
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);
lambdaDS.createResolver('QueryAllPosts', {
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('QueryAllPosts', {
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('QueryAllPosts', {
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('QueryAllPosts', {
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}`);
});
});