如何用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 theeventIdandidfields in your DynamoDB table forFeedback. - As part of generating this index, the transform automatically adds the
eventIdparameter to youronCreateFeedbacksubscription (and will also add it toonUpdateFeedback/onDeleteFeedbackif 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
eventIdparameter to only receive updates for feedback entries linked to that specific event.
内容的提问来源于stack exchange,提问作者odieatla
相关产品推荐
相关产品推荐

