class GraphQL::Client::Schema::ObjectClass
Public Class Methods
new(data = {}, errors = Errors.new, definer = nil)
click to toggle source
# 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
Public Instance Methods
_definer()
click to toggle source
# File lib/graphql/client/schema/object_type.rb, line 196 def _definer @definer end
_spreads()
click to toggle source
# File lib/graphql/client/schema/object_type.rb, line 200 def _spreads @definer.spreads end
errors()
click to toggle source
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.
# 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
inspect()
click to toggle source
# 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
method_missing(name, *args)
click to toggle source
Calls superclass method
# 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
respond_to_missing?(name, priv)
click to toggle source
Calls superclass method
# 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()
click to toggle source
# File lib/graphql/client/schema/object_type.rb, line 204 def source_definition @definer.definition end
to_h()
click to toggle source
Public: Returns the raw response data
Returns Hash
# File lib/graphql/client/schema/object_type.rb, line 192 def to_h @data end
Private Instance Methods
has_attribute?(attr)
click to toggle source
# File lib/graphql/client/schema/object_type.rb, line 306 def has_attribute?(attr) !!@data[attr] end
read_attribute(attr, type)
click to toggle source
# 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
verify_collocated_path() { || ... }
click to toggle source
# 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