|
| 1 | +import cdk = require('@aws-cdk/cdk'); |
| 2 | +import { CfnGraphQLApi, CfnApiKey, CfnGraphQLSchema, CfnDataSource, CfnResolver } from '@aws-cdk/aws-appsync'; |
| 3 | +import { Table, AttributeType, StreamViewType, BillingMode } from '@aws-cdk/aws-dynamodb'; |
| 4 | +import { Role, ServicePrincipal } from '@aws-cdk/aws-iam'; |
| 5 | + |
| 6 | + |
| 7 | +export class AppSyncCdkStack extends cdk.Stack { |
| 8 | + |
| 9 | + constructor(scope: cdk.Construct, id: string, props?: cdk.StackProps) { |
| 10 | + super(scope, id, props); |
| 11 | + |
| 12 | + const tableName = 'items' |
| 13 | + |
| 14 | + const itemsGraphQLApi = new CfnGraphQLApi(this, 'ItemsApi', { |
| 15 | + name: 'items-api', |
| 16 | + authenticationType: 'API_KEY' |
| 17 | + }); |
| 18 | + |
| 19 | + new CfnApiKey(this, 'ItemsApiKey', { |
| 20 | + apiId: itemsGraphQLApi.graphQlApiApiId |
| 21 | + }); |
| 22 | + |
| 23 | + const apiSchema = new CfnGraphQLSchema(this, 'ItemsSchema', { |
| 24 | + apiId: itemsGraphQLApi.graphQlApiApiId, |
| 25 | + definition: `type ${tableName} { |
| 26 | + ${tableName}Id: ID! |
| 27 | + name: String |
| 28 | + } |
| 29 | + type Paginated${tableName} { |
| 30 | + items: [${tableName}!]! |
| 31 | + nextToken: String |
| 32 | + } |
| 33 | + type Query { |
| 34 | + all(limit: Int, nextToken: String): Paginated${tableName}! |
| 35 | + getOne(${tableName}Id: ID!): ${tableName} |
| 36 | + } |
| 37 | + type Mutation { |
| 38 | + save(name: String!): ${tableName} |
| 39 | + delete(${tableName}Id: ID!): ${tableName} |
| 40 | + } |
| 41 | + type Schema { |
| 42 | + query: Query |
| 43 | + mutation: Mutation |
| 44 | + }` |
| 45 | + }); |
| 46 | + |
| 47 | + const itemsTable = new Table(this, 'ItemsTable', { |
| 48 | + tableName: tableName, |
| 49 | + partitionKey: { |
| 50 | + name: `${tableName}Id`, |
| 51 | + type: AttributeType.String |
| 52 | + }, |
| 53 | + billingMode: BillingMode.PayPerRequest, |
| 54 | + streamSpecification: StreamViewType.NewImage |
| 55 | + }); |
| 56 | + |
| 57 | + const itemsTableRole = new Role(this, 'ItemsDynamoDBRole', { |
| 58 | + assumedBy: new ServicePrincipal('appsync.amazonaws.com') |
| 59 | + }); |
| 60 | + |
| 61 | + itemsTableRole.attachManagedPolicy('arn:aws:iam::aws:policy/AmazonDynamoDBFullAccess'); |
| 62 | + |
| 63 | + const dataSource = new CfnDataSource(this, 'ItemsDataSource', { |
| 64 | + apiId: itemsGraphQLApi.graphQlApiApiId, |
| 65 | + name: 'ItemsDynamoDataSource', |
| 66 | + type: 'AMAZON_DYNAMODB', |
| 67 | + dynamoDbConfig: { |
| 68 | + tableName: itemsTable.tableName, |
| 69 | + awsRegion: this.region |
| 70 | + }, |
| 71 | + serviceRoleArn: itemsTableRole.roleArn |
| 72 | + }); |
| 73 | + |
| 74 | + const getOneResolver = new CfnResolver(this, 'GetOneQueryResolver', { |
| 75 | + apiId: itemsGraphQLApi.graphQlApiApiId, |
| 76 | + typeName: 'Query', |
| 77 | + fieldName: 'getOne', |
| 78 | + dataSourceName: dataSource.dataSourceName, |
| 79 | + requestMappingTemplate: `{ |
| 80 | + "version": "2017-02-28", |
| 81 | + "operation": "GetItem", |
| 82 | + "key": { |
| 83 | + "${tableName}Id": $util.dynamodb.toDynamoDBJson($ctx.args.${tableName}Id) |
| 84 | + } |
| 85 | + }`, |
| 86 | + responseMappingTemplate: `$util.toJson($ctx.result)` |
| 87 | + }); |
| 88 | + getOneResolver.addDependsOn(apiSchema); |
| 89 | + |
| 90 | + const getAllResolver = new CfnResolver(this, 'GetAllQueryResolver', { |
| 91 | + apiId: itemsGraphQLApi.graphQlApiApiId, |
| 92 | + typeName: 'Query', |
| 93 | + fieldName: 'all', |
| 94 | + dataSourceName: dataSource.dataSourceName, |
| 95 | + requestMappingTemplate: `{ |
| 96 | + "version": "2017-02-28", |
| 97 | + "operation": "Scan", |
| 98 | + "limit": $util.defaultIfNull($ctx.args.limit, 20), |
| 99 | + "nextToken": $util.toJson($util.defaultIfNullOrEmpty($ctx.args.nextToken, null)) |
| 100 | + }`, |
| 101 | + responseMappingTemplate: `$util.toJson($ctx.result)` |
| 102 | + }); |
| 103 | + getAllResolver.addDependsOn(apiSchema); |
| 104 | + |
| 105 | + const saveResolver = new CfnResolver(this, 'SaveMutationResolver', { |
| 106 | + apiId: itemsGraphQLApi.graphQlApiApiId, |
| 107 | + typeName: 'Mutation', |
| 108 | + fieldName: 'save', |
| 109 | + dataSourceName: dataSource.dataSourceName, |
| 110 | + requestMappingTemplate: `{ |
| 111 | + "version": "2017-02-28", |
| 112 | + "operation": "PutItem", |
| 113 | + "key": { |
| 114 | + "${tableName}Id": { "S": "$util.autoId()" } |
| 115 | + }, |
| 116 | + "attributeValues": { |
| 117 | + "name": $util.dynamodb.toDynamoDBJson($ctx.args.name) |
| 118 | + } |
| 119 | + }`, |
| 120 | + responseMappingTemplate: `$util.toJson($ctx.result)` |
| 121 | + }); |
| 122 | + saveResolver.addDependsOn(apiSchema); |
| 123 | + |
| 124 | + const deleteResolver = new CfnResolver(this, 'DeleteMutationResolver', { |
| 125 | + apiId: itemsGraphQLApi.graphQlApiApiId, |
| 126 | + typeName: 'Mutation', |
| 127 | + fieldName: 'delete', |
| 128 | + dataSourceName: dataSource.dataSourceName, |
| 129 | + requestMappingTemplate: `{ |
| 130 | + "version": "2017-02-28", |
| 131 | + "operation": "DeleteItem", |
| 132 | + "key": { |
| 133 | + "${tableName}Id": $util.dynamodb.toDynamoDBJson($ctx.args.${tableName}Id) |
| 134 | + } |
| 135 | + }`, |
| 136 | + responseMappingTemplate: `$util.toJson($ctx.result)` |
| 137 | + }); |
| 138 | + deleteResolver.addDependsOn(apiSchema); |
| 139 | + |
| 140 | + } |
| 141 | +} |
| 142 | + |
| 143 | +const app = new cdk.App(); |
| 144 | +new AppSyncCdkStack(app, 'AppSyncGraphQLDynamoDBExample'); |
| 145 | +app.run(); |
0 commit comments