Skip to content

Commit 047848a

Browse files
committed
inline public_send_field
1 parent 554f5f6 commit 047848a

File tree

1 file changed

+67
-74
lines changed

1 file changed

+67
-74
lines changed

lib/graphql/schema/field.rb

Lines changed: 67 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -597,28 +597,85 @@ def authorized?(object, args, context)
597597
# @param object [GraphQL::Schema::Object] An instance of some type class, wrapping an application object
598598
# @param args [Hash] A symbol-keyed hash of Ruby keyword arguments. (Empty if no args)
599599
# @param ctx [GraphQL::Query::Context]
600-
def resolve(object, args, ctx)
601-
if @resolve_proc
602-
raise "Can't run resolve proc for #{path} when using GraphQL::Execution::Interpreter"
603-
end
600+
def resolve(object, args, query_ctx)
604601
begin
605602
# Unwrap the GraphQL object to get the application object.
606603
application_object = object.object
607604

608-
Schema::Validator.validate!(validators, application_object, ctx, args)
605+
Schema::Validator.validate!(validators, application_object, query_ctx, args)
609606

610-
ctx.schema.after_lazy(self.authorized?(application_object, args, ctx)) do |is_authorized|
607+
query_ctx.schema.after_lazy(self.authorized?(application_object, args, query_ctx)) do |is_authorized|
611608
if is_authorized
612-
public_send_field(object, args, ctx)
609+
with_extensions(object, args, query_ctx) do |obj, ruby_kwargs|
610+
begin
611+
method_receiver = nil
612+
method_to_call = nil
613+
if @resolver_class
614+
if obj.is_a?(GraphQL::Schema::Object)
615+
obj = obj.object
616+
end
617+
obj = @resolver_class.new(object: obj, context: query_ctx, field: self)
618+
end
619+
620+
# Find a way to resolve this field, checking:
621+
#
622+
# - A method on the type instance;
623+
# - Hash keys, if the wrapped object is a hash;
624+
# - A method on the wrapped object;
625+
# - Or, raise not implemented.
626+
#
627+
if obj.respond_to?(resolver_method)
628+
method_to_call = resolver_method
629+
method_receiver = obj
630+
# Call the method with kwargs, if there are any
631+
if ruby_kwargs.any?
632+
obj.public_send(resolver_method, **ruby_kwargs)
633+
else
634+
obj.public_send(resolver_method)
635+
end
636+
elsif obj.object.is_a?(Hash)
637+
inner_object = obj.object
638+
if @dig_keys
639+
inner_object.dig(*@dig_keys)
640+
elsif inner_object.key?(@method_sym)
641+
inner_object[@method_sym]
642+
else
643+
inner_object[@method_str]
644+
end
645+
elsif obj.object.respond_to?(@method_sym)
646+
method_to_call = @method_sym
647+
method_receiver = obj.object
648+
if ruby_kwargs.any?
649+
obj.object.public_send(@method_sym, **ruby_kwargs)
650+
else
651+
obj.object.public_send(@method_sym)
652+
end
653+
else
654+
raise <<-ERR
655+
Failed to implement #{@owner.graphql_name}.#{@name}, tried:
656+
657+
- `#{obj.class}##{resolver_method}`, which did not exist
658+
- `#{obj.object.class}##{@method_sym}`, which did not exist
659+
- Looking up hash key `#{@method_sym.inspect}` or `#{@method_str.inspect}` on `#{obj.object}`, but it wasn't a Hash
660+
661+
To implement this field, define one of the methods above (and check for typos)
662+
ERR
663+
end
664+
rescue ArgumentError
665+
assert_satisfactory_implementation(method_receiver, method_to_call, ruby_kwargs)
666+
# if the line above doesn't raise, re-raise
667+
raise
668+
end
669+
end
613670
else
614-
raise GraphQL::UnauthorizedFieldError.new(object: application_object, type: object.class, context: ctx, field: self)
671+
raise GraphQL::UnauthorizedFieldError.new(object: application_object, type: object.class, context: query_ctx, field: self)
615672
end
616673
end
617674
rescue GraphQL::UnauthorizedFieldError => err
618675
err.field ||= self
619-
ctx.schema.unauthorized_field(err)
676+
query_ctx.schema.unauthorized_field(err)
620677
rescue GraphQL::UnauthorizedError => err
621-
ctx.schema.unauthorized_object(err)
678+
query_ctx.schema.unauthorized_object(err)
622679
end
623680
rescue GraphQL::ExecutionError => err
624681
err
@@ -637,70 +694,6 @@ def fetch_extra(extra_name, ctx)
637694

638695
private
639696

640-
def public_send_field(unextended_obj, unextended_ruby_kwargs, query_ctx)
641-
with_extensions(unextended_obj, unextended_ruby_kwargs, query_ctx) do |obj, ruby_kwargs|
642-
begin
643-
method_receiver = nil
644-
method_to_call = nil
645-
if @resolver_class
646-
if obj.is_a?(GraphQL::Schema::Object)
647-
obj = obj.object
648-
end
649-
obj = @resolver_class.new(object: obj, context: query_ctx, field: self)
650-
end
651-
652-
# Find a way to resolve this field, checking:
653-
#
654-
# - A method on the type instance;
655-
# - Hash keys, if the wrapped object is a hash;
656-
# - A method on the wrapped object;
657-
# - Or, raise not implemented.
658-
#
659-
if obj.respond_to?(resolver_method)
660-
method_to_call = resolver_method
661-
method_receiver = obj
662-
# Call the method with kwargs, if there are any
663-
if ruby_kwargs.any?
664-
obj.public_send(resolver_method, **ruby_kwargs)
665-
else
666-
obj.public_send(resolver_method)
667-
end
668-
elsif obj.object.is_a?(Hash)
669-
inner_object = obj.object
670-
if @dig_keys
671-
inner_object.dig(*@dig_keys)
672-
elsif inner_object.key?(@method_sym)
673-
inner_object[@method_sym]
674-
else
675-
inner_object[@method_str]
676-
end
677-
elsif obj.object.respond_to?(@method_sym)
678-
method_to_call = @method_sym
679-
method_receiver = obj.object
680-
if ruby_kwargs.any?
681-
obj.object.public_send(@method_sym, **ruby_kwargs)
682-
else
683-
obj.object.public_send(@method_sym)
684-
end
685-
else
686-
raise <<-ERR
687-
Failed to implement #{@owner.graphql_name}.#{@name}, tried:
688-
689-
- `#{obj.class}##{resolver_method}`, which did not exist
690-
- `#{obj.object.class}##{@method_sym}`, which did not exist
691-
- Looking up hash key `#{@method_sym.inspect}` or `#{@method_str.inspect}` on `#{obj.object}`, but it wasn't a Hash
692-
693-
To implement this field, define one of the methods above (and check for typos)
694-
ERR
695-
end
696-
rescue ArgumentError
697-
assert_satisfactory_implementation(method_receiver, method_to_call, ruby_kwargs)
698-
# if the line above doesn't raise, re-raise
699-
raise
700-
end
701-
end
702-
end
703-
704697
def assert_satisfactory_implementation(receiver, method_name, ruby_kwargs)
705698
method_defn = receiver.method(method_name)
706699
unsatisfied_ruby_kwargs = ruby_kwargs.dup

0 commit comments

Comments
 (0)