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

Inherits:
Object
  • Object
show all
Extended by:
ClassMethods
Defined in:
lib/graphql/client/schema/object_type.rb

Defined Under Namespace

Modules: ClassMethods

Instance Attribute Summary collapse

Attributes included from ClassMethods

#_spreads, #source_definition

Instance Method Summary collapse

Constructor Details

#initialize(data = {}, errors = Errors.new) ⇒ ObjectClass

Returns a new instance of ObjectClass.



187
188
189
190
191
# File 'lib/graphql/client/schema/object_type.rb', line 187

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

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(*args) ⇒ Object



205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
# File 'lib/graphql/client/schema/object_type.rb', line 205

def method_missing(*args)
  super
rescue NoMethodError => e
  type = self.class.type

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

  field = type.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} type. https://git.io/v1y3m"
  end

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

  raise error_class, message
end

Instance Attribute Details

#errorsObject (readonly)

Public: Return errors associated with data.

Returns Errors collection.



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

def errors
  @errors
end

Instance Method Details

#inspectObject



233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
# File 'lib/graphql/client/schema/object_type.rb', line 233

def inspect
  parent = self.class.ancestors.select { |m| m.is_a?(ObjectType) }.last

  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

#to_hObject

Public: Returns the raw response data

Returns Hash



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

def to_h
  @data
end