Class: GraphQL::Schema::TypeMap

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/graphql/schema/type_map.rb

Overview

Stores ‘{ name => type }` pairs for a given schema. It behaves like a hash except for a couple things:

- if you use `[key]` and that key isn't defined, 💥!
- if you try to define the same key twice, 💥!

If you want a type, but want to handle the undefined case, use #fetch.

Instance Method Summary collapse

Constructor Details

#initializeTypeMap

Returns a new instance of TypeMap.



13
14
15
# File 'lib/graphql/schema/type_map.rb', line 13

def initialize
  @storage = {}
end

Instance Method Details

#[](key) ⇒ Object



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

def [](key)
  @storage[key] || raise("No type found for '#{key}'")
end

#[]=(key, value) ⇒ Object



21
22
23
24
25
26
27
# File 'lib/graphql/schema/type_map.rb', line 21

def []=(key, value)
  if @storage.key?(key)
    raise("Duplicate type definition found for name '#{key}'")
  else
    @storage[key] = value
  end
end

#fetch(key, fallback_value) ⇒ Object



29
30
31
# File 'lib/graphql/schema/type_map.rb', line 29

def fetch(key, fallback_value)
  @storage.key?(key) ? @storage[key] : fallback_value
end