import * as cdk from '@aws-cdk/core' ; import lambda = require ( '@aws-cdk/aws-lambda' ) ; import { CfnApiKey , MappingTemplate , PrimaryKey , Values , GraphqlApi , Schema , FieldLogLevel } from '@aws-cdk/aws-appsync' ; import { AttributeType , BillingMode , Table } from '@aws-cdk/aws-dynamodb' ; import { join } from 'path' ; export class TheSimpleGraphQLServiceStack extends cdk . Stack { constructor ( scope , id , props ? ) { super ( scope , id , props ) ; /**
         * Create a new AppSync GraphQL API
         */ const api = new GraphqlApi ( this , 'Api' , { name : `demoapi` , logConfig : { fieldLogLevel : FieldLogLevel . ALL , } , schema : new Schema ( { filePath : join ( '__dirname' , '/../' , 'schema/schema.graphql' ) } ) , } ) ; const apiKey = new CfnApiKey ( this , 'the-simple-graphql-service-api-key' , { apiId : api . apiId } ) ; /**
         * Create new DynamoDB Table for Customer
         */ const customerTable = new Table ( this , 'CustomerTable' , { billingMode : BillingMode . PAY_PER_REQUEST , partitionKey : { name : 'id' , type : AttributeType . STRING , } , } ) ; /**
         * Add Customer DynamoDB as a Datasource for the Graphql API.
         */ const customerDS = api . addDynamoDbDataSource ( 'Customer' , customerTable ) ; customerDS . createResolver ( { typeName : 'Query' , fieldName : 'getCustomers' , requestMappingTemplate : MappingTemplate . dynamoDbScanTable ( ) , responseMappingTemplate : MappingTemplate . dynamoDbResultList ( ) , } ) ; customerDS . createResolver ( { typeName : 'Query' , fieldName : 'getCustomer' , requestMappingTemplate : MappingTemplate . dynamoDbGetItem ( 'id' , 'id' ) , responseMappingTemplate : MappingTemplate . dynamoDbResultItem ( ) , } ) ; customerDS . createResolver ( { typeName : 'Mutation' , fieldName : 'addCustomer' , requestMappingTemplate : MappingTemplate . dynamoDbPutItem ( PrimaryKey . partition ( 'id' ) . auto ( ) , Values . projecting ( 'customer' ) ) , responseMappingTemplate : MappingTemplate . dynamoDbResultItem ( ) , } ) ; customerDS . createResolver ( { typeName : 'Mutation' , fieldName : 'saveCustomer' , requestMappingTemplate : MappingTemplate . dynamoDbPutItem ( PrimaryKey . partition ( 'id' ) . is ( 'id' ) , Values . projecting ( 'customer' ) ) , responseMappingTemplate : MappingTemplate . dynamoDbResultItem ( ) , } ) ; customerDS . createResolver ( { typeName : 'Mutation' , fieldName : 'saveCustomerWithFirstOrder' , requestMappingTemplate : MappingTemplate . dynamoDbPutItem ( PrimaryKey . partition ( 'order' ) . auto ( ) . sort ( 'customer' ) . is ( 'customer.id' ) , Values . projecting ( 'order' ) . attribute ( 'referral' ) . is ( 'referral' ) ) , responseMappingTemplate : MappingTemplate . dynamoDbResultItem ( ) , } ) ; customerDS . createResolver ( { typeName : 'Mutation' , fieldName : 'removeCustomer' , requestMappingTemplate : MappingTemplate . dynamoDbDeleteItem ( 'id' , 'id' ) , responseMappingTemplate : MappingTemplate . dynamoDbResultItem ( ) , } ) ; const loyaltyLambda = new lambda . Function ( this , 'LoyaltyLambdaHandler' , { runtime : lambda . Runtime . NODEJS_12_X , code : lambda . Code . fromAsset ( 'lambda-fns' ) , handler : 'loyalty.handler' , } ) ; /**
         * Add Loyalty Lambda as a Datasource for the Graphql API.
         */ const loyaltyDS = api . addLambdaDataSource ( 'Loyalty' , loyaltyLambda ) ; loyaltyDS . createResolver ( { typeName : 'Query' , fieldName : 'getLoyaltyLevel' , requestMappingTemplate : MappingTemplate . lambdaRequest ( ) , responseMappingTemplate : MappingTemplate . lambdaResult ( ) , } ) ; new cdk . CfnOutput ( this , 'Endpoint' , { value : api . graphqlUrl } ) ; new cdk . CfnOutput ( this , 'API_Key' , { value : apiKey . attrApiKey } ) ; } }