This repository has been archived by the owner on Jun 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
GQLLambdaResolver.ts
70 lines (64 loc) · 2.09 KB
/
GQLLambdaResolver.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
import { Construct } from '@aws-cdk/core'
import { PolicyStatement, Role, ServicePrincipal } from '@aws-cdk/aws-iam'
import { CfnDataSource, CfnGraphQLApi, CfnResolver } from '@aws-cdk/aws-appsync'
import { Function } from '@aws-cdk/aws-lambda'
export class GQLLambdaResolver extends Construct {
public readonly dataSource: CfnDataSource
constructor(
parent: Construct,
graphqlApi: CfnGraphQLApi,
field: string,
type: 'Mutation' | 'Query',
lambda: Function,
) {
super(parent, `${field}${type}Resolver`)
const apiRole = new Role(this, 'Role', {
assumedBy: new ServicePrincipal('appsync.amazonaws.com'),
})
apiRole.addToPolicy(
new PolicyStatement({
actions: [
'logs:CreateLogGroup',
'logs:CreateLogStream',
'logs:PutLogEvents',
],
resources: [`arn:aws:logs:*:*:/aws/lambda/*`],
}),
)
apiRole.addToPolicy(
new PolicyStatement({
actions: ['lambda:InvokeFunction'],
resources: [lambda.functionArn],
}),
)
this.dataSource = new CfnDataSource(this, 'DataSource', {
apiId: graphqlApi.attrApiId,
name: `${field}${type}`,
type: 'AWS_LAMBDA',
serviceRoleArn: apiRole.roleArn,
lambdaConfig: {
lambdaFunctionArn: lambda.functionArn,
},
})
const resolver = new CfnResolver(this, 'Resolver', {
apiId: graphqlApi.attrApiId,
typeName: type,
fieldName: field,
dataSourceName: this.dataSource.name,
requestMappingTemplate:
'#set($payload = {})\n' +
'#foreach ($key in $context.arguments.keySet())\n' +
'$util.qr($payload.put($key, $context.arguments.get($key)))\n' +
'#end\n' +
'$util.qr($payload.put("cognitoIdentityId", $context.identity.cognitoIdentityId))\n' +
'{"version" : "2018-05-29", "operation": "Invoke", "payload": $util.toJson($payload)}',
responseMappingTemplate:
'#if( $context.result && $context.result.errorMessage )\n' +
' $utils.error($context.result.errorMessage, $context.result.errorType, $context.result.data, $context.result.errorInfo)\n' +
'#else\n' +
' $utils.toJson($context.result)\n' +
'#end',
})
resolver.node.addDependency(this.dataSource)
}
}