Skip to content

Commit 4fb8864

Browse files
author
Robert Mosolgo
authored
Merge pull request rmosolgo#3499 from rmosolgo/directives-on-fragments
Runtime directives on fragments
2 parents a6487a0 + 124bfc0 commit 4fb8864

File tree

9 files changed

+407
-66
lines changed

9 files changed

+407
-66
lines changed

benchmark/run.rb

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
require "graphql"
44
require "jazz"
55
require "benchmark/ips"
6-
require "ruby-prof"
6+
require "stackprof"
77
require "memory_profiler"
88
require "graphql/batch"
99

@@ -46,15 +46,11 @@ def self.profile
4646
SCHEMA.execute(document: DOCUMENT)
4747
# CARD_SCHEMA.validate(ABSTRACT_FRAGMENTS)
4848
res = nil
49-
result = RubyProf.profile do
49+
result = StackProf.run(mode: :wall) do
5050
# CARD_SCHEMA.validate(ABSTRACT_FRAGMENTS)
5151
res = SCHEMA.execute(document: DOCUMENT)
5252
end
53-
# printer = RubyProf::FlatPrinter.new(result)
54-
# printer = RubyProf::GraphHtmlPrinter.new(result)
55-
printer = RubyProf::FlatPrinterWithLineNumbers.new(result)
56-
57-
printer.print(STDOUT, {})
53+
StackProf::Report.new(result).print_text
5854
end
5955

6056
# Adapted from https://github.com/rmosolgo/graphql-ruby/issues/861
@@ -67,13 +63,10 @@ def self.profile_large_result
6763
}
6864
end
6965

70-
result = RubyProf.profile do
66+
result = StackProf.run(mode: :wall) do
7167
schema.execute(document: document)
7268
end
73-
printer = RubyProf::FlatPrinter.new(result)
74-
# printer = RubyProf::GraphHtmlPrinter.new(result)
75-
# printer = RubyProf::FlatPrinterWithLineNumbers.new(result)
76-
printer.print(STDOUT, {})
69+
StackProf::Report.new(result).print_text
7770

7871
report = MemoryProfiler.report do
7972
schema.execute(document: document)

graphql.gemspec

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ Gem::Specification.new do |s|
3636
s.add_development_dependency "racc", "~> 1.4"
3737
s.add_development_dependency "rake", "~> 12"
3838
s.add_development_dependency "rubocop", "0.68" # for Ruby 2.2 enforcement
39+
s.add_development_dependency "stackprof"
3940
# required for upgrader
4041
s.add_development_dependency "parser"
4142
# website stuff

guides/type_definitions/directives.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ query {
7777
Directive classes may implement the following class methods to interact with the runtime:
7878

7979
- `def self.include?(obj, args, ctx)`: If this hook returns `false`, the nodes flagged by this directive will be skipped at runtime.
80-
- `def self.resolve(obj, args, ctx)`: Wraps the resolution of flagged nodes. Resolution is passed as a __block__, so `yield` will continue resolution. The return value of this method will be treated as the return value of the flagged field.
80+
- `def self.resolve(obj, args, ctx)`: Wraps the resolution of flagged nodes. Resolution is passed as a __block__, so `yield` will continue resolution.
8181

8282
Looking for a runtime hook that isn't listed here? {% open_an_issue "New directive hook: @something", "<!-- Describe how the directive would be used and then how you might implement it --> " %} to start the conversation!
8383

lib/graphql/dataloader.rb

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

80+
# Use a self-contained queue for the work in the block.
81+
def run_isolated
82+
prev_queue = @pending_jobs
83+
@pending_jobs = []
84+
res = nil
85+
# Make sure the block is inside a Fiber, so it can `Fiber.yield`
86+
append_job {
87+
res = yield
88+
}
89+
run
90+
res
91+
ensure
92+
@pending_jobs = prev_queue
93+
end
94+
8095
# @api private Move along, move along
8196
def run
8297
# At a high level, the algorithm is:

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_isolated; 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_isolated 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,

0 commit comments

Comments
 (0)