Skip to content

Commit 15b16f5

Browse files
committed
Merge field extension methods
1 parent 3fb6431 commit 15b16f5

8 files changed

Lines changed: 85 additions & 188 deletions

File tree

lib/graphql/execution/field_resolve_step.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -491,7 +491,7 @@ def finish_extensions
491491
end
492492
else
493493
memo = memos[@finish_extension_idx]
494-
@field_results = ext.after_resolve_next(objects: @extended.object, arguments: @extended.arguments, context: ctx, values: @field_results, memo: memo) # rubocop:disable Development/ContextIsPassedCop
494+
@field_results = ext.after_resolve(objects: @extended.object, arguments: @extended.arguments, context: ctx, values: @field_results, memo: memo) # rubocop:disable Development/ContextIsPassedCop
495495
end
496496
@finish_extension_idx += 1
497497
if any_lazy_results?

lib/graphql/schema/field.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -929,7 +929,7 @@ def with_extensions(obj, args, ctx)
929929
def run_next_extensions_before_resolve(objs, args, ctx, extended, idx: 0, &block)
930930
extension = @extensions[idx]
931931
if extension
932-
extension.resolve_next(objects: objs, arguments: args, context: ctx) do |extended_objs, extended_args, memo|
932+
extension.resolve(objects: objs, arguments: args, context: ctx) do |extended_objs, extended_args, memo|
933933
if memo
934934
memos = extended.memos ||= {}
935935
memos[idx] = memo

lib/graphql/schema/field/connection_extension.rb

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,22 +12,13 @@ def apply
1212
end
1313

1414
# Remove pagination args before passing it to a user method
15-
def resolve(object:, arguments:, context:)
15+
def resolve(object: nil, objects: nil, arguments:, context:)
1616
next_args = arguments.dup
1717
next_args.delete(:first)
1818
next_args.delete(:last)
1919
next_args.delete(:before)
2020
next_args.delete(:after)
21-
yield(object, next_args, arguments)
22-
end
23-
24-
def resolve_next(objects:, arguments:, context:)
25-
next_args = arguments.dup
26-
next_args.delete(:first)
27-
next_args.delete(:last)
28-
next_args.delete(:before)
29-
next_args.delete(:after)
30-
yield(objects, next_args, arguments)
21+
yield(object || objects, next_args, arguments)
3122
end
3223

3324
def after_resolve(value:, object:, arguments:, context:, memo:)
@@ -36,10 +27,6 @@ def after_resolve(value:, object:, arguments:, context:, memo:)
3627
context.schema.connections.populate_connection(field, object.object, resolved_value, original_arguments, context)
3728
end
3829
end
39-
40-
def after_resolve_next(**kwargs)
41-
raise "This should never be called -- it's hardcoded in execution instead."
42-
end
4330
end
4431
end
4532
end

lib/graphql/schema/field/scope_extension.rb

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,6 @@ def after_resolve(object:, arguments:, context:, value:, memo:)
2828
value
2929
end
3030
end
31-
32-
def after_resolve_next(**kwargs)
33-
raise "This should never be called -- it's hardcoded in execution instead."
34-
end
3531
end
3632
end
3733
end

lib/graphql/schema/field_extension.rb

Lines changed: 11 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -123,63 +123,33 @@ def after_define_apply
123123
#
124124
# Whatever this method returns will be used for execution.
125125
#
126-
# @param object [Object] The object the field is being resolved on
126+
# @param object [Object] The object the field is being resolved on (not passed by new execution)
127+
# @param objects [Array<Object>] The objects the field is being resolved on (passed by new execution)
127128
# @param arguments [Hash] Ruby keyword arguments for resolving this field
128129
# @param context [Query::Context] the context for this query
129-
# @yieldparam object [Object] The object to continue resolving the field on
130+
# @yieldparam object_or_objects [Object, Array<Object>] The object or objects (new execution) to continue resolving the field on
130131
# @yieldparam arguments [Hash] The keyword arguments to continue resolving with
131132
# @yieldparam memo [Object] Any extension-specific value which will be passed to {#after_resolve} later
132133
# @return [Object] The return value for this field.
133-
def resolve(object:, arguments:, context:)
134-
yield(object, arguments, nil)
135-
end
136-
137-
# Called before batch-resolving {#field}. It should either:
138-
#
139-
# - `yield` values to continue execution; OR
140-
# - return something else to shortcut field execution.
141-
#
142-
# Whatever this method returns will be used for execution.
143-
#
144-
# @param objects [Array<Object>] The objects the field is being resolved on
145-
# @param arguments [Hash] Ruby keyword arguments for resolving this field
146-
# @param context [Query::Context] the context for this query
147-
# @yieldparam objects [Array<Object>] The objects to continue resolving the field on. Length must be the same as passed-in `objects:`
148-
# @yieldparam arguments [Hash] The keyword arguments to continue resolving with
149-
# @yieldparam memo [Object] Any extension-specific value which will be passed to {#after_resolve} later
150-
# @return [Array<Object>] The return value for this field, length matching passed-in `objects:`.
151-
def resolve_next(objects:, arguments:, context:)
152-
yield(objects, arguments, nil)
134+
def resolve(object: nil, objects: nil, arguments:, context:)
135+
yield(object || objects, arguments, nil)
153136
end
154137

