Last active
November 6, 2019 19:56
-
-
Save harigopal/d64f6273011582b657af7a8a0bc30797 to your computer and use it in GitHub Desktop.
Conventions for setting up a GraphQL Server on Rails - CreateCommentMutator
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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