Sha256: df7fe3021fc950e11260cce0d793e2df4e9dbceb4a27948f1dad08452d269f91

Contents?: true

Size: 1.53 KB

Versions: 7

Compression:

Stored size: 1.53 KB

Contents

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

      # @return [GraphQL::Field] The field which resolved this value
      attr_reader :field

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

      def initialize(field:, value:, owner:)
        @field = field
        @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.
      # @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

        if new_value == GraphQL::Execution::Execute::PROPAGATE_NULL
          if field.type.kind.non_null?
            @owner.propagate_null
          else
            @value = nil
          end
        else
          @value = new_value
        end
      end

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

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
graphql-1.4.5 lib/graphql/execution/field_result.rb
graphql-1.4.4 lib/graphql/execution/field_result.rb
graphql-1.4.3 lib/graphql/execution/field_result.rb
graphql-1.4.2 lib/graphql/execution/field_result.rb
graphql-1.4.1 lib/graphql/execution/field_result.rb
graphql-1.4.0 lib/graphql/execution/field_result.rb
graphql-1.3.0 lib/graphql/execution/field_result.rb