Skip to content

Commit 2ef2e5f

Browse files
committed
Update timeout and variables tests
1 parent 57c9408 commit 2ef2e5f

5 files changed

Lines changed: 49 additions & 22 deletions

File tree

guides/execution/migration.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,8 @@ Fully supported, but some legacy hooks are _not_ called. Implement the new hooks
186186
- `execute_query`, `execute_query_lazy`: use `execute_multiplex` for a top-level hook instead. (Single queries are always executed in a multiplex of size = 1.)
187187
- `resolve_type`, `authorized`: use `{begin,end}_resolve_type` and `{begin,end}_authorized` instead. (May be called multiple times for Dataloader etc.)
188188

189+
Additionally, `object` parameters to those methods will receive an _Array_ of `objects` instead.
190+
189191
### Lazy resolution (GraphQL-Batch)
190192

191193
Lazy resolution runs in the new execution (GraphQL-Batch is supported). When migrating to class methods, you may need to update your library method calls to work on a set of inputs rather than a single input.

lib/graphql/execution/input_values.rb

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -82,20 +82,37 @@ def variable_value(value, type)
8282
elsif type.kind.input_object?
8383
coerced_obj = {}
8484

85-
@query.types.arguments(type).each do |arg|
86-
arg_key = arg.keyword
87-
if value.key?(arg.graphql_name)
88-
arg_value = value[arg.graphql_name]
89-
elsif value.key?(sym_name = arg.graphql_name.to_sym)
90-
arg_value = value[sym_name]
91-
elsif arg.default_value?
92-
coerced_obj[arg_key] = arg.default_value
93-
next
94-
else
95-
next
85+
if value.is_a?(Hash)
86+
@query.types.arguments(type).each do |arg|
87+
arg_key = arg.keyword
88+
if value.key?(arg.graphql_name)
89+
arg_value = value[arg.graphql_name]
90+
elsif value.key?(sym_name = arg.graphql_name.to_sym)
91+
arg_value = value[sym_name]
92+
elsif arg.default_value?
93+
coerced_obj[arg_key] = arg.default_value
94+
next
95+
else
96+
next
97+
end
98+
99+
coerced_obj[arg_key] = variable_value(arg_value, arg.type)
96100
end
101+
else
102+
@query.types.arguments(type).each do |arg|
103+
arg_key = arg.keyword
104+
arg_name = arg.graphql_name
105+
if (v_node = value.arguments.find { |a| a.name == arg_name })
106+
arg_value = v_node.value
107+
elsif arg.default_value?
108+
coerced_obj[arg_key] = arg.default_value
109+
next
110+
else
111+
next
112+
end
97113

98-
coerced_obj[arg_key] = variable_value(arg_value, arg.type)
114+
coerced_obj[arg_key] = variable_value(arg_value, arg.type)
115+
end
99116
end
100117

101118
coerced_obj

lib/graphql/schema/timeout.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ def execute_multiplex(multiplex:)
6868
super
6969
end
7070

71-
def execute_field(query:, field:, **_rest)
71+
def begin_execute_field(field, _arguments, _objects, query)
7272
timeout_state = query.context.namespace(@timeout).fetch(:state)
7373
# If the `:state` is `false`, then `max_seconds(query)` opted out of timeout for this query.
7474
if timeout_state == false
@@ -84,7 +84,7 @@ def execute_field(query:, field:, **_rest)
8484

8585
# `handle_timeout` may have set this to be `false`
8686
if timeout_state != false
87-
error
87+
raise error
8888
else
8989
super
9090
end

spec/graphql/schema/timeout_spec.rb

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,13 @@ def handle_timeout(error, query)
2424
nested_sleep_type = Class.new(GraphQL::Schema::Object) do
2525
graphql_name "NestedSleep"
2626

27-
field :seconds, Float
27+
field :seconds, Float, resolve_legacy_instance_method: true
2828

2929
def seconds
3030
object
3131
end
3232

33-
field :nested_sleep, self do
33+
field :nested_sleep, self, resolve_legacy_instance_method: true do
3434
argument :seconds, Float
3535
end
3636

@@ -43,7 +43,7 @@ def nested_sleep(seconds:)
4343
query_type = Class.new(GraphQL::Schema::Object) do
4444
graphql_name "Query"
4545

46-
field :sleep_for, Float do
46+
field :sleep_for, Float, resolve_legacy_instance_method: true do
4747
argument :seconds, Float
4848
argument :disable_timeout, "Boolean", default_value: false
4949
end
@@ -56,7 +56,7 @@ def sleep_for(seconds:, disable_timeout:)
5656
seconds
5757
end
5858

59-
field :nested_sleep, nested_sleep_type do
59+
field :nested_sleep, nested_sleep_type, resolve_legacy_instance_method: true do
6060
argument :seconds, Float
6161
end
6262

@@ -109,7 +109,11 @@ def nested_sleep(seconds:)
109109
]
110110
assert_graphql_equal expected_data, result["data"]
111111
assert_equal expected_errors, result["errors"]
112-
assert_equal true, result.context[:other_trace_worked], "It works with other traces"
112+
if TESTING_EXEC_NEXT
113+
refute result.context.key?(:other_trace_worked), "It terminated before calling that other trace"
114+
else
115+
assert_equal true, result.context[:other_trace_worked], "It works with other traces"
116+
end
113117
end
114118
end
115119

@@ -219,7 +223,7 @@ def handle_timeout(err, query)
219223

220224
it "calls the block" do
221225
err = assert_raises(RuntimeError) { result }
222-
assert_equal "Query timed out after 2s: Timeout on Query.sleepFor", err.message
226+
assert_equal "#{TESTING_EXEC_NEXT ? "Resolving Query.sleepFor: " : ""}Query timed out after 2s: Timeout on Query.sleepFor", err.message
223227
end
224228
end
225229

spec/integration/rails/graphql/query/variables_spec.rb

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,16 +152,20 @@ def self.thingsCount(args, ctx) # rubocop:disable Naming/MethodName
152152

153153
query_type = Class.new(GraphQL::Schema::Object) do
154154
graphql_name "Query"
155-
field :variables_test, Integer, extras: [:ast_node], camelize: false do
155+
field :variables_test, Integer, extras: [:ast_node], camelize: false, resolve_static: true do
156156
argument :val, Integer, required: false
157157
argument :val_with_default, Integer, required: false, default_value: 13, camelize: false
158158
argument :complex_val, complex_val, required: false, camelize: false
159159
end
160160

161-
def variables_test(ast_node:, **args)
161+
def self.variables_test(context, ast_node:, **args)
162162
context.schema.args_cache[ast_node.alias] = args
163163
1
164164
end
165+
166+
def variables_test(ast_node:, **args)
167+
self.class.variables_test(context, ast_node: ast_node, **args)
168+
end
165169
end
166170

167171
Class.new(GraphQL::Schema) do

0 commit comments

Comments
 (0)