155138
# Called after {#field} was resolved, and after any lazy values (like `Promise`s) were synced,
156139
# but before the value was added to the GraphQL response.
157140
#
158141
# Whatever this hook returns will be used as the return value.
159142
#
160-
# @param object [Object] The object the field is being resolved on
143+
# @param object [Object] The object the field is being resolved on (not passed by new execution)
144+
# @param objects [Array<Object>] The object the field is being resolved on (passed by new execution)
161145
# @param arguments [Hash] Ruby keyword arguments for resolving this field
162146
# @param context [Query::Context] the context for this query
163-
# @param value [Object] Whatever the field previously returned
147+
# @param value [Object] Whatever the field previously returned (not passed by new execution)
148+
# @param values [Array<Object>] Whatever the field previously returned (passed by new execution)
164149
# @param memo [Object] The third value yielded by {#resolve}, or `nil` if there wasn't one
165150
# @return [Object] The return value for this field.
166-
def after_resolve(object:, arguments:, context:, value:, memo:)
167-
value
168-
end
169-
170-
# Called after {#field} was batch-resolved, and after any lazy values (like `Promise`s) were synced,
171-
# but before the value was added to the GraphQL response.
172-
#
173-
# Whatever this hook returns will be used as the return value.
174-
#
175-
# @param objects [Array<Object>] The objects the field is being resolved on
176-
# @param arguments [Hash] Ruby keyword arguments for resolving this field
177-
# @param context [Query::Context] the context for this query
178-
# @param values [Array<Object>] Whatever the field returned, one for each of `objects`
179-
# @param memo [Object] The third value yielded by {#resolve}, or `nil` if there wasn't one
180-
# @return [Array<Object>] The return values for this field, length matching `objects:`.
181-
def after_resolve_next(objects:, arguments:, context:, values:, memo:)
182-
values
151+
def after_resolve(object: nil, objects: nil, arguments:, context:, values: nil, value: nil, memo:)
152+
value || values
183153
end
184154
end
185155
end

lib/graphql/subscriptions/default_subscription_resolve_extension.rb

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,41 +2,41 @@
22
module GraphQL
33
class Subscriptions
44
class DefaultSubscriptionResolveExtension < GraphQL::Schema::FieldExtension
5-
def resolve(context:, object:, arguments:)
6-
has_override_implementation = @field.resolver ||
7-
object.respond_to?(@field.resolver_method)
5+
def resolve(context:, object: nil, objects: nil, arguments:)
6+
if objects
7+
has_override_implementation = @field.execution_mode != :direct_send
88

9-
if !has_override_implementation
10-
if context.query.subscription_update?
11-
object.object
9+
if !has_override_implementation
10+
if context.query.subscription_update?
11+
objects
12+
else
13+
objects.map { |o| context.skip }
14+
end
1215
else
13-
context.skip
16+
yield(objects, arguments)
1417
end
1518
else
16-
yield(object, arguments)
17-
end
18-
end
19+
has_override_implementation = @field.resolver ||
20+
object.respond_to?(@field.resolver_method)
1921

20-
def resolve_next(context:, objects:, arguments:)
21-
has_override_implementation = @field.execution_mode != :direct_send
22-
23-
if !has_override_implementation
24-
if context.query.subscription_update?
25-
objects
22+
if !has_override_implementation
23+
if context.query.subscription_update?
24+
object.object
25+
else
26+
context.skip
27+
end
2628
else
27-
objects.map { |o| context.skip }
29+
yield(object, arguments)
2830
end
29-
else
30-
yield(objects, arguments)
3131
end
3232
end
3333

34-
def after_resolve(value:, context:, object:, arguments:, **rest)
35-
self.class.write_subscription(@field, value, arguments, context)
36-
end
37-
38-
def after_resolve_next(values:, context:, objects:, arguments:, **rest)
39-
values.map do |value|
34+
def after_resolve(values: nil, value: nil, context:, object:, arguments:, **rest)
35+
if values
36+
values.map do |value|
37+
self.class.write_subscription(@field, value, arguments, context)
38+
end
39+
else
4040
self.class.write_subscription(@field, value, arguments, context)
4141
end
4242
end

0 commit comments

Comments
 (0)