Class: GraphQL::BaseType

Inherits:
Object
  • Object
show all
Includes:
Define::InstanceDefinable, Define::NonNullWithBang, Relay::TypeExtensions
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 Relay::TypeExtensions

#connection_type, #define_connection, #define_edge, #edge_type

Methods included from Define::InstanceDefinable

#define, #metadata, #redefine

Methods included from Define::NonNullWithBang

#!

Constructor Details

#initializeBaseType

Returns a new instance of BaseType.



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

def initialize
  @introspection = false
  @default_scalar = false
  @default_relay = false
end

Instance Attribute Details

#ast_nodeObject

Returns the value of attribute ast_node.



22
23
24
# File 'lib/graphql/base_type.rb', line 22

def ast_node
  @ast_node
end

#default_relay=(value) ⇒ Object (writeonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



74
75
76
# File 'lib/graphql/base_type.rb', line 74

def default_relay=(value)
  @default_relay = value
end

#default_scalar=(value) ⇒ Object (writeonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



74
75
76
# File 'lib/graphql/base_type.rb', line 74

def default_scalar=(value)
  @default_scalar = value
end

#descriptionString?

Returns a description for this type.

Returns:

  • (String, nil)

    a description for this type



56
57
58
# File 'lib/graphql/base_type.rb', line 56

def description
  @description
end

#introspection=(value) ⇒ Object (writeonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



74
75
76
# File 'lib/graphql/base_type.rb', line 74

def introspection=(value)
  @introspection = value
end

#nameString Also known as: graphql_name

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



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

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:



187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
# File 'lib/graphql/base_type.rb', line 187

def self.resolve_related_type(type_arg)
  case type_arg
  when Proc
    # lazy-eval it, then try again
    resolve_related_type(type_arg.call)
  when String
    # Get a constant by this name
    resolve_related_type(Object.const_get(type_arg))
  else
    if type_arg.respond_to?(:graphql_definition)
      type_arg.graphql_definition
    else
      type_arg
    end
  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)

See Also:

  • for override on List & NonNull types


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

def ==(other)
  other.is_a?(GraphQL::BaseType) && self.name == other.name
end

#coerce_input(value, ctx = nil) ⇒ Object



161
162
163
164
165
166
167
168
169
170
171
# File 'lib/graphql/base_type.rb', line 161

def coerce_input(value, ctx = nil)
  if value.nil?
    nil
  else
    if ctx.nil?
      warn_deprecated_coerce("coerce_isolated_input")
      ctx = GraphQL::Query::NullContext
    end
    coerce_non_null_input(value, ctx)
  end
end

#coerce_isolated_input(value) ⇒ Object



131
132
133
# File 'lib/graphql/base_type.rb', line 131

def coerce_isolated_input(value)
  coerce_input(value, GraphQL::Query::NullContext)
end

#coerce_isolated_result(value) ⇒ Object



135
136
137
# File 'lib/graphql/base_type.rb', line 135

def coerce_isolated_result(value)
  coerce_result(value, GraphQL::Query::NullContext)
end

#coerce_result(value, ctx) ⇒ Object



173
174
175
# File 'lib/graphql/base_type.rb', line 173

def coerce_result(value, ctx)
  raise GraphQL::RequiredImplementationMissingError
end

#default_relay?Boolean

Returns Is this type a built-in Relay type? (Node, PageInfo).

Returns:

  • (Boolean)

    Is this type a built-in Relay type? (Node, PageInfo)



69
70
71
# File 'lib/graphql/base_type.rb', line 69

def default_relay?
  @default_relay
end

#default_scalar?Boolean

Returns Is this type a built-in scalar type? (eg, String, Int).

Returns:

  • (Boolean)

    Is this type a built-in scalar type? (eg, String, Int)



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

def default_scalar?
  @default_scalar
end

#get_field(name) ⇒ GraphQL::Field?

Types with fields may override this

Parameters:

  • name (String)

    field name to lookup for this type

Returns:



180
181
182
# File 'lib/graphql/base_type.rb', line 180

def get_field(name)
  nil
end

#initialize_copy(other) ⇒ Object



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

def initialize_copy(other)
  super
  # Reset these derived defaults
  @connection_type = nil
  @edge_type = nil
end

#introspection?Boolean

Returns Is this type a predefined introspection type?.

Returns:

  • (Boolean)

    Is this type a predefined introspection type?



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

def introspection?
  @introspection
end

#list?Boolean

Returns true if this is a list type. A non-nullable list is considered a list.

Returns:

  • (Boolean)


220
221
222
# File 'lib/graphql/base_type.rb', line 220

def list?
  false
end

#non_null?Boolean

Returns true if this is a non-nullable type. A nullable list of non-nullables is considered nullable.

Returns:

  • (Boolean)


215
216
217
# File 'lib/graphql/base_type.rb', line 215

def non_null?
  false
end

#resolve_type(value, ctx) ⇒ Object

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



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

def resolve_type(value, ctx)
  self
end

#to_definition(schema, printer: nil, **args) ⇒ String

Return a GraphQL string for the type definition

Parameters:

Returns:

  • (String)

    type definition

See Also:

  • for additional options}


209
210
211
212
# File 'lib/graphql/base_type.rb', line 209

def to_definition(schema, printer: nil, **args)
  printer ||= GraphQL::Schema::Printer.new(schema, **args)
  printer.print_type(self)
end

#to_list_typeGraphQL::ListType

Returns a list version of this type.

Returns:



95
96
97
# File 'lib/graphql/base_type.rb', line 95

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:



90
91
92
# File 'lib/graphql/base_type.rb', line 90

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

#to_sObject Also known as: inspect, to_type_signature

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



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

def to_s
  name
end

#type_classObject



46
47
48
# File 'lib/graphql/base_type.rb', line 46

def type_class
  [:type_class]
end

#unwrapObject

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



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

def unwrap
  self
end

#valid_input?(value, ctx = nil) ⇒ Boolean

Returns:

  • (Boolean)


139
140
141
142
143
144
145
146
# File 'lib/graphql/base_type.rb', line 139

def valid_input?(value, ctx = nil)
  if ctx.nil?
    warn_deprecated_coerce("valid_isolated_input?")
    ctx = GraphQL::Query::NullContext
  end

  validate_input(value, ctx).valid?
end

#valid_isolated_input?(value) ⇒ Boolean

Returns:

  • (Boolean)


123
124
125
# File 'lib/graphql/base_type.rb', line 123

def valid_isolated_input?(value)
  valid_input?(value, GraphQL::Query::NullContext)
end

#validate_input(value, ctx = nil) ⇒ Object



148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/graphql/base_type.rb', line 148

def validate_input(value, ctx = nil)
  if ctx.nil?
    warn_deprecated_coerce("validate_isolated_input")
    ctx = GraphQL::Query::NullContext
  end

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

#validate_isolated_input(value) ⇒ Object



127
128
129
# File 'lib/graphql/base_type.rb', line 127

def validate_isolated_input(value)
  validate_input(value, GraphQL::Query::NullContext)
end