|
1 |
| -# cdk-appsync-data-source-sfn-express |
| 1 | +[](https://badge.fury.io/js/@opsbr%2Fcdk-appsync-data-source-sfn-express) [](https://github.com/opsbr/cdk-appsync-data-source-sfn-express/actions/workflows/release.yml) |
| 2 | + |
| 3 | +# cdk-appsync-data-source-sfn-express |
| 4 | + |
| 5 | +A CDK construct to create an AWS AppSync data source to call AWS Step Functions express workflows. |
| 6 | + |
| 7 | +[](https://constructs.dev/packages/@opsbr/cdk-appsync-data-source-sfn-express) |
| 8 | +## What is this? |
| 9 | + |
| 10 | +This construct provides an HTTP data source for AWS AppSync that uses IAM authentication and |
| 11 | +calls AWS Step Functions API - `StartSyncExecution`. This is a stop-gap solution until AWS |
| 12 | +provides a native integration with this API like they have for AWS Lambda or Amazon DynamoDB. |
| 13 | + |
| 14 | +Then, the construct can easily create a resolver to call this API, which means you can build |
| 15 | +AppSync GraphQL API by running a Step Functions state machine synchronously just like Amazon |
| 16 | +API Gateway already has, too. |
| 17 | + |
| 18 | +Also, this is a showcase for how to use HTTP data source for non-RESTful AWS endpoints. |
| 19 | + |
| 20 | +## Usage |
| 21 | + |
| 22 | +```typescript |
| 23 | +declare const api: appsync.CfnGraphQLApi; |
| 24 | +declare const stateMachine: stepfunctions.StateMachine; |
| 25 | + |
| 26 | +const schema = new appsync.CfnGraphQLSchema(stack, 'Schema', { |
| 27 | + apiId: api.attrApiId, |
| 28 | + definition: ` |
| 29 | + type Query { |
| 30 | + getPost(id: Int): Post |
| 31 | + } |
| 32 | + type Post { |
| 33 | + id: Int |
| 34 | + title: String |
| 35 | + } |
| 36 | + `, |
| 37 | +}); |
| 38 | + |
| 39 | +const sfnExpressDataSource = new AppSyncDataSourceStepFunctionsExpress(stack, 'SfnExpressDataSource', { |
| 40 | + apiId: api.attrApiId, |
| 41 | +}); |
| 42 | + |
| 43 | +sfnExpressDataSource.createStateMachineResolver('SumResolver', { |
| 44 | + stateMachine: sumStateMachine, |
| 45 | + schema, |
| 46 | + typeName: 'Query', |
| 47 | + fieldName: 'getPost', |
| 48 | +}); |
| 49 | +``` |
| 50 | + |
| 51 | +When queried, the specified state machine receives input as the arguments of `getPost()` e.g.: |
| 52 | + |
| 53 | +```graphql |
| 54 | +query MyQuery { |
| 55 | + getPost(id: 1) |
| 56 | +} |
| 57 | +``` |
| 58 | + |
| 59 | +is transformed to: |
| 60 | + |
| 61 | +```json |
| 62 | +{ |
| 63 | + "id": 1 |
| 64 | +} |
| 65 | +``` |
| 66 | + |
| 67 | +And the state machine should return the output like below: |
| 68 | + |
| 69 | +```json |
| 70 | +{ |
| 71 | + "id": 1, |
| 72 | + "title": "Hello world!" |
| 73 | +} |
| 74 | +``` |
| 75 | + |
| 76 | +Then, AppSync returns the output as the return type of the query: |
| 77 | + |
| 78 | +```graphql |
| 79 | +{ |
| 80 | + "data": { |
| 81 | + "getPost": { |
| 82 | + "id": 1, |
| 83 | + "title": "Hello world!" |
| 84 | + } |
| 85 | + } |
| 86 | +} |
| 87 | +``` |
| 88 | + |
| 89 | +## Notes |
| 90 | + |
| 91 | +- The state machine must be EXPRESS type. See [Step Functions document](https://docs.aws.amazon.com/step-functions/latest/dg/concepts-standard-vs-express.html) for more details. |
| 92 | +- Currently, this construct doesn't have a good error handling on the response mapping. |
| 93 | + - We need to handle the case when the workflow fails because `StartSyncExecution` returns 200. See [Step Functions document](https://docs.aws.amazon.com/step-functions/latest/apireference/API_StartSyncExecution.html). |
| 94 | + |
| 95 | +## Authors |
| 96 | + |
| 97 | +OpsBR Software Technology Inc. Visit our website: [https://opsbr.com](https://opsbr.com) |
| 98 | + |
| 99 | +## License |
| 100 | + |
| 101 | +Apache 2.0 |
0 commit comments