Sha256: eefc52dd4cb8ca98aa5306ba460e1314a038e8a14788a42ac2a4138d3b48f499

Contents?: true

Size: 1.72 KB

Versions: 11

Compression:

Stored size: 1.72 KB

Contents

# frozen_string_literal: true

module GraphQL
  class Schema
    # Represents a list type in the schema.
    # Wraps a {Schema::Member} as a list type.
    # @see {Schema::Member::TypeSystemHelpers#to_list_type}
    class List < GraphQL::Schema::Wrapper
      include Schema::Member::ValidatesInput

      # @return [GraphQL::TypeKinds::LIST]
      def kind
        GraphQL::TypeKinds::LIST
      end

      # @return [true]
      def list?
        true
      end

      def to_type_signature
        "[#{@of_type.to_type_signature}]"
      end

      # This is for introspection, where it's expected the name will be `null`
      def graphql_name
        nil
      end

      # Also for implementing introspection
      def description
        nil
      end

      def coerce_result(value, ctx)
        value.map { |i| i.nil? ? nil : of_type.coerce_result(i, ctx) }
      end

      def coerce_input(value, ctx)
        if value.nil?
          nil
        else
          coerced = ensure_array(value).map { |item| item.nil? ? item : of_type.coerce_input(item, ctx) }
          ctx.schema.after_any_lazies(coerced, &:itself)
        end
      end

      def validate_non_null_input(value, ctx)
        result = nil
        ensure_array(value).each_with_index do |item, index|
          item_result = of_type.validate_input(item, ctx)
          if !item_result.valid?
            result ||= GraphQL::Query::InputValidationResult.new
            result.merge_result!(index, item_result)
          end
        end
        result
      end

      private

      def ensure_array(value)
        # `Array({ a: 1 })` makes `[[:a, 1]]`, so do it manually
        if value.is_a?(Array)
          value
        else
          [value]
        end
      end
    end
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
graphql-2.0.12 lib/graphql/schema/list.rb
graphql-2.0.11 lib/graphql/schema/list.rb
graphql-2.0.9 lib/graphql/schema/list.rb
graphql-2.0.8 lib/graphql/schema/list.rb
graphql-2.0.7 lib/graphql/schema/list.rb
graphql-2.0.6 lib/graphql/schema/list.rb
graphql-2.0.5 lib/graphql/schema/list.rb
graphql-2.0.4 lib/graphql/schema/list.rb
graphql-2.0.3 lib/graphql/schema/list.rb
graphql-2.0.2 lib/graphql/schema/list.rb
graphql-2.0.1 lib/graphql/schema/list.rb