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

Rails GraphQL Update API报错:Types::ItemType#id不存在问题咨询

问题原因与解决方案

错误原因

你的UpdateItem Mutation声明返回类型为Types::ItemType,但resolve方法里调用Item.find(id).update!(...)后,返回的是布尔值true(update!执行成功时返回true,失败则抛出异常)。GraphQL收到这个布尔值后,尝试按照ItemType的定义解析你请求的id和title字段,但布尔值没有这些属性,因此抛出找不到字段实现的错误。

修复方法

1. 修改Mutation的resolve方法,返回Item实例

更新app/graphql/update_item.rb中的resolve方法,先获取Item实例,执行更新后返回该实例:

def resolve(id:, title:, description:, image_url:, artist_id:)
  item = Item.find(id)
  item.update!(title: title, description: description, image_url: image_url, artist_id: artist_id)
  item # 返回Item实例,而非update!的布尔结果
end

2. 修正GraphQL查询语句的语法错误

你的查询语句末尾多了一个多余的},修正后:

mutation{
  updateItem(input: {
    id: 13,
    title: "gfnm",
    description: "vcbnm",
    imageUrl: "fgehyerjfdbgh",
    artistId: 8
  }) {
    id
    title
  }
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.22 13:15:03