Class: GraphQL::Doctor::Source::Index

Inherits:
Struct
  • Object
show all
Defined in:
lib/graphql/doctor/source/index.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(**attributes) ⇒ Index

Returns a new instance of Index.



17
18
19
# File 'lib/graphql/doctor/source/index.rb', line 17

def initialize(**attributes)
  super(**INDEX_COLLECTIONS.to_h { |name| [name, Array(attributes[name])] })
end

Class Method Details

.from_h(hash) ⇒ Object

Raises:

  • (ArgumentError)


37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/graphql/doctor/source/index.rb', line 37

def self.from_h(hash)
  raise ArgumentError, "Invalid source index" unless serialized_index?(hash)

  values = hash.transform_keys(&:to_sym)
  new(
    types: Array(values[:types]).map { |value| record(IR::TypeDefinition, value, :location) },
    fields: Array(values[:fields]).map { |value| field(value) },
    arguments: Array(values[:arguments]).map { |value| argument(value) },
    method_definitions: Array(values[:method_definitions]).map { |value| method_definition(value) },
    interfaces: Array(values[:interfaces]).map do |value|
      record(IR::InterfaceImplementation, value, :location)
    end,
    dynamics: Array(values[:dynamics]).map { |value| record(IR::DynamicDefinition, value, :location) },
    comments: Array(values[:comments]).map { |value| record(IR::SourceComment, value, :location) },
    diagnostics: Array(values[:diagnostics]).map { |value| Diagnostic.from_h(value) }
  )
end

Instance Method Details

#freeze!Object



29
30
31
32
33
34
35
# File 'lib/graphql/doctor/source/index.rb', line 29

def freeze!
  INDEX_COLLECTIONS.each do |name|
    public_send(name).each { |value| deep_freeze(value) }
    public_send(name).freeze
  end
  freeze
end

#merge(other) ⇒ Object



21
22
23
# File 'lib/graphql/doctor/source/index.rb', line 21

def merge(other)
  self.class.new(**INDEX_COLLECTIONS.to_h { |name| [name, public_send(name) + other.public_send(name)] })
end

#to_hObject



25
26
27
# File 'lib/graphql/doctor/source/index.rb', line 25

def to_h
  INDEX_COLLECTIONS.to_h { |name| [name, public_send(name).map { |value| serialize(value) }] }
end