gitlab-org--gitlab-foss/lib/gitlab/graphql/known_operations.rb

40 lines
975 B
Ruby

# frozen_string_literal: true
module Gitlab
module Graphql
class KnownOperations
Operation = Struct.new(:name) do
def to_caller_id
"graphql:#{name}"
end
end
ANONYMOUS = Operation.new("anonymous").freeze
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([ANONYMOUS, 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 ANONYMOUS unless operation_name
@operation_hash[operation_name] || UNKNOWN
end
def operations
@operation_hash.values
end
end
end
end