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

#!

Constructor Details

#initializeBaseType



18
19
20
21
22
# File 'lib/graphql/base_type.rb', line 18

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

Instance Attribute Details

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



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

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.



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

def default_scalar=(value)
  @default_scalar = value
end

#descriptionString?



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

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.



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

def introspection=(value)
  @introspection = value
end

#nameString



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

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



165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/graphql/base_type.rb', line 165

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)

See Also:

  • for override on List & NonNull types


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

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

#coerce_input(value, ctx = nil) ⇒ Object



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

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



109
110
111
# File 'lib/graphql/base_type.rb', line 109

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

#coerce_isolated_result(value) ⇒ Object



113
114
115
# File 'lib/graphql/base_type.rb', line 113

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

#coerce_result(value, ctx) ⇒ Object

Raises:

  • (NotImplementedError)


151
152
153
# File 'lib/graphql/base_type.rb', line 151

def coerce_result(value, ctx)
  raise NotImplementedError
end

#connection_typeGraphQL::ObjectType



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

def connection_type
  @connection_type ||= define_connection
end

#default_relay?Boolean



48
49
50
# File 'lib/graphql/base_type.rb', line 48

def default_relay?
  @default_relay
end

#default_scalar?Boolean



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

def default_scalar?
  @default_scalar
end

#define_connection(**kwargs, &block) ⇒ GraphQL::ObjectType

Define a custom connection type for this object type



185
186
187
# File 'lib/graphql/base_type.rb', line 185

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

#define_edge(**kwargs, &block) ⇒ GraphQL::ObjectType

Define a custom edge type for this object type



196
197
198
# File 'lib/graphql/base_type.rb', line 196

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

#edge_typeGraphQL::ObjectType



190
191
192
# File 'lib/graphql/base_type.rb', line 190

def edge_type
  @edge_type ||= define_edge
end

#get_field(name) ⇒ GraphQL::Field?

Types with fields may override this



158
159
160
# File 'lib/graphql/base_type.rb', line 158

def get_field(name)
  nil
end

#initialize_copy(other) ⇒ Object



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

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

#introspection?Boolean



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

def introspection?
  @introspection
end

#list?Boolean

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



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

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.



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

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)



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

def resolve_type(value, ctx)
  self
end

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

Return a GraphQL string for the type definition

See Also:

  • for additional options}


205
206
207
208
# File 'lib/graphql/base_type.rb', line 205

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

#to_list_typeGraphQL::ListType



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

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

#to_non_null_typeGraphQL::NonNullType



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

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



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

def to_s
  name
end

#unwrapObject

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



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

def unwrap
  self
end

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



117
118
119
120
121
122
123
124
# File 'lib/graphql/base_type.rb', line 117

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



101
102
103
# File 'lib/graphql/base_type.rb', line 101

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

#validate_input(value, ctx = nil) ⇒ Object



126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/graphql/base_type.rb', line 126

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



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

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