Sha256: 2d766f7936f3d5a26fde18a5295d2b5ea3c023cb98c39527ba9702c7e5c1cdf6

Contents?: true

Size: 1.62 KB

Versions: 4

Compression:

Stored size: 1.62 KB

Contents

# frozen_string_literal: true
module GraphQL
  module Execution
    # This is one key-value pair in a GraphQL response.
    # @api private
    class FieldResult
      # @return [Any, Lazy] the GraphQL-ready response value, or a {Lazy} instance
      attr_reader :value

      # @return [SelectionResult] The result object that this field belongs to
      attr_reader :owner

      def initialize(type:, value:, owner:)
        @type = type
        @owner = owner
        self.value = value
      end

      # Set a new value for this field in the response.
      # It may be updated after resolving a {Lazy}.
      # If it is {Execute::PROPAGATE_NULL}, tell the owner to propagate null.
      # If the value is a {SelectionResult}, make a link with it, and if it's already null,
      # propagate the null as needed.
      # If it's {Execute::Execution::SKIP}, remove this field result from its parent
      # @param new_value [Any] The GraphQL-ready value
      def value=(new_value)
        if new_value.is_a?(SelectionResult)
          if new_value.invalid_null?
            new_value = GraphQL::Execution::Execute::PROPAGATE_NULL
          else
            new_value.owner = self
          end
        end

        case new_value
        when GraphQL::Execution::Execute::PROPAGATE_NULL
          if @type.kind.non_null?
            @owner.propagate_null
          else
            @value = nil
          end
        when GraphQL::Execution::Execute::SKIP
          @owner.delete(self)
        else
          @value = new_value
        end
      end

      def inspect
        "#<FieldResult #{value.inspect} (#{@type})>"
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
graphql-1.6.8 lib/graphql/execution/field_result.rb
graphql-1.6.7 lib/graphql/execution/field_result.rb
graphql-1.6.6 lib/graphql/execution/field_result.rb
graphql-1.6.5 lib/graphql/execution/field_result.rb