Skip to content

fix: custom mutations event bridge guide #8346

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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"]),
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Status enum doesn't necessarily need to be all-caps SCREAM-CASE. But, the list of options needs to align everywhere.

OrderStatusChange: a.customType({
orderId: a.id().required(),
status: a.ref("OrderStatus").required(),
Expand All @@ -90,7 +90,7 @@ export const data = defineData({
```

<Callout info>
**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.
</Callout>

## Step 2 - Add your Amazon EventBridge event bus as a data source
Expand Down Expand Up @@ -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
Expand All @@ -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"],
Expand All @@ -174,7 +176,7 @@ const rule = new aws_events.CfnRule(eventStack, "MyOrderRule", {
appSyncParameters: {
graphQlOperation: `
mutation PublishOrderFromEventBridge(
$orderId: String!
$orderId: ID!
$status: String!
$message: String!
) {
Expand Down Expand Up @@ -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(),
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -300,9 +302,9 @@ Next, create the following files in your `amplify/data` folder and use the code
<BlockSwitcher>
<Block name="Subscription">

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: {},
Expand All @@ -324,7 +326,7 @@ export function request(ctx) {
events: [
{
source: "amplify.orders",
["detail-type"]: "OrderStatusChange",
detailType: "OrderStatusChange",
detail: { ...ctx.args },
},
],
Expand Down Expand Up @@ -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);
},
Expand Down