Module: Rails::GraphQL::Field::TypedField
- Included in:
- InputField, OutputField
- Defined in:
- lib/rails/graphql/field/typed_field.rb
Overview
This is a helper module that basically works with fields that have an assigned type value
Defined Under Namespace
Modules: Proxied
Instance Attribute Summary collapse
-
#type ⇒ Object
readonly
Returns the value of attribute type.
Instance Method Summary collapse
-
#=~(other) ⇒ Object
Check if types are compatible.
-
#all_events ⇒ Object
Add the events from the associated type.
-
#all_listeners ⇒ Object
Add the listeners from the associated type.
-
#as_json(value) ⇒ Object
Turn the given value into a JSON string representation.
-
#deserialize(value) ⇒ Object
Turn a user input of this given type into an ruby object.
-
#events? ⇒ Boolean
Make sure to check the associated type.
- #initialize(name, type = nil, *args, **xargs, &block) ⇒ Object
- #initialize_copy ⇒ Object
-
#listeners? ⇒ Boolean
Make sure to check the associated type.
-
#of_type?(klass) ⇒ Boolean
A little extension of the
is_a?method that allows checking it using thetype_klass. -
#to_json(value) ⇒ Object
Transforms the given value to its representation in a JSON string.
-
#type_klass ⇒ Object
(also: #type_class)
Return the class of the type object.
-
#valid_field_types ⇒ Object
Sometimes the owner does not designate this, but it is safe to assume it will be associated to the object valid types.
-
#validate! ⇒ Object
Checks if the type of the field is valid.
Instance Attribute Details
#type ⇒ Object (readonly)
Returns the value of attribute type.
12 13 14 |
# File 'lib/rails/graphql/field/typed_field.rb', line 12 def type @type end |
Instance Method Details
#=~(other) ⇒ Object
Check if types are compatible
29 30 31 |
# File 'lib/rails/graphql/field/typed_field.rb', line 29 def =~(other) super && other.is_a?(Field::TypedField) && other.type_klass =~ type_klass end |
#all_events ⇒ Object
Add the events from the associated type
68 69 70 71 72 73 |
# File 'lib/rails/graphql/field/typed_field.rb', line 68 def all_events inherited = super return inherited unless type_klass.events? return type_klass.all_events if inherited.blank? Helpers.merge_hash_array(inherited, type_klass.all_events) end |
#all_listeners ⇒ Object
Add the listeners from the associated type
56 57 58 59 60 |
# File 'lib/rails/graphql/field/typed_field.rb', line 56 def all_listeners inherited = super return inherited unless type_klass.listeners? inherited.present? ? inherited + type_klass.all_listeners : type_klass.all_listeners end |
#as_json(value) ⇒ Object
Turn the given value into a JSON string representation
88 89 90 91 92 |
# File 'lib/rails/graphql/field/typed_field.rb', line 88 def as_json(value) return if value.nil? return type_klass.as_json(value) unless array? value.map { |part| type_klass.as_json(part) } end |
#deserialize(value) ⇒ Object
Turn a user input of this given type into an ruby object
95 96 97 98 99 |
# File 'lib/rails/graphql/field/typed_field.rb', line 95 def deserialize(value) return if value.nil? return type_klass.deserialize(value) unless array? value.map { |val| type_klass.deserialize(val) unless val.nil? } end |
#events? ⇒ Boolean
Make sure to check the associated type
76 77 78 |
# File 'lib/rails/graphql/field/typed_field.rb', line 76 def events? super || type_klass.events? end |
#initialize(name, type = nil, *args, **xargs, &block) ⇒ Object
16 17 18 19 20 |
# File 'lib/rails/graphql/field/typed_field.rb', line 16 def initialize(name, type = nil, *args, **xargs, &block) type = (name == :id ? :id : :string) if type.nil? assign_type(type) super(name, *args, **xargs, &block) end |
#initialize_copy ⇒ Object
22 23 24 25 26 |
# File 'lib/rails/graphql/field/typed_field.rb', line 22 def initialize_copy(*) super @type_klass = nil end |
#listeners? ⇒ Boolean
Make sure to check the associated type
63 64 65 |
# File 'lib/rails/graphql/field/typed_field.rb', line 63 def listeners? super || type_klass.listeners? end |
#of_type?(klass) ⇒ Boolean
A little extension of the is_a? method that allows checking it using the type_klass
41 42 43 |
# File 'lib/rails/graphql/field/typed_field.rb', line 41 def of_type?(klass) is_a?(klass) || type_klass <= klass end |
#to_json(value) ⇒ Object
Transforms the given value to its representation in a JSON string
81 82 83 84 85 |
# File 'lib/rails/graphql/field/typed_field.rb', line 81 def to_json(value) return 'null' if value.nil? return type_klass.to_json(value) unless array? value.map { |part| type_klass.to_json(part) } end |
#type_klass ⇒ Object Also known as: type_class
Return the class of the type object
46 47 48 49 50 51 |
# File 'lib/rails/graphql/field/typed_field.rb', line 46 def type_klass @type_klass ||= GraphQL.type_map.fetch!(type, prevent_register: owner, namespaces: namespaces, ) end |
#valid_field_types ⇒ Object
Sometimes the owner does not designate this, but it is safe to assume it will be associated to the object valid types
35 36 37 |
# File 'lib/rails/graphql/field/typed_field.rb', line 35 def valid_field_types owner.try(:valid_field_types) || Type::Object.valid_field_types end |
#validate! ⇒ Object
Checks if the type of the field is valid
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/rails/graphql/field/typed_field.rb', line 102 def validate!(*) super if defined? super raise ArgumentError, (+<<~MSG).squish unless type_klass.is_a?(Module) Unable to find the "#{type.inspect}" data type on GraphQL context. MSG valid_type = valid_field_types.empty? || valid_field_types.any? do |base_type| type_klass < base_type end raise ArgumentError, (+<<~MSG).squish unless valid_type The "#{type_klass.base_type}" is not accepted in this context. MSG end |