Skip to content

Commit 16b4035

Browse files
committed
Also handle arrays with skips in them
1 parent 21b2099 commit 16b4035

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed

lib/graphql/execution/interpreter/runtime.rb

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,25 @@ def []=(key, value)
4545

4646
class GraphQLResultArray < Array
4747
include GraphQLResult
48+
49+
def skip_at(index)
50+
# Mark this index as dead. It's tricky because some indices may already be storing
51+
# `Lazy`s. So the runtime is still holding indexes _before_ skipping,
52+
# this object has to coordinate incoming writes to account for any already-skipped indices.
53+
@skip_indices ||= []
54+
@skip_indices << index
55+
offset_by = @skip_indices.count { |skipped_idx| skipped_idx < index}
56+
delete_at_index = index - offset_by
57+
delete_at(delete_at_index)
58+
end
59+
60+
def []=(idx, value)
61+
if @skip_indices
62+
offset_by = @skip_indices.count { |skipped_idx| skipped_idx < idx }
63+
idx -= offset_by
64+
end
65+
super(idx, value)
66+
end
4867
end
4968

5069
class GraphQLSelectionSet < Hash
@@ -522,7 +541,7 @@ def continue_value(path, value, parent_type, field, is_non_null, ast_node, resul
522541
when Hash
523542
selection_result.delete(result_name)
524543
when Array
525-
selection_result.delete_at(result_name)
544+
selection_result.skip_at(result_name)
526545
else
527546
raise "Invariant: unexpected result class #{selection_result.class} (#{selection_result.inspect})"
528547
end

spec/graphql/execution/interpreter_spec.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -564,6 +564,17 @@ def skip
564564
def lazy_skip
565565
-> { context.skip }
566566
end
567+
568+
field :mixed_skips, [String], null: true
569+
def mixed_skips
570+
[
571+
"a",
572+
context.skip,
573+
"c",
574+
-> { context.skip },
575+
"e",
576+
]
577+
end
567578
end
568579

569580
class NothingSubscription < GraphQL::Schema::Subscription
@@ -592,6 +603,10 @@ class Subscription < GraphQL::Schema::Object
592603
assert_equal({}, res["data"])
593604
refute res.key?("errors")
594605

606+
res = LazySkipSchema.execute("{ mixedSkips }")
607+
assert_equal({ "mixedSkips" => ["a", "c", "e"] }, res["data"])
608+
refute res.key?("errors")
609+
595610
res = LazySkipSchema.execute("{ lazySkip }")
596611
assert_equal({}, res["data"])
597612
refute res.key?("errors")

0 commit comments

Comments
 (0)