Class: GraphQL::NonNullType

Inherits:
BaseType show all
Includes:
BaseType::ModifiesAnotherType
Defined in:
lib/graphql/non_null_type.rb

Overview

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.

(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.

(If a value isn't provided, Query::VariableValidationError will be raised).

Given a non-null type, you can always get the underlying type with BaseType::ModifiesAnotherType#unwrap.

Examples:

A field which always returns an error

field :items, !ItemType
# or
field :items, ItemType.to_non_null_type

A field which requires a string input

field :newNames do
  # ...
  argument :values, !types.String
  # or
  argument :values, types.String.to_non_null_type
end

Instance Attribute Summary collapse

Attributes inherited from BaseType

#default_relay, #default_scalar, #description, #introspection, #name

Instance Method Summary collapse

Methods included from BaseType::ModifiesAnotherType

#unwrap

Methods inherited from BaseType

#==, #connection_type, #default_relay?, #default_scalar?, #define_connection, #define_edge, #edge_type, #get_field, #initialize_copy, #introspection?, resolve_related_type, #resolve_type, #to_definition, #to_list_type, #to_non_null_type, #unwrap

Methods included from Define::InstanceDefinable

#define, #initialize_copy, #metadata, #redefine

Methods included from Define::NonNullWithBang

#!

Constructor Details

#initialize(of_type:) ⇒ NonNullType

Returns a new instance of NonNullType.



35
36
37
38
# File 'lib/graphql/non_null_type.rb', line 35

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

Instance Attribute Details

#of_typeObject (readonly)

Returns the value of attribute of_type.



34
35
36
# File 'lib/graphql/non_null_type.rb', line 34

def of_type
  @of_type
end

Instance Method Details

#coerce_input(value) ⇒ Object



54
55
56
# File 'lib/graphql/non_null_type.rb', line 54

def coerce_input(value)
  of_type.coerce_input(value)
end

#coerce_result(value) ⇒ Object



58
59
60
# File 'lib/graphql/non_null_type.rb', line 58

def coerce_result(value)
  of_type.coerce_result(value)
end

#kindObject



62
63
64
# File 'lib/graphql/non_null_type.rb', line 62

def kind
  GraphQL::TypeKinds::NON_NULL
end

#to_sObject



66
67
68
# File 'lib/graphql/non_null_type.rb', line 66

def to_s
  "#{of_type.to_s}!"
end

#valid_input?(value, warden) ⇒ Boolean

Returns:

  • (Boolean)


40
41
42
# File 'lib/graphql/non_null_type.rb', line 40

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

#validate_input(value, warden) ⇒ Object



44
45
46
47
48
49
50
51
52
# File 'lib/graphql/non_null_type.rb', line 44

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