summaryrefslogtreecommitdiff
path: root/app/graphql/mutations/notes/update/image_diff_note.rb
blob: f4533cd9edbe14a9ae27dea4a6ca5407d11ff79e (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
# frozen_string_literal: true

module Mutations
  module Notes
    module Update
      class ImageDiffNote < Mutations::Notes::Update::Base
        graphql_name 'UpdateImageDiffNote'

        argument :body,
                  GraphQL::STRING_TYPE,
                  required: false,
                  description: copy_field_description(Types::Notes::NoteType, :body)

        argument :position,
                  Types::Notes::UpdateDiffImagePositionInputType,
                  required: false,
                  description: copy_field_description(Types::Notes::NoteType, :position)

        def ready?(**args)
          # As both arguments are optional, validate here that one of the
          # arguments are present.
          #
          # This may be able to be done using InputUnions in the future
          # if this RFC is merged:
          # https://github.com/graphql/graphql-spec/blob/master/rfcs/InputUnion.md
          if args.values_at(:body, :position).compact.blank?
            raise Gitlab::Graphql::Errors::ArgumentError,
                  'body or position arguments are required'
          end

          super(**args)
        end

        private

        def pre_update_checks!(note, _args)
          unless note.is_a?(DiffNote) && note.position.on_image?
            raise Gitlab::Graphql::Errors::ResourceNotAvailable,
                  'Resource is not an ImageDiffNote'
          end
        end

        def note_params(note, args)
          super(note, args).merge(
            position: position_params(note, args)
          ).compact
        end

        def position_params(note, args)
          return unless args[:position]

          original_position = note.position.to_h

          Gitlab::Diff::Position.new(original_position.merge(args[:position]))
        end
      end
    end
  end
end