Class: Rails::GraphQL::Type::Object

Inherits:
Rails::GraphQL::Type show all
Extended by:
ActiveSupport::Autoload, Helpers::WithAssignment, Helpers::WithFields
Includes:
Helpers::Instantiable
Defined in:
lib/rails/graphql/type/object.rb

Overview

GraphQL ObjectType

Objects represent a list of named fields, each of which yield a value of a specific type. See spec.graphql.org/June2018/#ObjectTypeDefinition

Defined Under Namespace

Classes: DirectiveObject, EnumValueObject, FieldObject, InputValueObject, SchemaObject, TypeObject

Constant Summary

Constants inherited from Rails::GraphQL::Type

KINDS

Instance Attribute Summary

Attributes included from Helpers::Instantiable

#event

Class Method Summary collapse

Methods included from Helpers::WithAssignment

assigned?, assigned_class, assigned_to, assigned_to=, extended, register!, safe_assigned_class, valid_assignment?

Methods included from Helpers::WithFields

change_field, configure_field, disable_fields, enable_fields, enabled_fields, extended, field, field_names, find_by_gid, find_field, find_field!, has_field?, import, import_all, proxy_field, safe_field, validate!

Methods inherited from Rails::GraphQL::Type

base_type, create!, decorate, find_by_gid, gid_base_class, input_type?, kind, kind_enum, leaf_type?, operational?, output_type?, to_gql_backtrace

Methods included from Helpers::WithDirectives

#all_directive_events, #all_directive_listeners, #directive_events?, #directive_listeners?, extended, included, #initialize_copy, #use, #using?, #validate!

Methods included from Helpers::WithGlobalID

#to_gid_param, #to_global_id

Methods included from Helpers::Registerable

#aliases, extended, #inherited, #register!, #registered?

Class Method Details

.=~(other) ⇒ Object

Check if the other type is equivalent, by checking if the other is an interface that the current object implements



55
56
57
# File 'lib/rails/graphql/type/object.rb', line 55

def =~(other)
  super || (other.interface? && implements?(other))
end

.implements(*others, import_fields: true) ⇒ Object

Use this method to assign interfaces to the object



60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/rails/graphql/type/object.rb', line 60

def implements(*others, import_fields: true)
  return if others.blank?

  current = all_interfaces&.dup
  others.flat_map do |item|
    item = find_interface!(item)
    next if current&.include?(item) || interfaces.include?(item)

    item.implemented(self, import_fields: import_fields)
    interfaces << item
  end
end

.implements?(interface) ⇒ Boolean

Check if the object implements the given interface

Returns:

  • (Boolean)


74
75
76
# File 'lib/rails/graphql/type/object.rb', line 74

def implements?(interface)
  (object = find_interface(interface)).present? && all_interfaces.include?(object)
end

.inspectObject



78
79
80
81
82
83
84
85
86
# File 'lib/rails/graphql/type/object.rb', line 78

def inspect
  return super if self.eql?(Type::Object)
  fields = @fields&.values&.map(&:inspect)
  fields = fields.presence && +" {#{fields.join(', ')}}"

  directives = inspect_directives
  directives.prepend(' ') if directives.present?
  +"#<GraphQL::Object #{gql_name}#{fields}#{directives}>"
end

.valid_member?(value) ⇒ Boolean

Plain objects can check if a given value is a valid member

Returns:

  • (Boolean)


41
42
43
44
45
46
47
48
49
50
51
# File 'lib/rails/graphql/type/object.rb', line 41

def valid_member?(value)
  return true if valid_assignment?(value)

  check_hash = value.is_a?(::Hash)
  checker = check_hash ? :key? : :respond_to?

  fields.values.all? do |field|
    value.public_send(checker, field.method_name) ||
      (check_hash && (field.null? || value.key?(field.gql_name)))
  end
end