diff --git a/src/pages/[platform]/build-a-backend/data/custom-business-logic/connect-eventbridge-datasource/index.mdx b/src/pages/[platform]/build-a-backend/data/custom-business-logic/connect-eventbridge-datasource/index.mdx index 44684c53242..442aaee5bf9 100644 --- a/src/pages/[platform]/build-a-backend/data/custom-business-logic/connect-eventbridge-datasource/index.mdx +++ b/src/pages/[platform]/build-a-backend/data/custom-business-logic/connect-eventbridge-datasource/index.mdx @@ -67,7 +67,7 @@ const schema = a.schema({ }) .authorization(allow => [allow.publicApiKey()]), // highlight-start - OrderStatus: a.enum(["OrderPending", "OrderShipped", "OrderDelivered"]), + OrderStatus: a.enum(["PENDING", "SHIPPED", "DELIVERED"]), OrderStatusChange: a.customType({ orderId: a.id().required(), status: a.ref("OrderStatus").required(), @@ -90,7 +90,7 @@ export const data = defineData({ ``` -**NOTE:** At least one query is required for a schema to be valid. Otherwise, deployments will fail a schema error. The Amplify Data schema is auto-generated with a `Todo` model and corresponding queries under the hood. You can leave the `Todo` model in the schema until you add the first custom query to the schema in the next steps. +**NOTE:** At least one query is required for a schema to be valid. Otherwise, deployments will fail with a schema error. The Amplify Data schema is auto-generated with a `Todo` model and corresponding queries under the hood. You can leave the `Todo` model in the schema until you add the first custom query to the schema in the next steps. ## Step 2 - Add your Amazon EventBridge event bus as a data source @@ -127,14 +127,16 @@ const eventBus = aws_events.EventBus.fromEventBusName( // Add the EventBridge data source // highlight-start -backend.data.addEventBridgeDataSource("MyEventBridgeDataSource", eventBus); +backend.data.addEventBridgeDataSource("EventBridgeDataSource", eventBus); // highlight-end -// Create a policy statement to allow invoking the AppSync API's mutations +// Create a policy statement to allow invoking the AppSync API const policyStatement = new PolicyStatement({ effect: Effect.ALLOW, actions: ["appsync:GraphQL"], - resources: [`${backend.data.resources.graphqlApi.arn}/types/Mutation/*`], + resources: [ + `${backend.data.resources.graphqlApi.arn}/types/*`, + ], }); // Create a role for the EventBus to assume @@ -158,7 +160,7 @@ const rule = new aws_events.CfnRule(eventStack, "MyOrderRule", { https://docs.aws.amazon.com/AmazonS3/latest/userguide/ev-events.html */ - ["detail-type"]: ["OrderStatusChange"], + "detail-type": ["OrderStatusChange"], detail: { orderId: [{ exists: true }], status: ["PENDING", "SHIPPED", "DELIVERED"], @@ -174,7 +176,7 @@ const rule = new aws_events.CfnRule(eventStack, "MyOrderRule", { appSyncParameters: { graphQlOperation: ` mutation PublishOrderFromEventBridge( - $orderId: String! + $orderId: ID! $status: String! $message: String! ) { @@ -216,14 +218,14 @@ The `appSyncParameters` property specifies the mutation to invoke when the event Now that your event bus has been added as a data source, you can reference it in custom queries and mutations using the `a.handler.custom()` modifier which accepts the name of the data source and an entry point for your resolver. -Use the following code to add `publishOrderToEventBridge` and `publishOrderFromEventBridge` custom mutations, and an `onOrderStatusChange` custom subscription to your schema: +Use the following code to add `publishOrderToEventBridge` and `publishOrderFromEventBridge` custom mutations, and an `onOrderFromEventBridge` custom subscription to your schema: ```ts title="amplify/data/resource.ts" import { type ClientSchema, a, defineData } from "@aws-amplify/backend"; const schema = a.schema({ // ... - OrderStatus: a.enum(["OrderPending", "OrderShipped", "OrderDelivered"]), + OrderStatus: a.enum(["PENDING", "SHIPPED", "DELIVERED"]), OrderStatusChange: a.customType({ orderId: a.id().required(), status: a.ref("OrderStatus").required(), @@ -253,7 +255,7 @@ const schema = a.schema({ message: a.string().required(), }) .returns(a.ref("OrderStatusChange")) - .authorization((allow) => [allow.publicApiKey(), allow.guest()]) + .authorization((allow) => [allow.publicApiKey()]) .handler( a.handler.custom({ entry: "./publishOrderFromEventBridge.js", @@ -300,9 +302,9 @@ Next, create the following files in your `amplify/data` folder and use the code -The following code defines the custom business logic handler for the `onOrderStatusChange` subscription. Since the subscription uses a None data source the `response` function is empty as the subscription does not require any additional processing. +The following code defines the custom business logic handler for the `onOrderFromEventBridge` subscription. Since the subscription uses a None data source the `response` function is empty as the subscription does not require any additional processing. -```js title="amplify/data/onOrderStatusChange.js" +```js title="amplify/data/onOrderFromEventBridge.js" export function request(ctx) { return { payload: {}, @@ -324,7 +326,7 @@ export function request(ctx) { events: [ { source: "amplify.orders", - ["detail-type"]: "OrderStatusChange", + detailType: "OrderStatusChange", detail: { ...ctx.args }, }, ], @@ -374,7 +376,7 @@ To subscribe to events from your event bus, you can use the `client.subscription ```ts title="App.tsx" // Subscribe to the mutations triggered by the EventBridge rule -const sub = client.subscriptions.onOrderStatusChange().subscribe({ +const sub = client.subscriptions.onOrderFromEventBridge().subscribe({ next: (data) => { console.log(data); },