Sha256: dab9efa0a8efe77d6346f0ea92ef7abcfdd68b16d6bbfd9986c036d8d11df91a

Contents?: true

Size: 1.87 KB

Versions: 3

Compression:

Stored size: 1.87 KB

Contents

# frozen_string_literal: true
module Decidim
  module Comments
    # This module's job is to extend the API with custom fields related to
    # decidim-comments.
    module MutationExtensions
      # Public: Extends a type with `decidim-comments`'s fields.
      #
      # type - A GraphQL::BaseType to extend.
      #
      # Returns nothing.
      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
    end
  end
end

Version data entries

3 entries across 3 versions & 2 rubygems

Version Path
decidim-0.0.4 decidim-comments/lib/decidim/comments/mutation_extensions.rb
decidim-comments-0.0.3 lib/decidim/comments/mutation_extensions.rb
decidim-0.0.3 decidim-comments/lib/decidim/comments/mutation_extensions.rb