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

module Mutations
  module ReleaseAssetLinks
    class Update < BaseMutation
      graphql_name 'ReleaseAssetLinkUpdate'

      authorize :update_release

      ReleaseAssetLinkID = ::Types::GlobalIDType[::Releases::Link]

      argument :id, ReleaseAssetLinkID,
               required: true,
               description: 'ID of the release asset link to update.'

      argument :name, GraphQL::Types::String,
               required: false,
               description: 'Name of the asset link.'

      argument :url, GraphQL::Types::String,
               required: false,
               description: 'URL of the asset link.'

      argument :direct_asset_path, GraphQL::Types::String,
               required: false, as: :filepath,
               description: 'Relative path for a direct asset link.'

      argument :link_type, Types::ReleaseAssetLinkTypeEnum,
               required: false,
               description: 'Type of the asset link.'

      field :link,
            Types::ReleaseAssetLinkType,
            null: true,
            description: 'Asset link after mutation.'

      def ready?(**args)
        if args.key?(:link_type) && args[:link_type].nil?
          raise Gitlab::Graphql::Errors::ArgumentError,
                'if the linkType argument is provided, it cannot be null'
        end

        super
      end

      def resolve(id:, **link_attrs)
        link = authorized_find!(id)

        unless link.update(link_attrs)
          return { link: nil, errors: link.errors.full_messages }
        end

        { link: link, errors: [] }
      end

      def find_object(id)
        GitlabSchema.find_by_gid(id)
      end
    end
  end
end