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
相关产品推荐
相关产品推荐

