published: 3/13/2019last updated: 4/10/2019

Destroy mutation

This chapter we are doing just the same things we've already done!

generate mutation

rails g graphql:mutation destroy_post

Edit mutation destory_post.rb

module Mutations # Don't forget to change to Mutations::BaseMutation class DestroyPost < Mutations::BaseMutation def ready?(**_args) if !context[:current_user] raise GraphQL::ExecutionError, "You need to login!" else true end end # Just returning id is okay field :id, ID, null: true argument :id, ID, required: true def resolve(id:) post = Post.find(id) if post.user != context[:current_user] raise GraphQL::ExecutionError, "You are not authorized!" end post.destroy { id: id, } end end end

Test it!


query posts

*There is no id: "1" post.