Module: Rails::GraphQL::Field::ResolvedField
- Included in:
- OutputField
- Defined in:
- lib/rails/graphql/field/resolved_field.rb
Overview
This provides ways for fields to be resolved manually, by adding callbacks and additional configurations in order to resolve a field value during a request
Defined Under Namespace
Modules: Proxied
Class Method Summary collapse
-
.included(other) ⇒ Object
Just add the callbacks setup to the field.
Instance Method Summary collapse
-
#dynamic_resolver? ⇒ Boolean
Check if the field has a dynamic resolver.
-
#resolve(*args, **xargs, &block) ⇒ Object
Add a block that is performed while resolving a value of a field.
-
#resolver ⇒ Object
Get the resolver that can be already defined or used through the
method_name
. -
#validate! ⇒ Object
Checks if all the named callbacks can actually be called.
Class Method Details
.included(other) ⇒ Object
Just add the callbacks setup to the field
20 21 22 23 24 |
# File 'lib/rails/graphql/field/resolved_field.rb', line 20 def self.included(other) other.send(:expose_events!, :organized, :finalize, :prepared, :prepare) other.alias_method(:before_resolve, :prepare) other.alias_method(:after_resolve, :finalize) end |
Instance Method Details
#dynamic_resolver? ⇒ Boolean
Check if the field has a dynamic resolver
41 42 43 44 45 46 47 48 49 |
# File 'lib/rails/graphql/field/resolved_field.rb', line 41 def dynamic_resolver? if defined?(@dynamic_resolver) @dynamic_resolver elsif defined?(@resolver) @resolver.present? else callable?(method_name) end end |
#resolve(*args, **xargs, &block) ⇒ Object
Add a block that is performed while resolving a value of a field. It returns self
for chain purposes
28 29 30 31 |
# File 'lib/rails/graphql/field/resolved_field.rb', line 28 def resolve(*args, **xargs, &block) @resolver = Callback.new(self, :resolve, *args, **xargs, &block) self end |
#resolver ⇒ Object
Get the resolver that can be already defined or used through the method_name
35 36 37 38 |
# File 'lib/rails/graphql/field/resolved_field.rb', line 35 def resolver return unless dynamic_resolver? @resolver ||= Callback.new(self, :resolve, method_name) end |
#validate! ⇒ Object
Checks if all the named callbacks can actually be called
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/rails/graphql/field/resolved_field.rb', line 52 def validate!(*) super if defined? super # Store this result for performance purposes @dynamic_resolver = dynamic_resolver? return unless defined?(@events) # TODO: Change how events are validated. Relying on the +source_location # uses inspect, which is not a good approach # invalid = @events.each_pair.each_with_object({}) do |(key, events), hash| # events.each do |event| # _, method_name = event.source_location # next if method_name.nil? || callable?(method_name) # (hash[key] ||= []).push(method_name) # end # end # return if invalid.empty? # invalid = invalid.map { |key, list| (+"#{key} => [#{list.join(', ')}]") } # raise ArgumentError, (+<<~MSG).squish if invalid.present? # The "#{owner.name}" class does not define the following methods needed # for performing callbacks: #{invalid.join(', ')}. # MSG end |