summaryrefslogtreecommitdiff
path: root/lib/gitlab/graphql/known_operations.rb
blob: a551c9bb6dad57a5af37321a09bf28bd50256f65 (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
# frozen_string_literal: true

module Gitlab
  module Graphql
    class KnownOperations
      Operation = Struct.new(:name) do
        def to_caller_id
          "graphql:#{name}"
        end

        def query_urgency
          # We'll be able to actually correlate query_urgency with https://gitlab.com/gitlab-org/gitlab/-/issues/345141
          ::Gitlab::EndpointAttributes::DEFAULT_URGENCY
        end
      end

      UNKNOWN = Operation.new("unknown").freeze

      def self.default
        @default ||= self.new(Gitlab::Webpack::GraphqlKnownOperations.load)
      end

      def initialize(operation_names)
        @operation_hash = operation_names
          .map { |name| Operation.new(name).freeze }
          .concat([UNKNOWN])
          .index_by(&:name)
      end

      # Returns the known operation from the given ::GraphQL::Query object
      def from_query(query)
        operation_name = query.selected_operation_name

        return UNKNOWN unless operation_name

        @operation_hash[operation_name] || UNKNOWN
      end

      def operations
        @operation_hash.values
      end
    end
  end
end