Sha256: 0c91a7154cabf2414ec84788232c63f6837bfbedf6e641acf2e8e0cae442d701

Contents?: true

Size: 908 Bytes

Versions: 20

Compression:

Stored size: 908 Bytes

Contents

module GraphQL
  class Schema
    # 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}.
    class TypeMap
      extend Forwardable
      def_delegators :@storage, :key?, :keys, :values

      def initialize
        @storage = {}
      end

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

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

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

Version data entries

20 entries across 20 versions & 1 rubygems

Version Path
graphql-0.13.0 lib/graphql/schema/type_map.rb
graphql-0.12.0 lib/graphql/schema/type_map.rb
graphql-0.11.1 lib/graphql/schema/type_map.rb
graphql-0.11.0 lib/graphql/schema/type_map.rb
graphql-0.10.9 lib/graphql/schema/type_map.rb
graphql-0.10.8 lib/graphql/schema/type_map.rb
graphql-0.10.7 lib/graphql/schema/type_map.rb
graphql-0.10.6 lib/graphql/schema/type_map.rb
graphql-0.10.5 lib/graphql/schema/type_map.rb
graphql-0.10.4 lib/graphql/schema/type_map.rb
graphql-0.10.3 lib/graphql/schema/type_map.rb
graphql-0.10.2 lib/graphql/schema/type_map.rb
graphql-0.10.1 lib/graphql/schema/type_map.rb
graphql-0.10.0 lib/graphql/schema/type_map.rb
graphql-0.9.5 lib/graphql/schema/type_map.rb
graphql-0.9.4 lib/graphql/schema/type_map.rb
graphql-0.9.3 lib/graphql/schema/type_map.rb
graphql-0.9.2 lib/graphql/schema/type_map.rb
graphql-0.8.1 lib/graphql/schema/type_map.rb
graphql-0.8.0 lib/graphql/schema/type_map.rb