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

AWS AppSync报错:指令“aws_subscribe”不可用于FIELD_DEFINITION

解决AWS AppSync中aws_subscribe指令的报错问题

嘿,我刚碰到过类似的问题,这个报错在AWS AppSync里挺常见的,我来帮你理一理!

错误原因

Directive "aws_subscribe" may not be used on FIELD_DEFINITION这个报错的核心原因,是你把aws_subscribe指令用错了位置——这个指令只能用在Subscription类型的字段上,绝对不能放在Query或Mutation的字段定义里。

从你给出的Schema来看,目前只定义了Mutation、Query等类型,还没有添加Subscription类型,大概率是你想实现订阅功能,但不小心把指令加到了错误的位置上。

解决方案

首先,你需要在Schema中新增Subscription类型,然后在这个类型的字段上使用aws_subscribe,指定触发订阅的Mutation操作。下面是结合你的现有Schema修改后的完整示例:

type Mutation {
  deleteParcel(geoHash: String!, type_id: String!): Parcel
  addParcel(input: ParcelInput!): Parcel
  batchAddParcels(parcels: [ParcelInput]): [Parcel]
}

type Parcel {
  geoHash: String!
  type_id: String!
}

type ParcelConnection {
  items: [Parcel]
}

input ParcelInput {
  geoHash: String!
  type_id: String!
}

input ParcelsInput {
  parcels: [ParcelInput]
}

type Query {
  getNearbyParcels(geoHash: String!): ParcelConnection # 补全你未完成的Query字段
}

# 新增Subscription类型,这才是aws_subscribe的正确使用位置
type Subscription {
  # 监听addParcel操作完成后的Parcel数据变化
  onParcelAdded: Parcel
    @aws_subscribe(mutations: ["addParcel"])
  # 监听deleteParcel操作完成后的Parcel数据变化
  onParcelDeleted: Parcel
    @aws_subscribe(mutations: ["deleteParcel"])
}

额外注意事项

  • 确保aws_subscribe里指定的mutation名称(比如addParcel)和你Mutation类型中的字段名完全一致(大小写敏感)
  • 如果你的报错是因为不小心在Query或Mutation的字段上添加了aws_subscribe,直接移除该指令就能解决问题
  • 订阅字段的返回类型要和触发它的mutation的返回类型匹配(比如上面的onParcelAdded返回Parcel,和addParcel的返回类型一致)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.26 10:43:36