Skip to content

Commit a9cb3c6

Browse files
committed
Hack something that works
1 parent bb1c8d9 commit a9cb3c6

File tree

5 files changed

+26
-17
lines changed

5 files changed

+26
-17
lines changed

lib/graphql/dataloader.rb

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,17 @@ def append_job(&job)
7777
nil
7878
end
7979

80+
# Use a self-contained queue for the work in the block.
81+
def run_contained
82+
prev_queue = @pending_jobs
83+
@pending_jobs = []
84+
res = yield
85+
run
86+
res
87+
ensure
88+
@pending_jobs = prev_queue
89+
end
90+
8091
# @api private Move along, move along
8192
def run
8293
# At a high level, the algorithm is:
@@ -224,16 +235,16 @@ def resume(fiber)
224235
#
225236
# @see https://github.com/rmosolgo/graphql-ruby/issues/3449
226237
def spawn_fiber
227-
fiber_locals = {}
238+
fiber_locals = {}
228239

229240
Thread.current.keys.each do |fiber_var_key|
230241
fiber_locals[fiber_var_key] = Thread.current[fiber_var_key]
231-
end
242+
end
232243

233-
Fiber.new do
244+
Fiber.new do
234245
fiber_locals.each { |k, v| Thread.current[k] = v }
235246
yield
236-
end
247+
end
237248
end
238249
end
239250
end

lib/graphql/dataloader/null_dataloader.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ class NullDataloader < Dataloader
1010
# These are all no-ops because code was
1111
# executed sychronously.
1212
def run; end
13+
def run_contained; yield; end
1314
def yield; end
1415

1516
def append_job

lib/graphql/execution/interpreter/arguments_cache.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,12 @@ def initialize(query)
2828
end
2929

3030
def fetch(ast_node, argument_owner, parent_object)
31-
@storage[ast_node][argument_owner][parent_object]
3231
# If any jobs were enqueued, run them now,
3332
# since this might have been called outside of execution.
3433
# (The jobs are responsible for updating `result` in-place.)
35-
@dataloader.run
34+
@dataloader.run_contained do
35+
@storage[ast_node][argument_owner][parent_object]
36+
end
3637
# Ack, the _hash_ is updated, but the key is eventually
3738
# overridden with an immutable arguments instance.
3839
# The first call queues up the job,

lib/graphql/execution/interpreter/runtime.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -629,7 +629,7 @@ def run_directive(object, ast_node, idx, &block)
629629
if !dir_defn.is_a?(Class)
630630
dir_defn = dir_defn.type_class || raise("Only class-based directives are supported (not `@#{dir_node.name}`)")
631631
end
632-
dir_args = arguments(nil, dir_defn, dir_node).keyword_arguments
632+
dir_args = arguments(nil, dir_defn, dir_node)
633633
dir_defn.resolve(object, dir_args, context) do
634634
run_directive(object, ast_node, idx + 1, &block)
635635
end
@@ -640,7 +640,7 @@ def run_directive(object, ast_node, idx, &block)
640640
def directives_include?(node, graphql_object, parent_type)
641641
node.directives.each do |dir_node|
642642
dir_defn = schema.directives.fetch(dir_node.name).type_class || raise("Only class-based directives are supported (not #{dir_node.name.inspect})")
643-
args = arguments(graphql_object, dir_defn, dir_node).keyword_arguments
643+
args = arguments(graphql_object, dir_defn, dir_node)
644644
if !dir_defn.include?(graphql_object, args, context)
645645
return false
646646
end

spec/graphql/schema/directive_spec.rb

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -87,17 +87,14 @@ class CountFields < GraphQL::Schema::Directive
8787

8888
def self.resolve(obj, args, ctx)
8989
path = ctx[:current_path]
90-
p [:fetched_path, path]
91-
result = yield
92-
93-
p [:resolve_all, path]
94-
GraphQL::Execution::Interpreter::Resolve.resolve_all([result], ctx.dataloader)
95-
p [:run_dataloader, path]
96-
ctx.dataloader.run
90+
result = nil
91+
ctx.dataloader.run_contained do
92+
result = yield
93+
GraphQL::Execution::Interpreter::Resolve.resolve_all([result], ctx.dataloader)
94+
end
9795

9896
ctx[:count_fields] ||= Hash.new { |h, k| h[k] = [] }
9997
field_count = result.is_a?(Hash) ? result.size : 1
100-
p [path, field_count, result]
10198
ctx[:count_fields][path] << field_count
10299
nil # this does nothing
103100
end
@@ -150,7 +147,6 @@ class Schema < GraphQL::Schema
150147
end
151148

152149
describe "runtime directives" do
153-
focus
154150
it "works with fragment spreads, inline fragments, and fields" do
155151
query_str = <<-GRAPHQL
156152
{

0 commit comments

Comments
 (0)