-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadd_comment.rb
42 lines (32 loc) Β· 1.11 KB
/
add_comment.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
class Mutations::AddComment < Mutations::BaseMutation
description "Add Comment to post"
argument :postId, ID, required: true
argument :content, String, required: true
field :comment, Types::CommentType, null: true
field :success, Boolean, null: false
field :errors, [String], null: false
def resolve(args)
unless context[:current_user]
raise Exception, "Sign in to do this action"
end
post = Post.find(args[:post_id])
comment = Comment.new(
content: args[:content],
post: post,
user: context[:current_user]
)
if comment.save
return {
comment: comment,
success: true,
errors: comment.errors.full_messages
}
else
raise ActiveRecord::RecordInvalid, comment
end
rescue ActiveRecord::RecordInvalid => invalid
return { errors: invalid.record.errors.full_messages, success: false }
rescue Exception => e
{ errors: e.message.split(",") , success: false}
end
end