summaryrefslogtreecommitdiff
path: root/app/graphql/mutations/design_management/move.rb
blob: fe280e926d2e5900140c86d4a05d5192e33c426f (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
# frozen_string_literal: true

module Mutations
  module DesignManagement
    class Move < ::Mutations::BaseMutation
      graphql_name "DesignManagementMove"

      DesignID = ::Types::GlobalIDType[::DesignManagement::Design]

      argument :id, DesignID, required: true, as: :current_design,
               description: "ID of the design to move."

      argument :previous, DesignID, required: false, as: :previous_design,
               description: "ID of the immediately preceding design."

      argument :next, DesignID, required: false, as: :next_design,
               description: "ID of the immediately following design."

      field :design_collection, Types::DesignManagement::DesignCollectionType,
            null: true,
            description: "The current state of the collection."

      def resolve(**args)
        service = ::DesignManagement::MoveDesignsService.new(current_user, parameters(**args))

        { design_collection: service.collection, errors: service.execute.errors }
      end

      private

      def parameters(**args)
        args.transform_values { |id| find_design(id) }.transform_values(&:sync).tap do |hash|
          hash.each { |k, design| not_found(args[k]) unless current_user.can?(:read_design, design) }
        end
      end

      def find_design(id)
        # TODO: remove this line when the compatibility layer is removed
        # See: https://gitlab.com/gitlab-org/gitlab/-/issues/257883
        id = DesignID.coerce_isolated_input(id)
        GitlabSchema.find_by_gid(id)
      end

      def not_found(gid)
        raise Gitlab::Graphql::Errors::ResourceNotAvailable, "Resource not available: #{gid}"
      end
    end
  end
end