Sha256: 2a3a572631373acb8ecf748d3ea405bb1558cda141fd858cefde126c9fc2d82f

Contents?: true

Size: 1.8 KB

Versions: 28

Compression:

Stored size: 1.8 KB

Contents

# frozen_string_literal: true

module GraphQL
  module Execution
    # A plugin that wraps query execution with error handling.
    # Supports class-based schemas and the new {Interpreter} runtime only.
    #
    # @example Handling ActiveRecord::NotFound
    #
    #   class MySchema < GraphQL::Schema
    #     use GraphQL::Execution::Errors
    #
    #     rescue_from(ActiveRecord::NotFound) do |err, obj, args, ctx, field|
    #       ErrorTracker.log("Not Found: #{err.message}")
    #       nil
    #     end
    #   end
    #
    class Errors
      def self.use(schema)
        schema.error_handler = self.new(schema)
      end

      def initialize(schema)
        @schema = schema
      end

      class NullErrorHandler
        def self.with_error_handling(_ctx)
          yield
        end
      end

      # Call the given block with the schema's configured error handlers.
      #
      # If the block returns a lazy value, it's not wrapped with error handling. That area will have to be wrapped itself.
      #
      # @param ctx [GraphQL::Query::Context]
      # @return [Object] Either the result of the given block, or some object to replace the result, in case of error handling.
      def with_error_handling(ctx)
        yield
      rescue StandardError => err
        rescues = ctx.schema.rescues
        _err_class, handler = rescues.find { |err_class, handler| err.is_a?(err_class) }
        if handler
          runtime_info = ctx.namespace(:interpreter) || {}
          obj = runtime_info[:current_object]
          args = runtime_info[:current_arguments]
          field = runtime_info[:current_field]
          if obj.is_a?(GraphQL::Schema::Object)
            obj = obj.object
          end
          handler.call(err, obj, args, ctx, field)
        else
          raise err
        end
      end
    end
  end
end

Version data entries

28 entries across 28 versions & 1 rubygems

Version Path
graphql-1.11.10 lib/graphql/execution/errors.rb
graphql-1.11.9 lib/graphql/execution/errors.rb
graphql-1.11.8 lib/graphql/execution/errors.rb
graphql-1.11.7 lib/graphql/execution/errors.rb
graphql-1.11.6 lib/graphql/execution/errors.rb
graphql-1.11.5 lib/graphql/execution/errors.rb
graphql-1.11.4 lib/graphql/execution/errors.rb
graphql-1.11.3 lib/graphql/execution/errors.rb
graphql-1.11.2 lib/graphql/execution/errors.rb
graphql-1.10.14 lib/graphql/execution/errors.rb
graphql-1.11.1 lib/graphql/execution/errors.rb
graphql-1.10.13 lib/graphql/execution/errors.rb
graphql-1.11.0 lib/graphql/execution/errors.rb
graphql-1.10.12 lib/graphql/execution/errors.rb
graphql-1.10.11 lib/graphql/execution/errors.rb
graphql-1.10.10 lib/graphql/execution/errors.rb
graphql-1.10.9 lib/graphql/execution/errors.rb
graphql-1.10.8 lib/graphql/execution/errors.rb
graphql-1.10.7 lib/graphql/execution/errors.rb
graphql-1.10.6 lib/graphql/execution/errors.rb