Skip to content

Commit

Permalink
@rmosolgo's comments
Browse files Browse the repository at this point in the history
  • Loading branch information
esjee committed Jul 7, 2017
1 parent c5c8f5a commit 5dd14f5
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 24 deletions.
54 changes: 30 additions & 24 deletions lib/graphql/static_validation/rules/variable_usages_are_allowed.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,22 @@ def validate(context)
node_values = Array.wrap(node.value)
return if !(node_values.all? { |value| value.is_a? GraphQL::Language::Nodes::VariableIdentifier })

node_values.each do |node_value|
arguments = nil

case parent
when GraphQL::Language::Nodes::Field
arguments = context.field_definition.arguments
when GraphQL::Language::Nodes::Directive
arguments = context.directive_definition.arguments
when GraphQL::Language::Nodes::InputObject
arg_type = context.argument_definition.type.unwrap
if arg_type.is_a?(GraphQL::InputObjectType)
arguments = arg_type.input_fields
end
else
raise("Unexpected argument parent: #{parent}")
arguments = nil
case parent
when GraphQL::Language::Nodes::Field
arguments = context.field_definition.arguments
when GraphQL::Language::Nodes::Directive
arguments = context.directive_definition.arguments
when GraphQL::Language::Nodes::InputObject
arg_type = context.argument_definition.type.unwrap
if arg_type.is_a?(GraphQL::InputObjectType)
arguments = arg_type.input_fields
end
else
raise("Unexpected argument parent: #{parent}")
end

node_values.each do |node_value|
var_defn_ast = declared_variables[node_value.name]
# Might be undefined :(
# VariablesAreUsedAndDefined can't finalize its search until the end of the document.
Expand Down Expand Up @@ -76,22 +76,28 @@ def wrap_var_type_with_depth_of_arg(var_type, arg_node)
return var_type unless arg_node_value.is_a?(Array)
new_var_type = var_type

array_depth = 1 + depth_of_array(arg_node_value)

array_depth.times do
depth_of_array(arg_node_value).times do
new_var_type = GraphQL::ListType.new(of_type: new_var_type)
end

new_var_type
end

def depth_of_array(some_array)
result = 0
while (flattened = some_array.flatten) != some_array
result += 1
some_array = flattened
# @return [Integer] Returns the max depth of `array`, or `0` if it isn't an array at all
def depth_of_array(array)
case array
when Array
max_child_depth = 0
array.each do |item|
item_depth = depth_of_array(item)
if item_depth > max_child_depth
max_child_depth = item_depth
end
end
1 + max_child_depth
else
0
end
result
end

def list_dimension(type)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,5 +142,34 @@
assert_equal 0, errors.size
end
end

describe 'argument contains a list with literal values' do
let(:query_string) {
<<-GRAPHQL
query {
imageUrl(sizes: [{height: 100, width: 100, scale: 1}])
}
GRAPHQL
}

focus
it "is a valid query" do
assert_equal 0, errors.size
end
end

describe 'argument contains a list with both literal and variable values' do
let(:query_string) {
<<-GRAPHQL
query($size1: ImageSize, $size2: ImageSize) {
imageUrl(sizes: [{height: 100, width: 100, scale: 1}, $size1, {height: 1920, width: 1080, scale: 2}, $size2])
}
GRAPHQL
}

it "is a valid query" do
assert_equal 0, errors.size
end
end
end
end

0 comments on commit 5dd14f5

Please sign in to comment.