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

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

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of ObjectClass.



176
177
178
179
180
181
182
183
184
185
186
# File 'lib/graphql/client/schema/object_type.rb', line 176

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) ⇒ Object



231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
# 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://github.com/github-community-projects/graphql-client/blob/master/guides/unimplemented-field-error.md"
      end

      if @data.key?(field.name)
        raise ImplicitlyFetchedFieldError, "implicitly fetched field `#{field.name}' on #{type} type. https://github.com/github-community-projects/graphql-client/blob/master/guides/implicitly-fetched-field-error.md"
      else
        raise UnfetchedFieldError, "unfetched field `#{field.name}' on #{type} type. https://github.com/github-community-projects/graphql-client/blob/master/guides/unfetched-field-error.md"
      end
    end
  end
end

Instance Method Details

#_definerObject



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

def _definer
  @definer
end

#_spreadsObject



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

def _spreads
  @definer.spreads
end

#errorsObject

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.



223
224
225
226
227
228
229
# 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

#inspectObject



270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
# 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

#respond_to_missing?(name, priv) ⇒ Boolean

Returns:

  • (Boolean)


208
209
210
211
212
213
214
# 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_definitionObject



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

def source_definition
  @definer.definition
end

#to_hObject Also known as: to_hash

Public: Returns the raw response data

Returns Hash



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

def to_h
  @data
end