Welcome to mirror list, hosted at ThFree Co, Russian Federation.

find_argument_in_parent.rb « graphql « gitlab « lib - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 1f83f8fce7ab9ba3f8b6299d4ab52f18e642181a (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
# frozen_string_literal: true

module Gitlab
  module Graphql
    module FindArgumentInParent
      # Searches up the GraphQL AST and returns the first matching argument
      # passed to a node
      def self.find(parent, argument, limit_depth: nil)
        argument = argument.to_s.camelize(:lower).to_sym
        depth = 0

        while parent.respond_to?(:parent)
          args = node_args(parent)
          return args[argument] if args.key?(argument)

          depth += 1
          return if limit_depth && depth >= limit_depth

          parent = parent.parent
        end
      end

      class << self
        private

        def node_args(node)
          node.irep_node.arguments
        end
      end
    end
  end
end