I'm trying to write my first mutation in Graphql with RoR. It seems like this:
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
But every time I make request using Graphiql (like this:)
mutation createPost {
createPost(input:{
title:"dupa",
body:"dupa"
}) {
id
}
}
The post gets saved in database, but I recive an error
"error": {
"message": "can't write unknown attribute `client_mutation_id`" [...]
instead of requested id How can I solve this problem? this is my
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 link if it can help: https://github.com/giraffecms/GiraffeCMS-backend-rails/tree/blog/app/graphql
The Relay Input Object Mutations Specification lays down some requirements on what mutation inputs and outputs look like, and graphql-ruby can generate some of this boilerplate for you. In particular, you don't directly specify the type
of the mutation response; graphql-ruby generates a "payload" type for you, and you have to specify the field
s that go into it.
That is, I think it should work to say:
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 note (emphasis from original):
An argument called
clientMutationId
is always added, but it’s not passed to the resolve method. The value is re-inserted to the response. (It’s for client libraries to manage optimistic updates.)
So when your original version tries to return the model object directly, graphql-ruby tries to set #client_mutation_id
on it, and that results in the error you're getting.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With