【问题标题】:can't write unknown attribute `client_mutation_id`无法写入未知属性“client_mutation_id”
【发布时间】:2019-06-28 17:49:22
【问题描述】:

我正在尝试使用 RoR 在 Graphql 中编写我的第一个突变。好像是这样的:

app/graphql/mutations/create_post.rb

module Mutations
    class CreatePost < Mutations::BaseMutation
        argument :title, String, required: true
        argument :body, String, required: true

        type Types::PostType

        def resolve(title: nil, body: nil)
            Post.create!(title: title, body: body)
        end
    end
end

但每次我使用 Graphiql 提出请求时(像这样:)

mutation createPost {
  createPost(input:{
    title:"dupa",
    body:"dupa"
  }) {
    id
  }
}

帖子已保存在数据库中,但我收到错误消息

"error": {
    "message": "can't write unknown attribute `client_mutation_id`" [...]

而不是请求的 id 我怎么解决这个问题? 这是我的

app/graphql/mutations/base_mutation.rb

module Mutations
    class BaseMutation < GraphQL::Schema::RelayClassicMutation
    end
end

app/graphql/types/mutation_type.rb

module Types
  class MutationType < Types::BaseObject
    field :create_post, mutation: Mutations::CreatePost
  end
end

github 链接如果有帮助的话:https://github.com/giraffecms/GiraffeCMS-backend-rails/tree/blog/app/graphql

【问题讨论】:

    标签: ruby-on-rails ruby graphql


    【解决方案1】:

    Relay Input Object Mutations Specification 对变异输入和输出的外观和graphql-ruby can generate some of this boilerplate for you 提出了一些要求。特别是,您不直接指定突变响应的type; graphql-ruby 为您生成一个“有效负载”类型,您必须指定其中的 fields。

    也就是说,我认为应该这样说:

    class Mutations::CreatePost < Mutations::BaseMutation
        argument :title, String, required: true
        argument :body, String, required: true
    
        field :post, Types::PostType, null: false
    
        def resolve(title: nil, body: nil)
            post = Post.create!(title: title, body: body)
            { post: post }
        end
    end
    

    The API docs 注释(重点来自原文):

    总是添加了一个名为 clientMutationId 的参数,但它没有传递给 resolve 方法。该值被重新插入到响应中。 (用于管理乐观更新的客户端库。)

    因此,当您的原始版本尝试直接返回模型对象时,graphql-ruby 会尝试在其上设置 #client_mutation_id,这会导致您遇到错误。

    【讨论】:

      猜你喜欢
      • 2021-01-14
      • 2014-01-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多