forked from mtliendo/cdk-kitchen-sink
-
Notifications
You must be signed in to change notification settings - Fork 2
/
APIStack.ts
87 lines (78 loc) · 2.28 KB
/
APIStack.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
import { CfnOutput, Stack, StackProps } from 'aws-cdk-lib'
import { Table } from 'aws-cdk-lib/aws-dynamodb'
import { Construct } from 'constructs'
import * as path from 'path'
import {
GraphqlApi,
Schema,
AuthorizationType,
FieldLogLevel,
MappingTemplate,
PrimaryKey,
Values,
} from '@aws-cdk/aws-appsync-alpha'
import { UserPool } from 'aws-cdk-lib/aws-cognito'
import { IRole } from 'aws-cdk-lib/aws-iam'
import { IdentityPool } from '@aws-cdk/aws-cognito-identitypool-alpha'
interface APIStackProps extends StackProps {
userpool: UserPool
sampleTable: Table
unauthenticatedRole: IRole
identityPool: IdentityPool
}
export class APIStack extends Stack {
constructor(scope: Construct, id: string, props: APIStackProps) {
super(scope, id, props)
const api = new GraphqlApi(this, 'APISamples', {
name: 'APISamples',
schema: Schema.fromAsset(path.join(__dirname, 'graphql/schema.graphql')),
authorizationConfig: {
defaultAuthorization: {
authorizationType: AuthorizationType.USER_POOL,
userPoolConfig: {
userPool: props.userpool,
},
},
additionalAuthorizationModes: [
{ authorizationType: AuthorizationType.IAM },
],
},
logConfig: {
fieldLogLevel: FieldLogLevel.ALL,
},
xrayEnabled: true,
})
const TodoDataSource = api.addDynamoDbDataSource(
'TodoDataSourceSamples',
props.sampleTable
)
api.grantQuery(props.unauthenticatedRole, 'getTodo', 'listTodos')
TodoDataSource.createResolver({
typeName: 'Query',
fieldName: 'getTodo',
requestMappingTemplate: MappingTemplate.dynamoDbGetItem('id', 'id'),
responseMappingTemplate: MappingTemplate.dynamoDbResultItem(),
})
TodoDataSource.createResolver({
typeName: 'Mutation',
fieldName: 'createTodo',
requestMappingTemplate: MappingTemplate.dynamoDbPutItem(
PrimaryKey.partition('id').auto(),
Values.projecting('input')
),
responseMappingTemplate: MappingTemplate.dynamoDbResultItem(),
})
TodoDataSource.createResolver({
typeName: 'Query',
fieldName: 'listTodos',
requestMappingTemplate: MappingTemplate.dynamoDbScanTable(),
responseMappingTemplate: MappingTemplate.dynamoDbResultList(),
})
new CfnOutput(this, 'GraphQLAPIURL', {
value: api.graphqlUrl,
})
new CfnOutput(this, 'GraphQLAPIID', {
value: api.apiId,
})
}
}