Module: Rails::GraphQL::Request::ValueWriters
- Included in:
- Component::Field, Component::Typename
- Defined in:
- lib/rails/graphql/request/helpers/value_writers.rb
Overview
A set of helper methods to write a value to the response
Constant Summary collapse
- KIND_WRITERS =
TODO: Maybe move this to a setting so it allow extensions
{ union: :write_union, interface: :write_interface, object: :write_object, }.freeze
Instance Method Summary collapse
-
#format_array_exception(error, idx) ⇒ Object
Add the item index to the exception message.
-
#write_array(value, idx = -1,, &block) ⇒ Object
Resolve a given value when it is an array.
-
#write_array!(value, &block) ⇒ Object
Helper to start writing as array TODO: Add the support for ‘iterator`.
-
#write_value(value) ⇒ Object
Write a value to the response.
Instance Method Details
#format_array_exception(error, idx) ⇒ Object
Add the item index to the exception message
61 62 63 64 65 66 67 68 69 70 |
# File 'lib/rails/graphql/request/helpers/value_writers.rb', line 61 def format_array_exception(error, idx) real_error = (+<<~MSG).squish The #{ActiveSupport::Inflector.ordinalize(idx + 1)} value of the #{gql_name} field MSG source_error = +"The #{gql_name} field value" = error..gsub(source_error, real_error) error.define_singleton_method(:message) { } end |
#write_array(value, idx = -1,, &block) ⇒ Object
Resolve a given value when it is an array
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/rails/graphql/request/helpers/value_writers.rb', line 22 def write_array(value, idx = -1, &block) return write_leaf(value) if value.nil? write_array!(value) do |item| stacked(idx += 1) do block.call(item, idx) response.next rescue StandardError => error raise if item.nil? block.call(nil, idx) response.next format_array_exception(error, idx) request.exception_to_error(error, self) end rescue StandardError => error format_array_exception(error, idx) raise end end |
#write_array!(value, &block) ⇒ Object
Helper to start writing as array TODO: Add the support for ‘iterator`
46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/rails/graphql/request/helpers/value_writers.rb', line 46 def write_array!(value, &block) raise InvalidValueError, (+<<~MSG).squish unless value.respond_to?(:each) The #{gql_name} field is excepting an array but got an "#{value.class.name}" instead. MSG @writing_array = true response.with_stack(gql_name, array: true, plain: leaf_type?) do value.each(&block) end ensure @writing_array = nil end |
#write_value(value) ⇒ Object
Write a value to the response
16 17 18 19 |
# File 'lib/rails/graphql/request/helpers/value_writers.rb', line 16 def write_value(value) return write_leaf(value) if value.nil? send(KIND_WRITERS[field.kind] || :write_leaf, value) end |