Sha256: 661c3d71f2f8ed3f6aa8b5cb5e75c6e1b73891214494b9266f8aa630a221fe80

Contents?: true

Size: 1.62 KB

Versions: 5

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} (#{field.type})>"
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
graphql-1.6.4 lib/graphql/execution/field_result.rb
graphql-1.6.3 lib/graphql/execution/field_result.rb
graphql-1.6.2 lib/graphql/execution/field_result.rb
graphql-1.6.1 lib/graphql/execution/field_result.rb
graphql-1.6.0 lib/graphql/execution/field_result.rb