Sha256: 1c42363baa3fc5a810b86143cbfea2051e70c7cce9a58a465822f8a698b8e58e

Contents?: true

Size: 1.73 KB

Versions: 2

Compression:

Stored size: 1.73 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?

        if operation.operation_type == "query"
          root_type = query.schema.query
          execution_strategy_class = query.schema.query_execution_strategy
        elsif operation.operation_type == "mutation"
          root_type = query.schema.mutation
          execution_strategy_class = query.schema.mutation_execution_strategy
        end
        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

2 entries across 2 versions & 1 rubygems

Version Path
graphql-0.10.2 lib/graphql/query/executor.rb
graphql-0.10.1 lib/graphql/query/executor.rb