Module: Rails::GraphQL::Request::Directives

Included in:
Component::Field, Component::Fragment, Component::Operation, Component::Spread, Component::Typename
Defined in:
lib/rails/graphql/request/helpers/directives.rb

Overview

Helper module to collect the directives from fragments, operations, and fields.

Instance Method Summary collapse

Instance Method Details

#cache_dumpObject

Build the cache object



41
42
43
44
45
# File 'lib/rails/graphql/request/helpers/directives.rb', line 41

def cache_dump
  return super unless defined?(@directives)

  super.merge(directives: all_to_gid(@directives.transform_values))
end

#cache_load(data) ⇒ Object

Organize from cache data



48
49
50
51
52
53
54
# File 'lib/rails/graphql/request/helpers/directives.rb', line 48

def cache_load(data)
  return super unless data.key?(:directives)

  @directives = all_from_gid(data[:directives].transform_values).freeze

  super
end

#directive_eventsObject Also known as: all_events

Get the list of events from directives set during the request only and then caches it by request



20
21
22
23
24
25
26
27
# File 'lib/rails/graphql/request/helpers/directives.rb', line 20

def directive_events
  return unless directives?
  @directive_events ||= begin
    directives.map(&:all_events).compact.inject({}) do |lhash, rhash|
      Helpers.merge_hash_array(lhash, rhash)
    end
  end
end

#directive_listenersObject Also known as: all_listeners

Get the list of listeners from directives set during the request only



10
11
12
13
14
# File 'lib/rails/graphql/request/helpers/directives.rb', line 10

def directive_listeners
  return unless directives?
  return @directive_listeners if defined?(@directive_listeners)
  @directive_listeners = directives.map(&:all_listeners).compact.reduce(:+)
end

#using?(item) ⇒ Boolean

Check if the current component is using a directive TODO: This does not work with the instance

Returns:

  • (Boolean)


33
34
35
36
37
38
# File 'lib/rails/graphql/request/helpers/directives.rb', line 33

def using?(item)
  return false unless directives?

  directive = (item.is_a?(Symbol) || item.is_a?(String)) ? find_directive!(item) : item
  (directive < GraphQL::Directive) && directives.any?(directive)
end