Class: Graphlyte::Syntax::Type

Inherits:
Data
  • Object
show all
Defined in:
lib/graphlyte/syntax.rb

Overview

A reference to a type, possibly containing other types. See: spec.graphql.org/October2021/#sec-Type-References

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Data

#==, attr_accessor, attr_reader, attributes, #dup, #eql?, #hash, #inspect

Constructor Details

#initialize(name = nil, **kwargs) ⇒ Type

Returns a new instance of Type.



170
171
172
173
174
175
176
# File 'lib/graphlyte/syntax.rb', line 170

def initialize(name = nil, **kwargs)
  super(**kwargs)
  @inner ||= name
  @is_list ||= false
  @non_null ||= false
  @non_null_list ||= false
end

Instance Attribute Details

#innerObject

Returns the value of attribute inner.



168
169
170
# File 'lib/graphlyte/syntax.rb', line 168

def inner
  @inner
end

#is_listObject

Returns the value of attribute is_list.



168
169
170
# File 'lib/graphlyte/syntax.rb', line 168

def is_list
  @is_list
end

#non_nullObject

Returns the value of attribute non_null.



168
169
170
# File 'lib/graphlyte/syntax.rb', line 168

def non_null
  @non_null
end

#non_null_listObject

Returns the value of attribute non_null_list.



168
169
170
# File 'lib/graphlyte/syntax.rb', line 168

def non_null_list
  @non_null_list
end

Class Method Details

.from_type_ref(type_ref) ⇒ Object

Raises:

  • (ArgumentError)


202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
# File 'lib/graphlyte/syntax.rb', line 202

def self.from_type_ref(type_ref)
  raise ArgumentError, 'type_ref cannot be nil' if type_ref.nil?

  inner = from_type_ref(type_ref.of_type) if type_ref.of_type

  case type_ref.kind
  when :NON_NULL
    non_null(inner)
  when :LIST
    list_type(inner)
  when :SCALAR, :OBJECT, :ENUM
    raise ArgumentError, "#{type_ref.kind} cannot have inner type" if inner

    new(inner: type_ref.name)
  else
    raise ArgumentError, "Unexpected kind: #{type_ref.kind.inspect}"
  end
end

.list_of(inner) ⇒ Object

Used during value->type inference always non-null, because we have a value.



180
181
182
183
184
185
# File 'lib/graphlyte/syntax.rb', line 180

def self.list_of(inner)
  t = new(inner)
  t.is_list = true
  t.non_null = true
  t
end

.list_type(inner) ⇒ Object

Raises:

  • (ArgumentError)


221
222
223
224
225
# File 'lib/graphlyte/syntax.rb', line 221

def self.list_type(inner)
  raise ArgumentError, 'List types must have inner type' unless inner

  new(is_list: true, inner: inner)
end

.non_null(inner) ⇒ Object

Raises:

  • (ArgumentError)


227
228
229
230
231
232
233
234
235
236
237
238
# File 'lib/graphlyte/syntax.rb', line 227

def self.non_null(inner)
  raise ArgumentError, 'Non null types must have inner type' unless inner

  type = new(non_null: true, inner: inner)

  if inner.respond_to?(:is_list) && inner.is_list
    type.is_list = true
    type.inner = inner.inner
  end

  type
end

Instance Method Details

#to_sObject



187
188
189
190
191
192
193
194
# File 'lib/graphlyte/syntax.rb', line 187

def to_s
  str = inner.to_s
  str = "#{str}!" if non_null
  str = "[#{str}]" if is_list
  str += '!' if non_null_list

  str
end

#unpackObject



196
197
198
199
200
# File 'lib/graphlyte/syntax.rb', line 196

def unpack
  return inner if inner.is_a?(String)

  inner.unpack
end