Class: Rails::GraphQL::Request::Component::Spread

Inherits:
Rails::GraphQL::Request::Component show all
Includes:
Directives, SelectionSet
Defined in:
lib/rails/graphql/request/component/spread.rb

Overview

GraphQL Request Component Spread

This class holds information about a given spread that should be iterated, which connect to either a fragment or an inline selection

Instance Attribute Summary collapse

Attributes included from SelectionSet

#selection

Instance Method Summary collapse

Methods included from Directives

#directive_events, #directive_listeners, #using?

Methods inherited from Rails::GraphQL::Request::Component

#assignable?, #hash, #invalid?, #invalidate!, kind, #skip!, #skipped?, #unresolvable?

Methods included from Resolvable

#resolve!

Methods included from Preparable

#prepare!, #prepared_data!, #prepared_data?

Methods included from Organizable

#organize!

Constructor Details

#initialize(parent, node) ⇒ Spread

Returns a new instance of Spread.



19
20
21
22
23
24
25
26
# File 'lib/rails/graphql/request/component/spread.rb', line 19

def initialize(parent, node)
  @parent = parent

  @name = node[0]
  @inline = name.nil?

  super(node)
end

Instance Attribute Details

#current_objectObject (readonly)

Returns the value of attribute current_object.



17
18
19
# File 'lib/rails/graphql/request/component/spread.rb', line 17

def current_object
  @current_object
end

#fragmentObject (readonly)

Returns the value of attribute fragment.



17
18
19
# File 'lib/rails/graphql/request/component/spread.rb', line 17

def fragment
  @fragment
end

#nameObject (readonly)

Returns the value of attribute name.



17
18
19
# File 'lib/rails/graphql/request/component/spread.rb', line 17

def name
  @name
end

#parentObject (readonly)

Returns the value of attribute parent.



17
18
19
# File 'lib/rails/graphql/request/component/spread.rb', line 17

def parent
  @parent
end

#type_klassObject (readonly)

Returns the value of attribute type_klass.



17
18
19
# File 'lib/rails/graphql/request/component/spread.rb', line 17

def type_klass
  @type_klass
end

Instance Method Details

#broadcastable?Boolean

Check if all the sub fields or the fragment is broadcastable

Returns:

  • (Boolean)


34
35
36
# File 'lib/rails/graphql/request/component/spread.rb', line 34

def broadcastable?
  inline? ? selection.each_value.all?(&:broadcastable?) : fragment.broadcastable?
end

#cache_dumpObject

Build the cache object



49
50
51
# File 'lib/rails/graphql/request/component/spread.rb', line 49

def cache_dump
  inline? ? super.merge(type_klass: all_to_gid(type_klass)) : super
end

#cache_load(data) ⇒ Object

Organize from cache data



54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/rails/graphql/request/component/spread.rb', line 54

def cache_load(data)
  @name = data[:node][0]
  @inline = name.nil?

  if inline?
    @type_klass = all_from_gid(data[:type_klass])
  else
    collect_fragment
  end

  super
end

#inline?Boolean

Check if the object is an inline spread

Returns:

  • (Boolean)


29
30
31
# File 'lib/rails/graphql/request/component/spread.rb', line 29

def inline?
  @inline.present?
end

#resolve_with!(object) ⇒ Object

Redirect to the fragment or check the inline type before resolving



39
40
41
42
43
44
45
46
# File 'lib/rails/graphql/request/component/spread.rb', line 39

def resolve_with!(object)
  return if unresolvable?

  @current_object = object
  resolve!
ensure
  @current_object = nil
end