Skip to content

Commit

Permalink
chore: add integ task
Browse files Browse the repository at this point in the history
  • Loading branch information
riywo committed Nov 28, 2022
1 parent a0b8561 commit fa8df80
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .projen/tasks.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions .projenrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,9 @@ const project = new awscdk.AwsCdkConstructLibrary({
packageName: '@opsbr/cdk-appsync-data-source-sfn-express',
license: 'Apache-2.0',
stability: 'experimental',
scripts: {
integ: 'npx cdk deploy --app ./lib/integ.default.js',
},
});
project.addGitIgnore('/cdk.out/');
project.synth();
1 change: 1 addition & 0 deletions package.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

84 changes: 84 additions & 0 deletions src/integ.default.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
import * as cdk from 'aws-cdk-lib';
import { RemovalPolicy } from 'aws-cdk-lib';
import { CfnApiKey, CfnGraphQLApi, CfnGraphQLSchema } from 'aws-cdk-lib/aws-appsync';
import { LogGroup, RetentionDays } from 'aws-cdk-lib/aws-logs';
import { Choice, Condition, LogLevel, Pass, StateMachine, StateMachineType } from 'aws-cdk-lib/aws-stepfunctions';
import { AppSyncDataSourceStepFunctionsExpress } from './index';

const app = new cdk.App();
const stack = new cdk.Stack(app, 'IntegAppSyncDataSourceStepFunctionsExpress');

const initializeTask = new Pass(stack, 'Initialize', {
parameters: {
'values.$': '$.values',
'length.$': 'States.ArrayLength($.values)',
'index': 0,
'result': 0,
},
});
const checkIndexTask = new Choice(stack, 'Check index');
const isIndexEqualsToLength = Condition.numberEqualsJsonPath('$.index', '$.length');
const addResultAndIncrementIndexTask = new Pass(stack, 'Add result and increment index', {
parameters: {
'values.$': '$.values',
'length.$': '$.length',
'index.$': 'States.MathAdd($.index, 1)',
'result.$': 'States.MathAdd($.result, States.ArrayGetItem($.values, $.index))',
},
});
const returnTask = new Pass(stack, 'Return', {
outputPath: '$.result',
});
const definition = initializeTask
.next(checkIndexTask
.when(isIndexEqualsToLength, returnTask)
.otherwise(addResultAndIncrementIndexTask.next(checkIndexTask)),
);

const sumStateMachineLogGroup = new LogGroup(stack, 'SumStateMachineLogGroup', {
retention: RetentionDays.ONE_DAY,
removalPolicy: RemovalPolicy.DESTROY,
});
const sumStateMachine = new StateMachine(stack, 'SumStateMachine', {
definition,
stateMachineType: StateMachineType.EXPRESS,
logs: {
destination: sumStateMachineLogGroup,
level: LogLevel.ALL,
includeExecutionData: true,
},
});

const api = new CfnGraphQLApi(stack, 'Api', {
name: 'IntegAppSyncDataSourceStepFunctionsExpress',
authenticationType: 'API_KEY',
});
new CfnApiKey(stack, 'ApiKey', {
apiId: api.attrApiId,
});

const schema = new CfnGraphQLSchema(stack, 'Schema', {
apiId: api.attrApiId,
definition: `
schema {
query: Query
mutation: Mutation
}
type Query {
sum(values: [Int!]!): Int!
}
type Mutation {
sum(values: [Int!]!): Int!
}
`,
});

const sfnExpressDataSource = new AppSyncDataSourceStepFunctionsExpress(stack, 'SfnExpressDataSource', {
apiId: api.attrApiId,
});
sfnExpressDataSource.addStateMachineResolver('SumResolver', {
stateMachine: sumStateMachine,
schema,
typeName: 'Query',
fieldName: 'sum',
});

0 comments on commit fa8df80

Please sign in to comment.