summaryrefslogtreecommitdiff
path: root/app/graphql/resolvers/design_management/design_resolver.rb
blob: e0a68bae39744b4cf36c36a471e1f7c462d3f796 (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
# frozen_string_literal: true

module Resolvers
  module DesignManagement
    class DesignResolver < BaseResolver
      type ::Types::DesignManagement::DesignType, null: true

      argument :id, ::Types::GlobalIDType[::DesignManagement::Design],
               required: false,
               description: 'Find a design by its ID'

      argument :filename, GraphQL::STRING_TYPE,
               required: false,
               description: 'Find a design by its filename'

      def resolve(filename: nil, id: nil)
        params = parse_args(filename, id)

        build_finder(params).execute.first
      end

      def self.single
        self
      end

      private

      def issue
        object.issue
      end

      def build_finder(params)
        ::DesignManagement::DesignsFinder.new(issue, current_user, params)
      end

      def error(msg)
        raise ::Gitlab::Graphql::Errors::ArgumentError, msg
      end

      def parse_args(filename, id)
        provided = [filename, id].map(&:present?)

        if provided.none?
          error('one of id or filename must be passed')
        elsif provided.all?
          error('only one of id or filename may be passed')
        elsif filename.present?
          { filenames: [filename] }
        else
          { ids: [parse_gid(id)] }
        end
      end

      def parse_gid(gid)
        # TODO: remove this line when the compatibility layer is removed
        # See: https://gitlab.com/gitlab-org/gitlab/-/issues/257883
        gid = ::Types::GlobalIDType[::DesignManagement::Design].coerce_isolated_input(gid)

        gid.model_id
      end
    end
  end
end