Sha256: f0ac01eb2cc3d30c5ec865a16f812675d2133373914c649b22da3fcb460ea6d4

Contents?: true

Size: 1.88 KB

Versions: 15

Compression:

Stored size: 1.88 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

      prepend Schema::Member::CachedGraphQLDefinition::DeprecatedToGraphQL

      def to_graphql
        @of_type.graphql_definition(silence_deprecation_warning: true).to_list_type
      end

      # @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 = GraphQL::Query::InputValidationResult.new
        ensure_array(value).each_with_index do |item, index|
          item_result = of_type.validate_input(item, ctx)
          if !item_result.valid?
            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

15 entries across 15 versions & 1 rubygems

Version Path
graphql-1.13.15 lib/graphql/schema/list.rb
graphql-1.13.14 lib/graphql/schema/list.rb
graphql-1.13.13 lib/graphql/schema/list.rb
graphql-1.13.12 lib/graphql/schema/list.rb
graphql-1.13.11 lib/graphql/schema/list.rb
graphql-1.13.10 lib/graphql/schema/list.rb
graphql-1.13.9 lib/graphql/schema/list.rb
graphql-1.13.8 lib/graphql/schema/list.rb
graphql-1.13.7 lib/graphql/schema/list.rb
graphql-1.13.6 lib/graphql/schema/list.rb
graphql-1.13.5 lib/graphql/schema/list.rb
graphql-1.13.4 lib/graphql/schema/list.rb
graphql-1.13.3 lib/graphql/schema/list.rb
graphql-1.13.2 lib/graphql/schema/list.rb
graphql-1.13.1 lib/graphql/schema/list.rb