Sha256: 6fad74191b67ff57dbd8a5ff7fc3405a55776a4d245cfe86b099966aa935b23e

Contents?: true

Size: 1.77 KB

Versions: 5

Compression:

Stored size: 1.77 KB

Contents

# frozen_string_literal: true
module GraphQL
  # A non-null type modifies another type.
  #
  # Non-null types can be created with `!` (`InnerType!`)
  # or {BaseType#to_non_null_type} (`InnerType.to_non_null_type`)
  #
  # For return types, it says that the returned value will _always_ be present.
  #
  # @example A field which _always_ returns an error
  #   field :items, !ItemType
  #   # or
  #   field :items, ItemType.to_non_null_type
  #
  # (If the application fails to return a value, {InvalidNullError} will be passed to {Schema#type_error}.)
  #
  # For input types, it says that the incoming value _must_ be provided by the query.
  #
  # @example A field which _requires_ a string input
  #   field :newNames do
  #     # ...
  #     argument :values, !types.String
  #     # or
  #     argument :values, types.String.to_non_null_type
  #   end
  #
  # (If a value isn't provided, {Query::VariableValidationError} will be raised).
  #
  # Given a non-null type, you can always get the underlying type with {#unwrap}.
  #
  class NonNullType < GraphQL::BaseType
    include GraphQL::BaseType::ModifiesAnotherType
    extend GraphQL::Delegate

    attr_reader :of_type
    def initialize(of_type:)
      super()
      @of_type = of_type
    end

    def valid_input?(value, ctx)
      validate_input(value, ctx).valid?
    end

    def validate_input(value, ctx)
      if value.nil?
        result = GraphQL::Query::InputValidationResult.new
        result.add_problem("Expected value to not be null")
        result
      else
        of_type.validate_input(value, ctx)
      end
    end

    def_delegators :@of_type, :coerce_input, :coerce_result

    def kind
      GraphQL::TypeKinds::NON_NULL
    end

    def to_s
      "#{of_type.to_s}!"
    end
    alias_method :inspect, :to_s
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
graphql-1.6.7 lib/graphql/non_null_type.rb
graphql-1.6.6 lib/graphql/non_null_type.rb
graphql-1.6.5 lib/graphql/non_null_type.rb
graphql-1.6.4 lib/graphql/non_null_type.rb
graphql-1.6.3 lib/graphql/non_null_type.rb