Module: Rails::GraphQL::Helpers::WithDirectives
Overview
Helper module that allows other objects to hold directives during the definition process
Defined Under Namespace
Modules: DirectiveLocation
Class Method Summary collapse
Instance Method Summary collapse
-
#all_directive_events ⇒ Object
(also: #all_events)
Override the
all_events
method since callbacks can eventually be attached to objects that have directives, which then they need to be combined. -
#all_directive_listeners ⇒ Object
(also: #all_listeners)
Override the
all_listeners
method since callbacks can eventually be attached to objects that have directives, which then they need to be combined. -
#directive_events? ⇒ Boolean
(also: #events?)
Make sure to consider directive events while checking for events.
-
#directive_listeners? ⇒ Boolean
(also: #listeners?)
Make sure to consider directive listeners while checking for listeners.
- #initialize_copy(orig) ⇒ Object
-
#use(item_or_symbol, *list, **xargs) ⇒ Object
Use this method to assign directives to the given definition.
-
#using?(item) ⇒ Boolean
(also: #has_directive?)
Check whether a given directive is being used TODO: This does not work with the instance.
-
#validate! ⇒ Object
Validate all the directives to make sure the definition is valid.
Class Method Details
.extended(other) ⇒ Object
9 10 11 12 13 |
# File 'lib/rails/graphql/helpers/with_directives.rb', line 9 def self.extended(other) other.extend(Helpers::InheritedCollection) other.extend(WithDirectives::DirectiveLocation) other.inherited_collection(:directives) end |
.included(other) ⇒ Object
15 16 17 18 19 20 |
# File 'lib/rails/graphql/helpers/with_directives.rb', line 15 def self.included(other) other.extend(WithDirectives::DirectiveLocation) other.define_method(:directives) { @directives ||= Set.new } other.define_method(:all_directives) { @directives if defined?(@directives) } other.define_method(:directives?) { defined?(@directives) && @directives.present? } end |
Instance Method Details
#all_directive_events ⇒ Object Also known as: all_events
Override the all_events
method since callbacks can eventually be attached to objects that have directives, which then they need to be combined
106 107 108 109 110 111 112 113 114 |
# File 'lib/rails/graphql/helpers/with_directives.rb', line 106 def all_directive_events inherited = super if defined?(super) return inherited unless directives? all_directives.inject(inherited || {}) do |result, directive| next result if (val = directive.all_events).blank? Helpers.merge_hash_array(result, val) end end |
#all_directive_listeners ⇒ Object Also known as: all_listeners
Override the all_listeners
method since callbacks can eventually be attached to objects that have directives, which then they need to be combined
86 87 88 89 90 91 92 |
# File 'lib/rails/graphql/helpers/with_directives.rb', line 86 def all_directive_listeners inherited = super if defined?(super) return inherited unless directives? current = all_directives.map(&:all_listeners).compact.reduce(:+) inherited.nil? ? current : inherited + current end |
#directive_events? ⇒ Boolean Also known as: events?
Make sure to consider directive events while checking for events
119 120 121 |
# File 'lib/rails/graphql/helpers/with_directives.rb', line 119 def directive_events? (defined?(super) && super) || all_directives&.any?(&:events?) end |
#directive_listeners? ⇒ Boolean Also known as: listeners?
Make sure to consider directive listeners while checking for listeners
97 98 99 |
# File 'lib/rails/graphql/helpers/with_directives.rb', line 97 def directive_listeners? (defined?(super) && super) || all_directives&.any?(&:listeners?) end |
#initialize_copy(orig) ⇒ Object
22 23 24 25 26 27 |
# File 'lib/rails/graphql/helpers/with_directives.rb', line 22 def initialize_copy(orig) super return if orig.directives.nil? @directives = orig.directives.dup end |
#use(item_or_symbol, *list, **xargs) ⇒ Object
Use this method to assign directives to the given definition. You can also provide a symbol as a first argument and extra named-arguments to auto initialize a new instance of that directive.
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/rails/graphql/helpers/with_directives.rb', line 50 def use(item_or_symbol, *list, **xargs) if item_or_symbol.is_a?(Symbol) directive = fetch!(item_or_symbol) raise ArgumentError, (+<<~MSG).squish unless directive < GraphQL::Directive Unable to find the #{item_or_symbol.inspect} directive. MSG list = [directive.new(**xargs)] else list << item_or_symbol end directives.merge(GraphQL.directives_to_set(list, all_directives, source: self)) self rescue DefinitionError => e raise e.class, +"#{e.}\n Defined at: #{caller(2)[0]}" end |
#using?(item) ⇒ Boolean Also known as: has_directive?
Check whether a given directive is being used TODO: This does not work with the instance
70 71 72 73 74 75 76 77 |
# File 'lib/rails/graphql/helpers/with_directives.rb', line 70 def using?(item) directive = (item.is_a?(Symbol) || item.is_a?(String)) ? fetch!(item) : item raise ArgumentError, (+<<~MSG).squish unless directive < GraphQL::Directive The provided #{item.inspect} is not a valid directive. MSG !!all_directives&.any?(directive) end |
#validate! ⇒ Object
Validate all the directives to make sure the definition is valid
126 127 128 129 130 131 132 |
# File 'lib/rails/graphql/helpers/with_directives.rb', line 126 def validate!(*) super if defined? super return unless defined? @directives @directives.each(&:validate!) @directives.freeze end |