From dae02286ba942d3783ff13c4f74f34f72d20b68d Mon Sep 17 00:00:00 2001 From: Luke Duncalfe Date: Fri, 28 Jun 2019 14:28:43 +1200 Subject: CE backport for changes in EE MR 14292 EE MR https://gitlab.com/gitlab-org/gitlab-ee/merge_requests/14292 https://gitlab.com/gitlab-org/gitlab-ee/issues/9491 --- lib/gitlab/graphql/find_argument_in_parent.rb | 32 ++++++++++++++++ .../gitlab/graphql/find_argument_in_parent_spec.rb | 44 ++++++++++++++++++++++ 2 files changed, 76 insertions(+) create mode 100644 lib/gitlab/graphql/find_argument_in_parent.rb create mode 100644 spec/lib/gitlab/graphql/find_argument_in_parent_spec.rb diff --git a/lib/gitlab/graphql/find_argument_in_parent.rb b/lib/gitlab/graphql/find_argument_in_parent.rb new file mode 100644 index 00000000000..1f83f8fce7a --- /dev/null +++ b/lib/gitlab/graphql/find_argument_in_parent.rb @@ -0,0 +1,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 diff --git a/spec/lib/gitlab/graphql/find_argument_in_parent_spec.rb b/spec/lib/gitlab/graphql/find_argument_in_parent_spec.rb new file mode 100644 index 00000000000..91e90315b3e --- /dev/null +++ b/spec/lib/gitlab/graphql/find_argument_in_parent_spec.rb @@ -0,0 +1,44 @@ +# frozen_string_literal: true + +require 'spec_helper' + +describe Gitlab::Graphql::FindArgumentInParent do + describe '#find' do + def build_node(parent = nil, args: {}) + props = { irep_node: double(arguments: args) } + props[:parent] = parent if parent # The root node shouldn't respond to parent + + double(props) + end + + let(:parent) do + build_node( + build_node( + build_node( + build_node, + args: { myArg: 1 } + ) + ) + ) + end + let(:arg_name) { :my_arg } + + it 'searches parents and returns the argument' do + expect(described_class.find(parent, :my_arg)).to eq(1) + end + + it 'can find argument when passed in as both Ruby and GraphQL-formatted symbols and strings' do + [:my_arg, :myArg, 'my_arg', 'myArg'].each do |arg| + expect(described_class.find(parent, arg)).to eq(1) + end + end + + it 'returns nil if no arguments found in parents' do + expect(described_class.find(parent, :bar)).to eq(nil) + end + + it 'can limit the depth it searches to' do + expect(described_class.find(parent, :my_arg, limit_depth: 1)).to eq(nil) + end + end +end -- cgit v1.2.1