Module: Decidim::Comments::MutationExtensions

Defined in:
lib/decidim/comments/mutation_extensions.rb

Overview

This module’s job is to extend the API with custom fields related to decidim-comments.

Class Method Summary collapse

Class Method Details

.extend!(type) ⇒ Object

Public: Extends a type with ‘decidim-comments`’s fields.

type - A GraphQL::BaseType to extend.

Returns nothing.



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
43
# File 'lib/decidim/comments/mutation_extensions.rb', line 12

def self.extend!(type)
  type.define do
    field :addComment, Decidim::Comments::CommentType do
      description "Add a new comment to a commentable"
      argument :commentableId, !types.String, "The commentable's ID"
      argument :commentableType, !types.String, "The commentable's class name. i.e. `Decidim::ParticipatoryProcess`"
      argument :body, !types.String, "The comments's body"
      argument :alignment, types.Int, "The comment's alignment. Can be 0 (neutral), 1 (in favor) or -1 (against)'", default_value: 0
      argument :userGroupId, types.ID, "The comment's user group id. Replaces the author."

      resolve lambda { |_obj, args, ctx|
        params = { "comment" => { "body" => args[:body], "alignment" => args[:alignment], "user_group_id" => args[:userGroupId] } }
        form = Decidim::Comments::CommentForm.from_params(params)
        commentable = args[:commentableType].constantize.find(args[:commentableId])
        Decidim::Comments::CreateComment.call(form, ctx[:current_user], commentable) do
          on(:ok) do |comment|
            return comment
          end
        end
      }
    end

    field :comment, Decidim::Comments::CommentMutationType do
      description "A comment"
      argument :id, !types.ID, "The comment's id"

      resolve lambda { |_obj, args, _ctx|
        Comment.find(args["id"])
      }
    end
  end
end