Sha256: 3df93e8d0aefda944a86a98f74c10d8688c5b6b9cc90cada2fd05f5c344ebcdb

Contents?: true

Size: 1.7 KB

Versions: 3

Compression:

Stored size: 1.7 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, :existing_type_hash

  def initialize(type, existing_type_hash)
    type = type.nil? ? nil : type.unwrap
    validate_type(type)
    if type.respond_to?(:name) && existing_type_hash.fetch(type.name, nil).equal?(type)
      @result = existing_type_hash
    else
      @type = type
    end
    @existing_type_hash = existing_type_hash
  end

  def result
    @result ||= find_types(type, existing_type_hash)
  end

  # Reduce all of `types` and return the combined result
  def self.find_all(types)
    type_map = GraphQL::Schema::TypeMap.new
    types.reduce(type_map) 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|
        reduce_type(field.type, type_hash)
        field.arguments.each do |name, argument|
          reduce_type(argument.type, type_hash)
        end
      end
    end
    if type.kind.object?
      type.interfaces.each do |interface|
        reduce_type(interface, type_hash)
      end
    end
    if type.kind.resolves?
      type.possible_types.each do |possible_type|
        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

  def validate_type(type)
    errors = []
    type_validator = GraphQL::Schema::TypeValidator.new
    type_validator.validate(type, errors)
    if errors.any?
      raise GraphQL::Schema::InvalidTypeError.new(type, errors)
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
graphql-0.9.2 lib/graphql/schema/type_reducer.rb
graphql-0.8.1 lib/graphql/schema/type_reducer.rb
graphql-0.8.0 lib/graphql/schema/type_reducer.rb