123456789_123456789_123456789_123456789_123456789_

Class: GraphQL::Client::Schema::ObjectClass

Relationships & Source Files
Inherits: Object
Defined in: lib/graphql/client/schema/object_type.rb

Class Method Summary

Instance Method Summary

Constructor Details

.new(data = {}, errors = Errors.new, definer = nil) ⇒ ObjectClass

[ GitHub ]

  
# File 'lib/graphql/client/schema/object_type.rb', line 177

def initialize(data = {}, errors = Errors.new, definer = nil)
  @data = data
  @casted_data = {}
  @errors = errors

  # If we are not provided a definition, we can use this empty default
  definer ||= ObjectType::WithDefinition.new(self.class, {}, nil, [])

  @definer = definer
  @enforce_collocated_callers = source_definition && source_definition.client.enforce_collocated_callers
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args)

[ GitHub ]

  
# File 'lib/graphql/client/schema/object_type.rb', line 231

def method_missing(name, *args)
  if (attr = self.class::READERS[name]) && (type = @definer.defined_fields[attr])
    if @enforce_collocated_callers
      verify_collocated_path do
        read_attribute(attr, type)
      end
    else
      read_attribute(attr, type)
    end
  elsif (attr = self.class::PREDICATES[name]) && @definer.defined_fields[attr]
    has_attribute?(attr)
  else
    begin
      super
    rescue NoMethodError => e
      type = self.class.type

      if ActiveSupport::Inflector.underscore(e.name.to_s) != e.name.to_s
        raise e
      end

      all_fields = type.respond_to?(:all_fields) ? type.all_fields : type.fields.values
      field = all_fields.find do |f|
        f.name == e.name.to_s || ActiveSupport::Inflector.underscore(f.name) == e.name.to_s
      end

      unless field
        raise UnimplementedFieldError, "undefined field `#{e.name}' on #{type.graphql_name} type. https://git.io/v1y3m"
      end

      if @data.key?(field.name)
        raise ImplicitlyFetchedFieldError, "implicitly fetched field `#{field.name}' on #{type} type. https://git.io/v1yGL"
      else
        raise UnfetchedFieldError, "unfetched field `#{field.name}' on #{type} type. https://git.io/v1y3U"
      end
    end
  end
end

Instance Method Details

#_definer

[ GitHub ]

  
# File 'lib/graphql/client/schema/object_type.rb', line 196

def _definer
  @definer
end

#_spreads

[ GitHub ]

  
# File 'lib/graphql/client/schema/object_type.rb', line 200

def _spreads
  @definer.spreads
end

#errors

Public: Return errors associated with data.

It’s possible to define “errors” as a field. Ideally this shouldn’t happen, but if it does we should prefer the field rather than the builtin error type.

Returns Errors collection.

[ GitHub ]

  
# File 'lib/graphql/client/schema/object_type.rb', line 223

def errors
  if type = @definer.defined_fields["errors"]
    read_attribute("errors", type)
  else
    @errors
  end
end

#has_attribute?(attr) ⇒ Boolean (private)

[ GitHub ]

  
# File 'lib/graphql/client/schema/object_type.rb', line 306

def has_attribute?(attr)
  !!@data[attr]
end

#inspect

[ GitHub ]

  
# File 'lib/graphql/client/schema/object_type.rb', line 270

def inspect
  parent = self.class
  until parent.superclass == ObjectClass
    parent = parent.superclass
  end

  ivars = @data.map { |key, value|
    if value.is_a?(Hash) || value.is_a?(Array)
      "#{key}=..."
    else
      "#{key}=#{value.inspect}"
    end
  }

  buf = "#<#{parent.name}".dup
  buf << " " << ivars.join(" ") if ivars.any?
  buf << ">"
  buf
end

#read_attribute(attr, type) (private)

[ GitHub ]

  
# File 'lib/graphql/client/schema/object_type.rb', line 300

def read_attribute(attr, type)
  @casted_data.fetch(attr) do
    @casted_data[attr] = type.cast(@data[attr], @errors.filter_by_path(attr))
  end
end

#respond_to_missing?(name, priv) ⇒ Boolean

[ GitHub ]

  
# File 'lib/graphql/client/schema/object_type.rb', line 208

def respond_to_missing?(name, priv)
  if (attr = self.class::READERS[name]) || (attr = self.class::PREDICATES[name])
    @definer.defined_fields.key?(attr) || super
  else
    super
  end
end

#source_definition

[ GitHub ]

  
# File 'lib/graphql/client/schema/object_type.rb', line 204

def source_definition
  @definer.definition
end

#to_h

Public: Returns the raw response data

Returns Hash

[ GitHub ]

  
# File 'lib/graphql/client/schema/object_type.rb', line 192

def to_h
  @data
end

#verify_collocated_path (private)

[ GitHub ]

  
# File 'lib/graphql/client/schema/object_type.rb', line 292

def verify_collocated_path
  location = caller_locations(2, 1)[0]

  CollocatedEnforcement.verify_collocated_path(location, source_definition.source_location[0]) do
    yield
  end
end