Skip to content

Commit c068062

Browse files
committed
Update union_spec.rb and improve type resolution
1 parent fddf4de commit c068062

2 files changed

Lines changed: 31 additions & 10 deletions

File tree

lib/graphql/execution/field_resolve_step.rb

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -442,7 +442,16 @@ def enqueue_next_steps
442442
if (object_type = @runner.runtime_type_at[result])
443443
# OK
444444
else
445-
object_type = @runner.resolve_type(@static_type, next_object, query)
445+
query.current_trace.begin_resolve_type(@static_type, next_object, query.context)
446+
object_type = query.resolve_type(@static_type, next_object)
447+
if object_type.is_a?(Array)
448+
object_type, next_object = object_type
449+
end
450+
if @runner.resolves_lazies && @runner.lazy?(object_type)
451+
# TODO batch this
452+
object_type = sync(object_type)
453+
end
454+
query.current_trace.end_resolve_type(@static_type, next_object, query.context, object_type)
446455
@runner.runtime_type_at[result] = object_type
447456
end
448457
next_objects_by_type[object_type] << next_object

spec/graphql/schema/union_spec.rb

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -112,17 +112,25 @@ def self.resolve_type(object, ctx)
112112
end
113113

114114
class Query < GraphQL::Schema::Object
115-
field :boxed_union, BoxedUnion
115+
field :boxed_union, BoxedUnion, resolve_static: true
116116

117-
def boxed_union
117+
def self.boxed_union(context)
118118
Box.new(context[:value])
119119
end
120120

121-
field :unboxed_union, UnboxedUnion
121+
def boxed_union
122+
self.class.boxed_union(context)
123+
end
124+
125+
field :unboxed_union, UnboxedUnion, resolve_static: true
122126

123-
def unboxed_union
127+
def self.unboxed_union(context)
124128
context[:value]
125129
end
130+
131+
def unboxed_union
132+
self.class.unboxed_union(context)
133+
end
126134
end
127135

128136
query(Query)
@@ -410,29 +418,33 @@ def unboxed_union
410418
describe "use with loads:" do
411419
class UnionLoadsSchema < GraphQL::Schema
412420
class Image < GraphQL::Schema::Object
413-
field :title, String
421+
field :title, String, hash_key: :title
414422
end
415423

416424
class Video < GraphQL::Schema::Object
417-
field :title, String
425+
field :title, String, hash_key: :title
418426
end
419427

420428
class Post < GraphQL::Schema::Object
421-
field :title, String
429+
field :title, String, hash_key: :title
422430
end
423431

424432
class MediaItem < GraphQL::Schema::Union
425433
possible_types Image, Video
426434
end
427435

428436
class Query < GraphQL::Schema::Object
429-
field :media_item_type, String do
437+
field :media_item_type, String, resolve_static: true do
430438
argument :id, ID, loads: MediaItem, as: :media_item
431439
end
432440

433-
def media_item_type(media_item:)
441+
def self.media_item_type(context, media_item:)
434442
media_item[:type]
435443
end
444+
445+
def media_item_type(media_item:)
446+
self.class.media_item_type(context, media_item: media_item)
447+
end
436448
end
437449

438450
query(Query)

0 commit comments

Comments
 (0)