Sha256: ef4ac36cc8270fafae66de8288c11d3af6ef5a06c2ffbd1c08100434856476e2

Contents?: true

Size: 1.43 KB

Versions: 59

Compression:

Stored size: 1.43 KB

Contents

# frozen_string_literal: true
# test_via: ../query.rb
module GraphQL
  class Query
    class Executor
      class PropagateNull < StandardError; end

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

      def initialize(query)
        @query = query
      end

      # Evaluate {operation_name} on {query}.
      # Handle {GraphQL::ExecutionError}s by putting them in the "errors" key.
      # @return [Hash] A GraphQL response, with either a "data" key or an "errors" key
      def result
        execute
      rescue GraphQL::ExecutionError => err
        query.context.errors << err
        {"errors" => [err.to_h]}
      end

      private

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

        op_type = operation.operation_type
        root_type = query.root_type_for_operation(op_type)
        execution_strategy_class = query.schema.execution_strategy_for_operation(op_type)
        execution_strategy = execution_strategy_class.new

        query.context.execution_strategy = execution_strategy
        data_result = begin
          execution_strategy.execute(operation, root_type, query)
        rescue PropagateNull
          nil
        end
        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

59 entries across 59 versions & 1 rubygems

Version Path
graphql-1.11.10 lib/graphql/query/executor.rb
graphql-1.11.9 lib/graphql/query/executor.rb
graphql-1.11.8 lib/graphql/query/executor.rb
graphql-1.12.3 lib/graphql/query/executor.rb
graphql-1.12.2 lib/graphql/query/executor.rb
graphql-1.12.1 lib/graphql/query/executor.rb
graphql-1.12.0 lib/graphql/query/executor.rb
graphql-1.11.7 lib/graphql/query/executor.rb
graphql-1.11.6 lib/graphql/query/executor.rb
graphql-1.11.5 lib/graphql/query/executor.rb
graphql-1.11.4 lib/graphql/query/executor.rb
graphql-1.11.3 lib/graphql/query/executor.rb
graphql-1.11.2 lib/graphql/query/executor.rb
graphql-1.10.14 lib/graphql/query/executor.rb
graphql-1.11.1 lib/graphql/query/executor.rb
graphql-1.10.13 lib/graphql/query/executor.rb
graphql-1.11.0 lib/graphql/query/executor.rb
graphql-1.10.12 lib/graphql/query/executor.rb
graphql-1.9.21 lib/graphql/query/executor.rb
graphql-1.10.11 lib/graphql/query/executor.rb