无法写入未知属性`client_mutation_id`

时间:2019-02-04 22:44:12

标签: ruby-on-rails ruby graphql

我正在尝试使用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

1 个答案:

答案 0 :(得分:2)

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

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

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,这会导致您得到错误。