Skip to content

Commit ecfd85c

Browse files
committed
Reduce nesting in happy path
1 parent 047848a commit ecfd85c

File tree

1 file changed

+73
-69
lines changed

1 file changed

+73
-69
lines changed

lib/graphql/schema/field.rb

Lines changed: 73 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -598,87 +598,91 @@ def authorized?(object, args, context)
598598
# @param args [Hash] A symbol-keyed hash of Ruby keyword arguments. (Empty if no args)
599599
# @param ctx [GraphQL::Query::Context]
600600
def resolve(object, args, query_ctx)
601-
begin
602-
# Unwrap the GraphQL object to get the application object.
603-
application_object = object.object
604-
605-
Schema::Validator.validate!(validators, application_object, query_ctx, args)
606-
607-
query_ctx.schema.after_lazy(self.authorized?(application_object, args, query_ctx)) do |is_authorized|
608-
if is_authorized
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)
601+
# Unwrap the GraphQL object to get the application object.
602+
application_object = object.object
603+
604+
Schema::Validator.validate!(validators, application_object, query_ctx, args)
605+
606+
query_ctx.schema.after_lazy(self.authorized?(application_object, args, query_ctx)) do |is_authorized|
607+
if is_authorized
608+
with_extensions(object, args, query_ctx) do |obj, ruby_kwargs|
609+
begin
610+
method_receiver = nil
611+
method_to_call = nil
612+
if @resolver_class
613+
if obj.is_a?(GraphQL::Schema::Object)
614+
obj = obj.object
618615
end
616+
obj = @resolver_class.new(object: obj, context: query_ctx, field: self)
617+
end
619618

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
619+
# Find a way to resolve this field, checking:
620+
#
621+
# - A method on the type instance;
622+
# - Hash keys, if the wrapped object is a hash;
623+
# - A method on the wrapped object;
624+
# - Or, raise not implemented.
625+
#
626+
if obj.respond_to?(resolver_method)
627+
method_to_call = resolver_method
628+
method_receiver = obj
629+
# Call the method with kwargs, if there are any
630+
if ruby_kwargs.any?
631+
obj.public_send(resolver_method, **ruby_kwargs)
632+
else
633+
obj.public_send(resolver_method)
634+
end
635+
elsif obj.object.is_a?(Hash)
636+
inner_object = obj.object
637+
if @dig_keys
638+
inner_object.dig(*@dig_keys)
639+
elsif inner_object.key?(@method_sym)
640+
inner_object[@method_sym]
653641
else
654-
raise <<-ERR
655-
Failed to implement #{@owner.graphql_name}.#{@name}, tried:
642+
inner_object[@method_str]
643+
end
644+
elsif obj.object.respond_to?(@method_sym)
645+
method_to_call = @method_sym
646+
method_receiver = obj.object
647+
if ruby_kwargs.any?
648+
obj.object.public_send(@method_sym, **ruby_kwargs)
649+
else
650+
obj.object.public_send(@method_sym)
651+
end
652+
else
653+
raise <<-ERR
654+
Failed to implement #{@owner.graphql_name}.#{@name}, tried:
656655
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
656+
- `#{obj.class}##{resolver_method}`, which did not exist
657+
- `#{obj.object.class}##{@method_sym}`, which did not exist
658+
- Looking up hash key `#{@method_sym.inspect}` or `#{@method_str.inspect}` on `#{obj.object}`, but it wasn't a Hash
660659
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
660+
To implement this field, define one of the methods above (and check for typos)
661+
ERR
668662
end
663+
rescue ArgumentError
664+
assert_satisfactory_implementation(method_receiver, method_to_call, ruby_kwargs)
665+
# if the line above doesn't raise, re-raise
666+
raise
669667
end
670-
else
671-
raise GraphQL::UnauthorizedFieldError.new(object: application_object, type: object.class, context: query_ctx, field: self)
672668
end
669+
else
670+
raise GraphQL::UnauthorizedFieldError.new(object: application_object, type: object.class, context: query_ctx, field: self)
673671
end
674-
rescue GraphQL::UnauthorizedFieldError => err
675-
err.field ||= self
672+
end
673+
rescue GraphQL::UnauthorizedFieldError => err
674+
err.field ||= self
675+
begin
676676
query_ctx.schema.unauthorized_field(err)
677-
rescue GraphQL::UnauthorizedError => err
677+
rescue GraphQL::ExecutionError => err
678+
err
679+
end
680+
rescue GraphQL::UnauthorizedError => err
681+
begin
678682
query_ctx.schema.unauthorized_object(err)
683+
rescue GraphQL::ExecutionError => err
684+
err
679685
end
680-
rescue GraphQL::ExecutionError => err
681-
err
682686
end
683687

684688
# @param ctx [GraphQL::Query::Context]

0 commit comments

Comments
 (0)