Skip to content

Commit 36db37d

Browse files
committed
feat(Lazy) if a GraphQL::ExecutionError is raised during lazy resolution, rescue it
1 parent 2f81c30 commit 36db37d

File tree

2 files changed

+42
-3
lines changed

2 files changed

+42
-3
lines changed

lib/graphql/execution/lazy.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ def value
3131
@value = @get_value_func.call
3232
end
3333
@value
34+
rescue GraphQL::ExecutionError => err
35+
@resolved = true
36+
@value = err
3437
end
3538

3639
# @return [Lazy] A {Lazy} whose value depends on another {Lazy}, plus any transformations in `block`

spec/graphql/execution/lazy_spec.rb

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,19 @@
22

33
describe GraphQL::Execution::Lazy do
44
class Wrapper
5-
def initialize(item)
6-
@item = item
5+
def initialize(item = nil, &block)
6+
if block
7+
@block = block
8+
else
9+
@item = item
10+
end
711
end
812

913
def item
14+
if @block
15+
@item = @block.call()
16+
@block = nil
17+
end
1018
@item
1119
end
1220
end
@@ -75,7 +83,13 @@ def value
7583

7684
field :nullableNestedSum, LazySum do
7785
argument :value, types.Int
78-
resolve ->(o, args, c) { SumAll.new(c, args[:value]) }
86+
resolve ->(o, args, c) {
87+
if args[:value] == 13
88+
Wrapper.new { raise GraphQL::ExecutionError.new("13 is unlucky") }
89+
else
90+
SumAll.new(c, args[:value])
91+
end
92+
}
7993
end
8094

8195
field :listSum, types[LazySum] do
@@ -169,7 +183,29 @@ def run_query(query_str)
169183
}
170184
assert_equal(expected_data, res["data"])
171185
assert_equal 1, res["errors"].length
186+
end
187+
188+
it "handles raised errors" do
189+
res = run_query %|
190+
{
191+
a: nullableNestedSum(value: 1) { value }
192+
b: nullableNestedSum(value: 13) { value }
193+
c: nullableNestedSum(value: 2) { value }
194+
}|
195+
196+
expected_data = {
197+
"a" => { "value" => 3 },
198+
"b" => nil,
199+
"c" => { "value" => 3 },
200+
}
201+
assert_equal expected_data, res["data"]
172202

203+
expected_errors = [{
204+
"message"=>"13 is unlucky",
205+
"locations"=>[{"line"=>4, "column"=>9}],
206+
"path"=>["b"],
207+
}]
208+
assert_equal expected_errors, res["errors"]
173209
end
174210

175211
it "resolves mutation fields right away" do

0 commit comments

Comments
 (0)