You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用GraphQL Transform让AWS AppSync自动生成带参数的订阅?

Answer

To generate a subscription with the eventId parameter for filtering specific events, you need to adjust your Feedback model to include a @key directive that references the eventId field. Here's how to do it step by step:

1. Update Your Feedback Model

Modify your existing Feedback type to explicitly define the eventId field (the foreign key created by your @connection) and add a @key directive that includes eventId as part of the key:

type Feedback @model @key(fields: ["eventId", "id"]) {
  id: ID!
  event: Event! @connection(name: "EventFeedbacks")
  eventId: ID! # Explicit foreign key field (required for the @key directive)
  submittedDate: AWSDateTime!
}

2. Regenerate Your Schema

Run the appropriate command to re-generate your GraphQL schema and client code (depending on your workflow):

amplify codegen
# Or if you're pushing changes to the cloud:
amplify push

What This Does

  • The @key(fields: ["eventId", "id"]) directive tells the GraphQL Transform to create a secondary index on the eventId and id fields in your DynamoDB table for Feedback.
  • As part of generating this index, the transform automatically adds the eventId parameter to your onCreateFeedback subscription (and will also add it to onUpdateFeedback/onDeleteFeedback if those exist).

Resulting Subscription Schema

After regeneration, your subscription will match exactly what you wanted:

type Subscription {
  onCreateFeedback(eventId: ID): Feedback @aws_subscribe(mutations: ["createFeedback"])
}

Notes

  • You don’t need to manually edit the subscription schema—this is all handled automatically by the transform when you define the @key.
  • When using this subscription, pass the eventId parameter to only receive updates for feedback entries linked to that specific event.

内容的提问来源于stack exchange,提问作者odieatla

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.11 09:01:42