Sha256: 26387173a93b2437864fcf6c133817b43937780d97c4429ac96210460c02bde0

Contents?: true

Size: 1.43 KB

Versions: 48

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

      # Evalute {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

48 entries across 48 versions & 1 rubygems

Version Path
graphql-1.8.18 lib/graphql/query/executor.rb
graphql-1.8.17 lib/graphql/query/executor.rb
graphql-1.8.16 lib/graphql/query/executor.rb
graphql-1.8.15 lib/graphql/query/executor.rb
graphql-1.8.14 lib/graphql/query/executor.rb
graphql-1.8.13 lib/graphql/query/executor.rb
graphql-1.8.12 lib/graphql/query/executor.rb
graphql-1.9.0.pre1 lib/graphql/query/executor.rb
graphql-1.8.11 lib/graphql/query/executor.rb
graphql-1.8.10 lib/graphql/query/executor.rb
graphql-1.8.9 lib/graphql/query/executor.rb
graphql-1.8.8 lib/graphql/query/executor.rb
graphql-1.8.7 lib/graphql/query/executor.rb
graphql-1.8.6 lib/graphql/query/executor.rb
graphql-1.8.5 lib/graphql/query/executor.rb
graphql-1.8.4 lib/graphql/query/executor.rb
graphql-1.8.3 lib/graphql/query/executor.rb
graphql-1.8.2 lib/graphql/query/executor.rb
graphql-1.8.1 lib/graphql/query/executor.rb
graphql-1.8.0 lib/graphql/query/executor.rb