Sha256: f5c12aca71a4b1be27b8f05bf6d954fecc315890463dd599c3f448256afa6c83

Contents?: true

Size: 1.46 KB

Versions: 2

Compression:

Stored size: 1.46 KB

Contents

# Starting from a given type, discover other types in the system by
# traversing that type's fields, possible_types, etc
class GraphQL::Schema::TypeReducer
  attr_reader :type, :result
  def initialize(type, existing_type_hash)
    if [GraphQL::TypeKinds::NON_NULL, GraphQL::TypeKinds::LIST].include?(type.kind)
      @result = reduce_type(type.of_type, existing_type_hash)
    elsif existing_type_hash.has_key?(type.name)
      # been here, done that
      @result = existing_type_hash
    else
      @result = find_types(type, existing_type_hash.dup)
    end
  end

  # Reduce all of `types` and return the combined result
  def self.find_all(types)
    types.reduce({}) do |memo, type|
      self.new(type, memo).result
    end
  end

  private

  def find_types(type, type_hash)
    type_hash[type.name] = type
    if type.kind.fields?
      type.fields.each do |name, field|
        type_hash.merge!(reduce_type(field.type, type_hash))
        field.arguments.each do |name, argument|
          type_hash.merge!(reduce_type(argument.type, type_hash))
        end
      end
    end
    if type.kind.object?
      type.interfaces.each do |interface|
        type_hash.merge!(reduce_type(interface, type_hash))
      end
    end
    if type.kind.resolves?
      type.possible_types.each do |possible_type|
        type_hash.merge!(reduce_type(possible_type, type_hash))
      end
    end
    type_hash
  end

  def reduce_type(type, type_hash)
    self.class.new(type, type_hash).result
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
graphql-0.5.0 lib/graph_ql/schema/type_reducer.rb
graphql-0.4.0 lib/graph_ql/schema/type_reducer.rb