Skip to content

Commit d7565e4

Browse files
committed
Remove instrument documentation, migrate subscriptions and LazyHelpers
1 parent 6a55247 commit d7565e4

File tree

8 files changed

+51
-92
lines changed

8 files changed

+51
-92
lines changed

guides/queries/instrumentation.md

Lines changed: 0 additions & 33 deletions
This file was deleted.

guides/queries/tracing.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ search: true
66
section: Queries
77
desc: Observation hooks for execution
88
index: 11
9+
redirect_from:
10+
- /queries/instrumentation
911
---
1012

1113
{{ "GraphQL::Tracing::Trace" | api_doc }} provides hooks to observe and modify events during runtime. Tracing hooks are methods, defined in modules and mixed in with {{ "Schema.trace_with" | api_doc }}.

guides/schema/definition.md

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -101,19 +101,11 @@ See the {% internal_link "Object Identification guide", "/schema/object_identifi
101101

102102
## Execution Configuration
103103

104-
__`instrument`__ attaches instrumenters to the schema, see {% internal_link "Instrumentation", "/queries/instrumentation" %} for more information.
104+
__`trace_with`__ attaches tracer modules, see {% internal_link "Tracing", "/queries/tracing" %} for more.
105105

106106
```ruby
107107
class MySchema < GraphQL::Schema
108-
instrument :query, ResolveTimerInstrumentation
109-
end
110-
```
111-
112-
__`tracer`__ is another way to hook into execution, see {% internal_link "Tracing", "/queries/tracing" %} for more.
113-
114-
```ruby
115-
class MySchema < GraphQL::Schema
116-
tracer MetricTracer
108+
trace_with MetricTracer
117109
end
118110
```
119111

lib/graphql/execution/interpreter.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,9 @@ def run_all(schema, query_options, context: {}, max_complexity: schema.max_compl
5151
# Do as much eager evaluation of the query as possible
5252
results = []
5353
queries.each_with_index do |query, idx|
54+
if query.subscription? && !query.subscription_update?
55+
query.context.namespace(:subscriptions)[:events] = []
56+
end
5457
multiplex.dataloader.append_job {
5558
operation = query.selected_operation
5659
result = if operation.nil? || !query.valid? || query.context.errors.any?
@@ -96,6 +99,9 @@ def run_all(schema, query_options, context: {}, max_complexity: schema.max_compl
9699
# Then, find all errors and assign the result to the query object
97100
results.each_with_index do |data_result, idx|
98101
query = queries[idx]
102+
if (events = query.context.namespace(:subscriptions)[:events]) && events.any?
103+
schema.subscriptions.write_subscription(query, events)
104+
end
99105
# Assign the result so that it can be accessed in instrumentation
100106
query.result_values = if data_result.equal?(NO_OPERATION)
101107
if !query.valid? || query.context.errors.any?

lib/graphql/schema.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1044,6 +1044,11 @@ def lazy_resolve(lazy_class, value_method)
10441044
end
10451045

10461046
def instrument(instrument_step, instrumenter, options = {})
1047+
warn <<~WARN
1048+
Schema.instrument is deprecated, use `trace_with` instead: https://graphql-ruby.org/queries/tracing.html"
1049+
(From `#{self}.instrument(#{instrument_step}, #{instrumenter})` at #{caller(1, 1).first})
1050+
1051+
WARN
10471052
trace_with(Tracing::LegacyHooksTrace)
10481053
own_instrumenters[instrument_step] << instrumenter
10491054
end

lib/graphql/subscriptions.rb

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
require "securerandom"
33
require "graphql/subscriptions/broadcast_analyzer"
44
require "graphql/subscriptions/event"
5-
require "graphql/subscriptions/instrumentation"
65
require "graphql/subscriptions/serialize"
76
require "graphql/subscriptions/action_cable_subscriptions"
87
require "graphql/subscriptions/default_subscription_resolve_extension"
@@ -30,8 +29,6 @@ def self.use(defn, options = {})
3029
raise ArgumentError, "Can't reinstall subscriptions. #{schema} is using #{schema.subscriptions}, can't also add #{self}"
3130
end
3231

33-
instrumentation = Subscriptions::Instrumentation.new(schema: schema)
34-
defn.instrument(:query, instrumentation)
3532
options[:schema] = schema
3633
schema.subscriptions = self.new(**options)
3734
schema.add_subscription_extension_if_necessary

lib/graphql/subscriptions/instrumentation.rb

Lines changed: 0 additions & 28 deletions
This file was deleted.

spec/support/lazy_helpers.rb

Lines changed: 36 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -124,46 +124,64 @@ def list_sum(values:)
124124
end
125125
end
126126

127-
class SumAllInstrumentation
128-
def initialize(counter:)
129-
@counter = counter
127+
module SumAllInstrumentation
128+
def execute_query(query:)
129+
add_check(query, "before #{query.selected_operation.name}")
130+
super
130131
end
131132

132-
def before_query(q)
133-
add_check(q, "before #{q.selected_operation.name}")
133+
def execute_query_lazy(query:, multiplex:)
134+
result = super
135+
multiplex.queries.reverse_each do |q|
136+
add_check(q, "after #{q.selected_operation.name}")
137+
end
138+
result
139+
end
140+
141+
def execute_multiplex(multiplex:)
142+
add_check(multiplex, "before multiplex 1")
134143
# TODO not threadsafe
135144
# This should use multiplex-level context
136145
SumAll.all.clear
146+
result = super
147+
add_check(multiplex, "after multiplex 1")
148+
result
137149
end
138150

139-
def after_query(q)
140-
add_check(q, "after #{q.selected_operation.name}")
141-
end
151+
private
142152

143-
def before_multiplex(multiplex)
144-
add_check(multiplex, "before multiplex #@counter")
153+
def add_check(object, text)
154+
checks = object.context[:instrumentation_checks]
155+
if checks
156+
checks << text
157+
end
145158
end
159+
end
146160

147-
def after_multiplex(multiplex)
148-
add_check(multiplex, "after multiplex #@counter")
161+
module SumAllInstrumentation2
162+
def execute_multiplex(multiplex:)
163+
add_check(multiplex, "before multiplex 2")
164+
result = super
165+
add_check(multiplex, "after multiplex 2")
166+
result
149167
end
150168

151-
def add_check(obj, text)
152-
checks = obj.context[:instrumentation_checks]
169+
private
170+
171+
def add_check(object, text)
172+
checks = object.context[:instrumentation_checks]
153173
if checks
154174
checks << text
155175
end
156176
end
157177
end
158-
159178
class LazySchema < GraphQL::Schema
160179
query(LazyQuery)
161180
mutation(LazyQuery)
162181
lazy_resolve(Wrapper, :item)
163182
lazy_resolve(SumAll, :value)
164-
instrument(:query, SumAllInstrumentation.new(counter: nil))
165-
instrument(:multiplex, SumAllInstrumentation.new(counter: 1))
166-
instrument(:multiplex, SumAllInstrumentation.new(counter: 2))
183+
trace_with(SumAllInstrumentation2)
184+
trace_with(SumAllInstrumentation)
167185

168186
def self.sync_lazy(lazy)
169187
if lazy.is_a?(SumAll) && lazy.own_value > 1000

0 commit comments

Comments
 (0)