Class: Rails::GraphQL::Field::OutputField

Inherits:
Rails::GraphQL::Field show all
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

MutationField, SubscriptionField

Defined Under Namespace

Modules: Proxied

Constant Summary

Constants included from Helpers::WithCallbacks

Helpers::WithCallbacks::DEFAULT_EVENT_TYPES

Instance Attribute Summary

Attributes included from TypedField

#type

Attributes inherited from Rails::GraphQL::Field

#gql_name, #name, #owner

Instance Method Summary collapse

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

#to_gid_param, #to_global_id

Methods included from Helpers::WithCallbacks

extended, included, #on

Methods included from Helpers::WithEvents

extended, included, #on

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_eventsObject



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_listenersObject



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

Returns:

  • (Boolean)


173
174
175
# File 'lib/rails/graphql/field/output_field.rb', line 173

def broadcastable?
  defined?(@broadcastable) && @broadcastable
end

#entry_point?Boolean

Returns:

  • (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

Returns:

  • (Boolean)


155
156
157
# File 'lib/rails/graphql/field/output_field.rb', line 155

def events?
  super || defined?(@events) && @events.present?
end

#listeners?Boolean

Returns:

  • (Boolean)


169
170
171
# File 'lib/rails/graphql/field/output_field.rb', line 169

def listeners?
  super || defined?(@listeners) && @listeners.present?
end

#schema_typeObject

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

Returns:

  • (Boolean)


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

Raises:



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.message
end