Module: GraphQL::Schema::Member::HasFields::ObjectMethods

Defined in:
lib/graphql/schema/member/has_fields.rb

Overview

-- @requires_ancestor: Kernel @requires_ancestor: Module @requires_ancestor: GraphQL::Schema::Member::HasFields

Instance Method Summary collapse

Instance Method Details

#fields(context = GraphQL::Query::NullContext.instance) ⇒ Object

Returns

  • Hash<String => GraphQL::Schema::Field> — Fields on this object, keyed by name, including inherited fields

:call-seq:

fields(context:) -> Hash[String, GraphQL::Schema::Field]


319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
# File 'lib/graphql/schema/member/has_fields.rb', line 319

def fields(context = GraphQL::Query::NullContext.instance)
  # Objects need to check that the interface implementation is visible, too
  warden = Warden.from_context(context)
  # Local overrides take precedence over inherited fields
  visible_fields = {}
  had_any_fields_at_all = false #: bool
  all_ancestors = ancestors #: Array[untyped]
  for ancestor in all_ancestors
    if ancestor.respond_to?(:own_fields) && visible_interface_implementation?(ancestor, context, warden)
      ancestor.own_fields.each do |field_name, fields_entry|
        had_any_fields_at_all = true
        # Choose the most local definition that passes `.visible?` --
        # stop checking for fields by name once one has been found.
        if !visible_fields.key?(field_name) && (f = Warden.visible_entry?(:visible_field?, fields_entry, context, warden))
          visible_fields[field_name] = f.ensure_loaded
        end
      end
    end
  end
  if !had_any_fields_at_all && !has_no_fields?
    warn(GraphQL::Schema::Object::FieldsAreRequiredError.new(self).message + "\n\nThis will raise an error in a future GraphQL-Ruby version.")
  end
  visible_fields
end

#get_field(field_name, context = GraphQL::Query::NullContext.instance) ⇒ Object



295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
# File 'lib/graphql/schema/member/has_fields.rb', line 295

def get_field(field_name, context = GraphQL::Query::NullContext.instance)
  # Objects need to check that the interface implementation is visible, too
  warden = Warden.from_context(context)
  ancs = ancestors #: Array[untyped]
  skip_visible = context.respond_to?(:types) && context.types.is_a?(GraphQL::Schema::Visibility::Profile)
  i = 0
  while (ancestor = ancs[i])
    if ancestor.respond_to?(:own_fields) &&
        visible_interface_implementation?(ancestor, context, warden) &&
        (f_entry = ancestor.own_fields[field_name]) &&
        (skip_visible || (f_entry = Warden.visible_entry?(:visible_field?, f_entry, context, warden)))
      return (skip_visible ? f_entry : f_entry.ensure_loaded)
    end
    i += 1
  end
  nil
end