summaryrefslogtreecommitdiff
path: root/app/graphql/mutations/boards/lists/create.rb
blob: 9eb9a4d4b87bfadb6034dd83c93ebbf6518edff6 (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
52
53
54
55
56
57
58
59
60
61
62
63
64
# frozen_string_literal: true

module Mutations
  module Boards
    module Lists
      class Create < Base
        graphql_name 'BoardListCreate'

        argument :backlog, GraphQL::BOOLEAN_TYPE,
                 required: false,
                 description: 'Create the backlog list.'

        argument :label_id, ::Types::GlobalIDType[::Label],
                 required: false,
                 description: 'Global ID of an existing label.'

        def ready?(**args)
          if args.slice(*mutually_exclusive_args).size != 1
            arg_str = mutually_exclusive_args.map { |x| x.to_s.camelize(:lower) }.join(' or ')
            raise Gitlab::Graphql::Errors::ArgumentError, "one and only one of #{arg_str} is required"
          end

          super
        end

        def resolve(**args)
          board  = authorized_find!(id: args[:board_id])
          params = create_list_params(args)

          response = create_list(board, params)

          {
            list: response.success? ? response.payload[:list] : nil,
            errors: response.errors
          }
        end

        private

        def create_list(board, params)
          create_list_service =
            ::Boards::Lists::CreateService.new(board.resource_parent, current_user, params)

          create_list_service.execute(board)
        end

        # Overridden in EE
        def create_list_params(args)
          params = args.slice(*mutually_exclusive_args).with_indifferent_access
          params[:label_id] &&= ::GitlabSchema.parse_gid(params[:label_id], expected_type: ::Label).model_id

          params
        end

        # Overridden in EE
        def mutually_exclusive_args
          [:backlog, :label_id]
        end
      end
    end
  end
end

Mutations::Boards::Lists::Create.prepend_if_ee('::EE::Mutations::Boards::Lists::Create')