1- export class Hello {
2- public sayHello ( ) {
3- return 'hello, world!' ;
1+ import { Stack } from 'aws-cdk-lib' ;
2+ import { CfnDataSource , CfnGraphQLSchema , CfnResolver } from 'aws-cdk-lib/aws-appsync' ;
3+ import { PolicyStatement , Role , ServicePrincipal } from 'aws-cdk-lib/aws-iam' ;
4+ import { StateMachine , StateMachineType } from 'aws-cdk-lib/aws-stepfunctions' ;
5+ import { Construct } from 'constructs' ;
6+
7+ export interface AppSyncDataSourceStepFunctionsExpressProps {
8+ readonly apiId : string ;
9+ }
10+
11+ export interface AddStateMachineResolverProps {
12+ readonly stateMachine : StateMachine ;
13+ readonly schema : CfnGraphQLSchema ;
14+ readonly typeName : string ;
15+ readonly fieldName : string ;
16+ }
17+
18+ export class AppSyncDataSourceStepFunctionsExpress extends Construct {
19+ readonly role : Role ;
20+ readonly dataSource : CfnDataSource ;
21+
22+ constructor ( scope : Construct , id : string , props : AppSyncDataSourceStepFunctionsExpressProps ) {
23+ super ( scope , id ) ;
24+ this . role = new Role ( this , 'ServiceRole' , {
25+ assumedBy : new ServicePrincipal ( 'appsync.amazonaws.com' ) ,
26+ } ) ;
27+ this . role . addToPolicy ( new PolicyStatement ( {
28+ resources : [ '*' ] ,
29+ actions : [ 'states:StartSyncExecution' ] ,
30+ } ) ) ;
31+ const region = Stack . of ( this ) . region ;
32+ this . dataSource = new CfnDataSource ( this , 'SyncStatesApi' , {
33+ apiId : props . apiId ,
34+ name : 'SyncStatesApi' ,
35+ type : 'HTTP' ,
36+ httpConfig : {
37+ endpoint : `https://sync-states.${ region } .amazonaws.com` ,
38+ authorizationConfig : {
39+ authorizationType : 'AWS_IAM' ,
40+ awsIamConfig : {
41+ signingRegion : region ,
42+ signingServiceName : 'states' ,
43+ } ,
44+ } ,
45+ } ,
46+ serviceRoleArn : this . role . roleArn ,
47+ } ) ;
48+ }
49+
50+ addStateMachineResolver ( id : string , props : AddStateMachineResolverProps ) {
51+ const { stateMachine, schema, typeName, fieldName } = props ;
52+ if ( stateMachine . stateMachineType !== StateMachineType . EXPRESS ) {
53+ throw new Error ( `stateMachine must be EXPRESS type. (${ stateMachine . stateMachineName } is ${ stateMachine . stateMachineType } )` ) ;
54+ }
55+ const resolver = new CfnResolver ( this , id , {
56+ apiId : this . dataSource . apiId ,
57+ typeName,
58+ fieldName,
59+ dataSourceName : this . dataSource . name ,
60+ kind : 'UNIT' ,
61+ requestMappingTemplate : `
62+ {
63+ "version": "2018-05-29",
64+ "method": "POST",
65+ "resourcePath": "/",
66+ "params": {
67+ "headers": {
68+ "X-Amz-Target": "AWSStepFunctions.StartSyncExecution",
69+ "Content-Type": "application/x-amz-json-1.0"
70+ },
71+ "body": $util.toJson({
72+ "stateMachineArn": "${ stateMachine . stateMachineArn } ",
73+ "input": $util.toJson($ctx.args)
74+ })
75+ }
76+ }
77+ ` ,
78+ responseMappingTemplate : `
79+ $util.parseJson($ctx.result.body).output
80+ ` ,
81+ } ) ;
82+ resolver . addDependsOn ( schema ) ;
83+ return resolver ;
484 }
585}
0 commit comments