Sha256: e884c3bdf2bbac731ff30abea2d32413966c37f88538407a172c0cdab15de8a4

Contents?: true

Size: 1.58 KB

Versions: 11

Compression:

Stored size: 1.58 KB

Contents

module GraphQL
  class Query
    class Executor
      # @return [GraphQL::Query] the query being executed
      attr_reader :query

      def initialize(query)
        @query = query
      end

      # Evalute {operation_name} on {query}. Handle errors by putting them in the "errors" key.
      # (Or, if `query.debug`, by re-raising them.)
      # @return [Hash] A GraphQL response, with either a "data" key or an "errors" key
      def result
        execute
      rescue GraphQL::ExecutionError => err
        {"errors" => [err.to_h]}
      rescue GraphQL::Query::OperationNameMissingError => err
        {"errors" => [{"message" => err.message}]}
      rescue StandardError => err
        query.debug && raise(err)
        message = "Something went wrong during query execution: #{err}" #\n#{err.backtrace.join("\n  ")}"
        {"errors" => [{"message" => message}]}
      end

      private

      def execute
        operation = query.selected_operation
        return {} if operation.nil?

        op_type = operation.operation_type
        root_type = query.schema.public_send(op_type)
        execution_strategy_class = query.schema.public_send("#{op_type}_execution_strategy")
        execution_strategy = execution_strategy_class.new

        query.context.execution_strategy = execution_strategy
        data_result = execution_strategy.execute(operation, root_type, query)
        result = { "data" => data_result }
        error_result = query.context.errors.map(&:to_h)

        if error_result.any?
          result["errors"] = error_result
        end

        result
      end
    end
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
graphql-0.12.1 lib/graphql/query/executor.rb
graphql-0.12.0 lib/graphql/query/executor.rb
graphql-0.11.1 lib/graphql/query/executor.rb
graphql-0.11.0 lib/graphql/query/executor.rb
graphql-0.10.9 lib/graphql/query/executor.rb
graphql-0.10.8 lib/graphql/query/executor.rb
graphql-0.10.7 lib/graphql/query/executor.rb
graphql-0.10.6 lib/graphql/query/executor.rb
graphql-0.10.5 lib/graphql/query/executor.rb
graphql-0.10.4 lib/graphql/query/executor.rb
graphql-0.10.3 lib/graphql/query/executor.rb