Skip to content

Commit f0ca079

Browse files
committed
migrate RawValue to use finalizer
1 parent e2eff74 commit f0ca079

10 files changed

Lines changed: 39 additions & 29 deletions

File tree

lib/graphql/execution.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
# frozen_string_literal: true
22
require "graphql/execution/directive_checks"
3+
require "graphql/execution/next"
34
require "graphql/execution/interpreter"
45
require "graphql/execution/lazy"
56
require "graphql/execution/lookahead"
67
require "graphql/execution/multiplex"
7-
require "graphql/execution/next"
88
require "graphql/execution/errors"
99

1010
module GraphQL

lib/graphql/execution/interpreter/handles_raw_value.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@ module Execution
55
class Interpreter
66
# Wrapper for raw values
77
class RawValue
8+
include GraphQL::Execution::Next::Finalizer
9+
10+
def assign_graphql_result(query, result_data, result_key)
11+
result_data[result_key] = @object
12+
end
13+
814
def initialize(obj = nil)
915
@object = obj
1016
end

lib/graphql/execution/next/field_resolve_step.rb

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -520,8 +520,12 @@ def build_results
520520
else
521521
nil
522522
end
523-
elsif field_result.is_a?(GraphQL::RuntimeError)
524-
add_graphql_error(field_result)
523+
elsif field_result.is_a?(Finalizer)
524+
if field_result.is_a?(GraphQL::RuntimeError)
525+
add_graphql_error(field_result)
526+
else
527+
field_result.path = path
528+
end
525529
else
526530
# TODO `nil`s in [T!] types aren't handled
527531
return_type.coerce_result(field_result, ctx)
@@ -596,8 +600,13 @@ def build_graphql_result(graphql_result, key, field_result, return_type, is_nn,
596600
else
597601
graphql_result[key] = nil
598602
end
599-
elsif field_result.is_a?(GraphQL::RuntimeError)
600-
graphql_result[key] = add_graphql_error(field_result)
603+
elsif field_result.is_a?(Finalizer)
604+
graphql_result[key] = if field_result.is_a?(GraphQL::RuntimeError)
605+
add_graphql_error(field_result)
606+
else
607+
field_result.path = path
608+
field_result
609+
end
601610
elsif is_list
602611
if is_nn
603612
return_type = return_type.of_type
@@ -728,16 +737,6 @@ def resolve_batch(objects, context, args_hash)
728737
end
729738
end
730739
end
731-
732-
class RawValueFieldResolveStep < FieldResolveStep
733-
def build_graphql_result(graphql_result, key, field_result, return_type, is_nn, is_list, is_from_array) # rubocop:disable Metrics/ParameterLists
734-
if field_result.is_a?(Interpreter::RawValue)
735-
graphql_result[key] = field_result.resolve
736-
else
737-
super
738-
end
739-
end
740-
end
741740
end
742741
end
743742
end

lib/graphql/execution/next/runner.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ def initialize(multiplex, authorization:)
1313
@dataloader = multiplex.context[:dataloader] ||= @schema.dataloader_class.new
1414
@resolves_lazies = @schema.resolves_lazies?
1515
@lazy_cache = resolves_lazies ? {}.compare_by_identity : nil
16-
@field_resolve_step_class = @schema.uses_raw_value? ? RawValueFieldResolveStep : FieldResolveStep
1716
@authorization = authorization
1817
if @authorization
1918
@authorizes_cache = Hash.new do |h, query_context|
@@ -180,7 +179,8 @@ def execute
180179
@schema.subscriptions.finish_subscriptions(query)
181180
end
182181

183-
fin_result = if query.context.errors.empty?
182+
finalizers = query.finalizers
183+
fin_result = if finalizers.empty?
184184
result
185185
else
186186
data = result["data"]
@@ -216,7 +216,7 @@ def gather_selections(type_defn, ast_selections, selections_step, query, prototy
216216
step = into[key] ||= begin
217217
prototype_result[key] = nil
218218

219-
@field_resolve_step_class.new(
219+
FieldResolveStep.new(
220220
selections_step: selections_step,
221221
key: key,
222222
parent_type: type_defn,

lib/graphql/query.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,7 @@ def finalizers
308308
def add_finalizer(finalizer)
309309
f = @finalizers ||= []
310310
f << finalizer
311-
f
311+
finalizer
312312
end
313313

314314
# Determine the values for variables of this query, using default values

lib/graphql/query/context.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,13 @@ def add_error(error)
126126
nil
127127
end
128128

129+
# @param value [Object] Any object to be inserted directly into the final response
130+
# @return [GraphQL::Execution::Interpreter::RawValue] Return this from the field
131+
def raw_value(value)
132+
rv = GraphQL::Execution::Interpreter::RawValue.new(rv)
133+
query.add_finalizer(rv)
134+
end
135+
129136
# @example Print the GraphQL backtrace during field resolution
130137
# puts ctx.backtrace
131138
#

lib/graphql/runtime_error.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# frozen_string_literal: true
2+
module GraphQL
3+
class RuntimeError < Error
4+
include GraphQL::Execution::Next::Finalizer
5+
end
6+
end

lib/graphql/schema.rb

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1362,14 +1362,6 @@ def lazy_resolve(lazy_class, value_method)
13621362
lazy_methods.set(lazy_class, value_method)
13631363
end
13641364

1365-
def uses_raw_value?
1366-
!!@uses_raw_value
1367-
end
1368-
1369-
def uses_raw_value(new_val)
1370-
@uses_raw_value = new_val
1371-
end
1372-
13731365
def resolves_lazies?
13741366
lazy_method_count = 0
13751367
lazy_methods.each do |k, v|

lib/graphql/schema/object.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ def dataloader
3232
# Call this in a field method to return a value that should be returned to the client
3333
# without any further handling by GraphQL.
3434
def raw_value(obj)
35-
GraphQL::Execution::Interpreter::RawValue.new(obj)
35+
rv = GraphQL::Execution::Interpreter::RawValue.new(obj)
36+
context.query.add_finalizer(rv)
3637
end
3738

3839
class << self

spec/graphql/execution/interpreter_spec.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,6 @@ class Schema < GraphQL::Schema
296296
query(Query)
297297
mutation(Mutation)
298298
lazy_resolve(Box, :value)
299-
uses_raw_value(true)
300299
use GraphQL::Schema::AlwaysVisible
301300
use(GraphQL::Execution::Next) if TESTING_EXEC_NEXT
302301

0 commit comments

Comments
 (0)