我正试图用RoR在Graphql中编写我的第一个变异。看起来是这样的:
app/graphql/突变/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/突变/碱基变异
module Mutations
class BaseMutation < GraphQL::Schema::RelayClassicMutation
end
endapp/graphql/type/诱变_type.app
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
发布于 2019-02-05 02:49:06
继电器输入对象突变规范规定了一些关于突变输入和输出的要求,以及graphql-ruby可以为您生成一些这种样板。。特别是,您不直接指定突变响应的type;graphql为您生成一个“有效负载”类型,并且您必须指定进入其中的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
endAPI文档注释(从原文中强调):
一个名为
clientMutationId的参数总是会被添加,但是它不会传递给resolve方法。该值将重新插入到响应中。(客户端库负责管理乐观更新。)
因此,当您的原始版本试图直接返回模型对象时,graphql尝试在其上设置#client_mutation_id,这将导致您所得到的错误。
https://stackoverflow.com/questions/54525442
复制相似问题