Class: Rails::GraphQL::Field::OutputField
- Inherits:
-
Rails::GraphQL::Field
- Object
- Rails::GraphQL::Field
- Rails::GraphQL::Field::OutputField
- Includes:
- AuthorizedField, ResolvedField, TypedField, Helpers::WithArguments, Helpers::WithCallbacks, Helpers::WithEvents, Helpers::WithGlobalID, Helpers::WithValidator
- Defined in:
- lib/rails/graphql/field/output_field.rb
Overview
GraphQL Output Field
Most of the fields in a GraphQL operation are output fields or similar or proxies of it. They can express both leaf and branch data. They can also be the entry point of a GraphQL request.
Options
-
:method_name
- The name of the method used to fetch the field data (defaults to nil). -
:deprecated
- A shortcut to adding a deprecated directive to the field (defaults to nil).
Direct Known Subclasses
Defined Under Namespace
Modules: Proxied
Constant Summary
Constants included from Helpers::WithCallbacks
Helpers::WithCallbacks::DEFAULT_EVENT_TYPES
Instance Attribute Summary
Attributes included from TypedField
Attributes inherited from Rails::GraphQL::Field
Instance Method Summary collapse
-
#=~(other) ⇒ Object
Check if the arguments are also equivalent.
- #all_events ⇒ Object
- #all_listeners ⇒ Object
-
#apply_changes(**xargs, &block) ⇒ Object
Accept changes to the method name through the
apply_changes
. - #broadcastable? ⇒ Boolean
- #entry_point? ⇒ Boolean
- #events? ⇒ Boolean
-
#initialize(*args, method_name: nil, deprecated: false, **xargs, &block) ⇒ OutputField
constructor
A new instance of OutputField.
- #listeners? ⇒ Boolean
-
#schema_type ⇒ Object
By default, output fields that belongs to a schema is a query field.
-
#valid_output?(value, deep: true) ⇒ Boolean
Checks if a given raw value is valid for this field.
-
#validate! ⇒ Object
Checks if the default value of the field is valid.
-
#validate_output!(value, **xargs) ⇒ Object
Trigger the exception based value validator.
Methods included from TypedField
#as_json, #deserialize, #initialize_copy, #of_type?, #to_json, #type_klass, #valid_field_types
Methods included from ResolvedField
#dynamic_resolver?, included, #resolve, #resolver
Methods included from AuthorizedField
#authorizable?, #authorize, #authorizer
Methods included from Helpers::WithGlobalID
Methods included from Helpers::WithCallbacks
Methods included from Helpers::WithEvents
Methods included from Helpers::WithArguments
#argument, extended, #has_argument?, #has_arguments?, #id_argument, included, #initialize_copy, #ref_argument
Methods inherited from Rails::GraphQL::Field
#all_owners, #array?, #as_json, #configure, #description, #deserialize, #disable!, #disabled?, #enable!, #enabled?, #initialize_copy, input_type?, #inspect, #internal?, leaf_type?, #method_name, mutation?, #null?, #nullable?, output_type?, proxy?, proxyable_methods, #required!, #required?, #required_items!, subscription?, #to_json, #to_proxy, #valid?, #valid_input?
Methods included from Helpers::WithDescription
#desc, #description, #description=, #description?
Methods included from Helpers::WithDirectives
#all_directive_events, #all_directive_listeners, #directive_events?, #directive_listeners?, extended, included, #initialize_copy, #use, #using?
Constructor Details
#initialize(*args, method_name: nil, deprecated: false, **xargs, &block) ⇒ OutputField
Returns a new instance of OutputField.
89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/rails/graphql/field/output_field.rb', line 89 def initialize(*args, method_name: nil, deprecated: false, **xargs, &block) @method_name = method_name.to_s.underscore.to_sym unless method_name.nil? @broadcastable = xargs.delete(:broadcastable) if xargs.key?(:broadcastable) if deprecated.present? xargs[:directives] = ::Array.wrap(xargs[:directives]) xargs[:directives] << Directive::DeprecatedDirective.new( reason: (deprecated.is_a?(String) ? deprecated : nil), ) end super(*args, **xargs, &block) end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method in the class Rails::GraphQL::Field
Instance Method Details
#=~(other) ⇒ Object
Check if the arguments are also equivalent
115 116 117 |
# File 'lib/rails/graphql/field/output_field.rb', line 115 def =~(other) super && match_arguments?(other) end |
#all_events ⇒ Object
145 146 147 148 149 150 151 152 153 |
# File 'lib/rails/graphql/field/output_field.rb', line 145 def all_events if !defined?(@events) || !(local = @events).present? super elsif (inherited = super).nil? local else Helpers.merge_hash_array(inherited, local) end end |
#all_listeners ⇒ Object
159 160 161 162 163 164 165 166 167 |
# File 'lib/rails/graphql/field/output_field.rb', line 159 def all_listeners if !defined?(@listeners) || !(local = @listeners).present? super elsif (inherited = super).nil? local else inherited + local end end |
#apply_changes(**xargs, &block) ⇒ Object
Accept changes to the method name through the apply_changes
104 105 106 107 |
# File 'lib/rails/graphql/field/output_field.rb', line 104 def apply_changes(**xargs, &block) @method_name = xargs.delete(:method_name) if xargs.key?(:method_name) super end |
#broadcastable? ⇒ Boolean
173 174 175 |
# File 'lib/rails/graphql/field/output_field.rb', line 173 def broadcastable? defined?(@broadcastable) && @broadcastable end |
#entry_point? ⇒ Boolean
177 178 179 |
# File 'lib/rails/graphql/field/output_field.rb', line 177 def entry_point? owner.is_a?(Helpers::WithSchemaFields) end |
#events? ⇒ Boolean
155 156 157 |
# File 'lib/rails/graphql/field/output_field.rb', line 155 def events? super || defined?(@events) && @events.present? end |
#listeners? ⇒ Boolean
169 170 171 |
# File 'lib/rails/graphql/field/output_field.rb', line 169 def listeners? super || defined?(@listeners) && @listeners.present? end |
#schema_type ⇒ Object
By default, output fields that belongs to a schema is a query field
110 111 112 |
# File 'lib/rails/graphql/field/output_field.rb', line 110 def schema_type :query end |
#valid_output?(value, deep: true) ⇒ Boolean
Checks if a given raw value is valid for this field
120 121 122 123 124 125 126 127 |
# File 'lib/rails/graphql/field/output_field.rb', line 120 def valid_output?(value, deep: true) return false unless super return null? if value.nil? return valid_output_array?(value, deep) if array? return true unless leaf_type? || deep type_klass.valid_output?(value) end |
#validate! ⇒ Object
Checks if the default value of the field is valid
137 138 139 140 141 142 143 |
# File 'lib/rails/graphql/field/output_field.rb', line 137 def validate!(*) super if defined? super raise ArgumentError, (+<<~MSG).squish unless type_klass.output_type? The "#{type_klass.gql_name}" is not a valid output type. MSG end |
#validate_output!(value, **xargs) ⇒ Object
Trigger the exception based value validator
130 131 132 133 134 |
# File 'lib/rails/graphql/field/output_field.rb', line 130 def validate_output!(value, **xargs) super(value, :field, **xargs) rescue ValidationError => error raise InvalidValueError, error. end |