Skip to content

Instantly share code, notes, and snippets.

@harigopal
Last active November 6, 2019 19:56
Show Gist options
  • Save harigopal/d64f6273011582b657af7a8a0bc30797 to your computer and use it in GitHub Desktop.
Save harigopal/d64f6273011582b657af7a8a0bc30797 to your computer and use it in GitHub Desktop.
Conventions for setting up a GraphQL Server on Rails - CreateCommentMutator
# app/queries/create_comment_mutator.rb
class CreateCommentMutator < ApplicationQuery
property :post_id
property :comment, validates: { length: { minimum: 1, maximum: 500 } }
validate :post_belongs_to_team
def post_belongs_to_team
return if post.present?
errors[:base] << 'Could not find a post in your team with that ID'
end
def create_comment
Posts::CreateCommentService.new(post).create(current_user, comment)
end
def authorized?
current_user.present?
end
private
def post
current_user.team.posts.find_by(id: post_id)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment