Method: GraphQL::Schema::Field#resolve

Defined in:
lib/graphql/schema/field.rb

#resolve(object, args, query_ctx) ⇒ Object

This method is called by the interpreter for each field. You can extend it in your base field classes.

Parameters:



722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
# File 'lib/graphql/schema/field.rb', line 722

def resolve(object, args, query_ctx)
  # Unwrap the GraphQL object to get the application object.
  application_object = object.object
  method_receiver = nil
  method_to_call = nil
  method_args = nil

  @own_validators && Schema::Validator.validate!(validators, application_object, query_ctx, args)

  query_ctx.query.after_lazy(self.authorized?(application_object, args, query_ctx)) do |is_authorized|
    if is_authorized
      with_extensions(object, args, query_ctx) do |obj, ruby_kwargs|
        method_args = ruby_kwargs
        if @resolver_class
          if obj.is_a?(GraphQL::Schema::Object)
            obj = obj.object
          end
          obj = @resolver_class.new(object: obj, context: query_ctx, field: self)
        end

        inner_object = obj.object

        if !NOT_CONFIGURED.equal?(@hash_key)
          hash_value = if inner_object.is_a?(Hash)
            inner_object.key?(@hash_key) ? inner_object[@hash_key] : inner_object[@hash_key_str]
          elsif inner_object.respond_to?(:[])
            inner_object[@hash_key]
          else
            nil
          end
          if hash_value == false
            hash_value
          else
            hash_value || (@fallback_value != NOT_CONFIGURED ? @fallback_value : nil)
          end
        elsif obj.respond_to?(resolver_method)
          method_to_call = resolver_method
          method_receiver = obj
          # Call the method with kwargs, if there are any
          if !ruby_kwargs.empty?
            obj.public_send(resolver_method, **ruby_kwargs)
          else
            obj.public_send(resolver_method)
          end
        elsif inner_object.is_a?(Hash)
          if @dig_keys
            inner_object.dig(*@dig_keys)
          elsif inner_object.key?(@method_sym)
            inner_object[@method_sym]
          elsif inner_object.key?(@method_str) || !inner_object.default_proc.nil?
            inner_object[@method_str]
          elsif @fallback_value != NOT_CONFIGURED
            @fallback_value
          else
            nil
          end
        elsif inner_object.respond_to?(@method_sym)
          method_to_call = @method_sym
          method_receiver = obj.object
          if !ruby_kwargs.empty?
            inner_object.public_send(@method_sym, **ruby_kwargs)
          else
            inner_object.public_send(@method_sym)
          end
        elsif @fallback_value != NOT_CONFIGURED
          @fallback_value
        else
          raise <<-ERR
        Failed to implement #{@owner.graphql_name}.#{@name}, tried:

        - `#{obj.class}##{resolver_method}`, which did not exist
        - `#{inner_object.class}##{@method_sym}`, which did not exist
        - Looking up hash key `#{@method_sym.inspect}` or `#{@method_str.inspect}` on `#{inner_object}`, but it wasn't a Hash

        To implement this field, define one of the methods above (and check for typos), or supply a `fallback_value`.
        ERR
        end
      end
    else
      raise GraphQL::UnauthorizedFieldError.new(object: application_object, type: object.class, context: query_ctx, field: self)
    end
  end
rescue GraphQL::UnauthorizedFieldError => err
  err.field ||= self
  begin
    query_ctx.schema.unauthorized_field(err)
  rescue GraphQL::ExecutionError => err
    err
  end
rescue GraphQL::UnauthorizedError => err
  begin
    query_ctx.schema.unauthorized_object(err)
  rescue GraphQL::ExecutionError => err
    err
  end
rescue ArgumentError
  if method_receiver && method_to_call
    assert_satisfactory_implementation(method_receiver, method_to_call, method_args)
  end
  # if the line above doesn't raise, re-raise
  raise
rescue GraphQL::ExecutionError => err
  err
end