Class: GraphQL::BaseType

Inherits:
Object
  • Object
show all
Includes:
Define::InstanceDefinable, Define::NonNullWithBang
Defined in:
lib/graphql/base_type.rb

Overview

The parent for all type classes.

Defined Under Namespace

Modules: ModifiesAnotherType

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Define::InstanceDefinable

#define, #metadata, #redefine

Methods included from Define::NonNullWithBang

#!

Instance Attribute Details

#descriptionString?

Returns a description for this type

Returns:

  • (String, nil)

    a description for this type


17
18
19
# File 'lib/graphql/base_type.rb', line 17

def description
  @description
end

#nameString

Returns the name of this type, must be unique within a Schema

Returns:

  • (String)

    the name of this type, must be unique within a Schema


14
15
16
# File 'lib/graphql/base_type.rb', line 14

def name
  @name
end

Class Method Details

During schema definition, types can be defined inside procs or as strings. This function converts it to a type instance

Returns:


92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/graphql/base_type.rb', line 92

def self.resolve_related_type(type_arg)
  case type_arg
  when Proc
    # lazy-eval it
    type_arg.call
  when String
    # Get a constant by this name
    Object.const_get(type_arg)
  else
    type_arg
  end
end

Instance Method Details

#==(other) ⇒ Boolean

Returns are these types equivalent? (incl. non-null, list)

Parameters:

Returns:

  • (Boolean)

    are these types equivalent? (incl. non-null, list)


22
23
24
25
26
27
28
# File 'lib/graphql/base_type.rb', line 22

def ==(other)
  if other.is_a?(GraphQL::BaseType)
    self.to_s == other.to_s
  else
    super
  end
end

#coerce_input(value) ⇒ Object


77
78
79
80
# File 'lib/graphql/base_type.rb', line 77

def coerce_input(value)
  return nil if value.nil?
  coerce_non_null_input(value)
end

#connection_typeObject

Get the default connection type for this object type


106
107
108
# File 'lib/graphql/base_type.rb', line 106

def connection_type
  @connection_type ||= define_connection
end

#define_connection(**kwargs, &block) ⇒ Object

Define a custom connection type for this object type


111
112
113
# File 'lib/graphql/base_type.rb', line 111

def define_connection(**kwargs, &block)
  GraphQL::Relay::ConnectionType.create_type(self, **kwargs, &block)
end

#define_edge(**kwargs, &block) ⇒ Object

Define a custom edge type for this object type


121
122
123
# File 'lib/graphql/base_type.rb', line 121

def define_edge(**kwargs, &block)
  GraphQL::Relay::EdgeType.create_type(self, **kwargs, &block)
end

#edge_typeObject

Get the default edge type for this object type


116
117
118
# File 'lib/graphql/base_type.rb', line 116

def edge_type
  @edge_type ||= define_edge
end

#get_field(name) ⇒ GraphQL::Field?

Types with fields may override this

Parameters:

  • name (String)

    field name to lookup for this type

Returns:


85
86
87
# File 'lib/graphql/base_type.rb', line 85

def get_field(name)
  nil
end

#resolve_type(value) ⇒ Object

Find out which possible type to use for value. Returns self if there are no possible types (ie, not Union or Interface)


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

def resolve_type(value)
  self
end

#to_list_typeGraphQL::ListType

Returns a list version of this type

Returns:


42
43
44
# File 'lib/graphql/base_type.rb', line 42

def to_list_type
  GraphQL::ListType.new(of_type: self)
end

#to_non_null_typeGraphQL::NonNullType

Returns a non-null version of this type

Returns:


37
38
39
# File 'lib/graphql/base_type.rb', line 37

def to_non_null_type
  GraphQL::NonNullType.new(of_type: self)
end

#to_sObject Also known as: inspect

Print the human-readable name of this type using the query-string naming pattern


59
60
61
# File 'lib/graphql/base_type.rb', line 59

def to_s
  name
end

#unwrapObject

If this type is modifying an underlying type, return the underlying type. (Otherwise, return self.)


32
33
34
# File 'lib/graphql/base_type.rb', line 32

def unwrap
  self
end

#valid_input?(value, warden) ⇒ Boolean

Returns:

  • (Boolean)

65
66
67
# File 'lib/graphql/base_type.rb', line 65

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

#validate_input(value, warden) ⇒ Object


69
70
71
72
73
74
75
# File 'lib/graphql/base_type.rb', line 69

def validate_input(value, warden)
  if value.nil?
    GraphQL::Query::InputValidationResult.new
  else
    validate_non_null_input(value, warden)
  end
end