1+ import * as cdk from 'aws-cdk-lib' ;
2+ import { RemovalPolicy } from 'aws-cdk-lib' ;
3+ import { CfnApiKey , CfnGraphQLApi , CfnGraphQLSchema } from 'aws-cdk-lib/aws-appsync' ;
4+ import { LogGroup , RetentionDays } from 'aws-cdk-lib/aws-logs' ;
5+ import { Choice , Condition , LogLevel , Pass , StateMachine , StateMachineType } from 'aws-cdk-lib/aws-stepfunctions' ;
6+ import { AppSyncDataSourceStepFunctionsExpress } from './index' ;
7+
8+ const app = new cdk . App ( ) ;
9+ const stack = new cdk . Stack ( app , 'IntegAppSyncDataSourceStepFunctionsExpress' ) ;
10+
11+ const initializeTask = new Pass ( stack , 'Initialize' , {
12+ parameters : {
13+ 'values.$' : '$.values' ,
14+ 'length.$' : 'States.ArrayLength($.values)' ,
15+ 'index' : 0 ,
16+ 'result' : 0 ,
17+ } ,
18+ } ) ;
19+ const checkIndexTask = new Choice ( stack , 'Check index' ) ;
20+ const isIndexEqualsToLength = Condition . numberEqualsJsonPath ( '$.index' , '$.length' ) ;
21+ const addResultAndIncrementIndexTask = new Pass ( stack , 'Add result and increment index' , {
22+ parameters : {
23+ 'values.$' : '$.values' ,
24+ 'length.$' : '$.length' ,
25+ 'index.$' : 'States.MathAdd($.index, 1)' ,
26+ 'result.$' : 'States.MathAdd($.result, States.ArrayGetItem($.values, $.index))' ,
27+ } ,
28+ } ) ;
29+ const returnTask = new Pass ( stack , 'Return' , {
30+ outputPath : '$.result' ,
31+ } ) ;
32+ const definition = initializeTask
33+ . next ( checkIndexTask
34+ . when ( isIndexEqualsToLength , returnTask )
35+ . otherwise ( addResultAndIncrementIndexTask . next ( checkIndexTask ) ) ,
36+ ) ;
37+
38+ const sumStateMachineLogGroup = new LogGroup ( stack , 'SumStateMachineLogGroup' , {
39+ retention : RetentionDays . ONE_DAY ,
40+ removalPolicy : RemovalPolicy . DESTROY ,
41+ } ) ;
42+ const sumStateMachine = new StateMachine ( stack , 'SumStateMachine' , {
43+ definition,
44+ stateMachineType : StateMachineType . EXPRESS ,
45+ logs : {
46+ destination : sumStateMachineLogGroup ,
47+ level : LogLevel . ALL ,
48+ includeExecutionData : true ,
49+ } ,
50+ } ) ;
51+
52+ const api = new CfnGraphQLApi ( stack , 'Api' , {
53+ name : 'IntegAppSyncDataSourceStepFunctionsExpress' ,
54+ authenticationType : 'API_KEY' ,
55+ } ) ;
56+ new CfnApiKey ( stack , 'ApiKey' , {
57+ apiId : api . attrApiId ,
58+ } ) ;
59+
60+ const schema = new CfnGraphQLSchema ( stack , 'Schema' , {
61+ apiId : api . attrApiId ,
62+ definition : `
63+ schema {
64+ query: Query
65+ mutation: Mutation
66+ }
67+ type Query {
68+ sum(values: [Int!]!): Int!
69+ }
70+ type Mutation {
71+ sum(values: [Int!]!): Int!
72+ }
73+ ` ,
74+ } ) ;
75+
76+ const sfnExpressDataSource = new AppSyncDataSourceStepFunctionsExpress ( stack , 'SfnExpressDataSource' , {
77+ apiId : api . attrApiId ,
78+ } ) ;
79+ sfnExpressDataSource . addStateMachineResolver ( 'SumResolver' , {
80+ stateMachine : sumStateMachine ,
81+ schema,
82+ typeName : 'Query' ,
83+ fieldName : 'sum' ,
84+ } ) ;
0 commit comments