summaryrefslogtreecommitdiff
path: root/app/graphql/mutations/custom_emoji/create.rb
blob: ad392d6c81490602ab6c8022bb8b93cef053a417 (plain)
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
43
44
45
46
47
48
49
50
51
# frozen_string_literal: true

module Mutations
  module CustomEmoji
    class Create < BaseMutation
      include Mutations::ResolvesGroup

      graphql_name 'CreateCustomEmoji'

      authorize :create_custom_emoji

      field :custom_emoji,
            Types::CustomEmojiType,
            null: true,
            description: 'New custom emoji.'

      argument :group_path, GraphQL::Types::ID,
               required: true,
               description: 'Namespace full path the emoji is associated with.'

      argument :name, GraphQL::Types::String,
               required: true,
               description: 'Name of the emoji.'

      argument :url, GraphQL::Types::String,
               required: true,
               as: :file,
               description: 'Location of the emoji file.'

      def resolve(group_path:, **args)
        group = authorized_find!(group_path: group_path)
        # See https://gitlab.com/gitlab-org/gitlab/-/merge_requests/37911#note_444682238
        args[:external] = true
        args[:creator] = current_user

        custom_emoji = group.custom_emoji.create(args)

        {
          custom_emoji: custom_emoji.valid? ? custom_emoji : nil,
          errors: errors_on_object(custom_emoji)
        }
      end

      private

      def find_object(group_path:)
        resolve_group(full_path: group_path)
      end
    end
  end
end