Method: GraphQL::Schema::Field#initialize

Defined in:
lib/graphql/schema/field.rb

#initialize(type: nil, name: nil, owner: nil, null: nil, description: NOT_CONFIGURED, comment: NOT_CONFIGURED, deprecation_reason: nil, method: nil, hash_key: nil, dig: nil, resolver_method: nil, connection: nil, max_page_size: NOT_CONFIGURED, default_page_size: NOT_CONFIGURED, scope: nil, introspection: false, camelize: true, trace: nil, complexity: nil, ast_node: nil, extras: EMPTY_ARRAY, extensions: EMPTY_ARRAY, connection_extension: self.class.connection_extension, resolver_class: nil, subscription_scope: nil, relay_node_field: false, relay_nodes_field: false, method_conflict_warning: true, broadcastable: NOT_CONFIGURED, arguments: EMPTY_HASH, directives: EMPTY_HASH, validates: EMPTY_ARRAY, fallback_value: NOT_CONFIGURED, dynamic_introspection: false, &definition_block) ⇒ Field

Returns a new instance of Field.

Parameters:

  • name (Symbol) (defaults to: nil)

    The underscore-cased version of this field name (will be camelized for the GraphQL API)

  • type (Class, GraphQL::BaseType, Array) (defaults to: nil)

    The return type of this field

  • owner (Class) (defaults to: nil)

    The type that this field belongs to

  • null (Boolean) (defaults to: nil)

    (defaults to true) true if this field may return null, false if it is never null

  • description (String) (defaults to: NOT_CONFIGURED)

    Field description

  • comment (String) (defaults to: NOT_CONFIGURED)

    Field comment

  • deprecation_reason (String) (defaults to: nil)

    If present, the field is marked "deprecated" with this message

  • method (Symbol) (defaults to: nil)

    The method to call on the underlying object to resolve this field (defaults to name)

  • hash_key (String, Symbol) (defaults to: nil)

    The hash key to lookup on the underlying object (if its a Hash) to resolve this field (defaults to name or name.to_s)

  • dig (Array<String, Symbol>) (defaults to: nil)

    The nested hash keys to lookup on the underlying hash to resolve this field using dig

  • resolver_method (Symbol) (defaults to: nil)

    The method on the type to call to resolve this field (defaults to name)

  • connection (Boolean) (defaults to: nil)

    true if this field should get automagic connection behavior; default is to infer by *Connection in the return type name

  • connection_extension (Class) (defaults to: self.class.connection_extension)

    The extension to add, to implement connections. If nil, no extension is added.

  • max_page_size (Integer, nil) (defaults to: NOT_CONFIGURED)

    For connections, the maximum number of items to return from this field, or nil to allow unlimited results.

  • default_page_size (Integer, nil) (defaults to: NOT_CONFIGURED)

    For connections, the default number of items to return from this field, or nil to return unlimited results.

  • introspection (Boolean) (defaults to: false)

    If true, this field will be marked as #introspection? and the name may begin with __

  • resolver_class (Class) (defaults to: nil)

    (Private) A Resolver which this field was derived from. Use resolver: to create a field with a resolver.

  • arguments ({String=>GraphQL::Schema::Argument, Hash}) (defaults to: EMPTY_HASH)

    Arguments for this field (may be added in the block, also)

  • camelize (Boolean) (defaults to: true)

    If true, the field name will be camelized when building the schema

  • complexity (Numeric) (defaults to: nil)

    When provided, set the complexity for this field

  • scope (Boolean) (defaults to: nil)

    If true, the return type's .scope_items method will be called on the return value

  • subscription_scope (Symbol, String) (defaults to: nil)

    A key in context which will be used to scope subscription payloads

  • extensions (Array<Class, Hash<Class => Object>>) (defaults to: EMPTY_ARRAY)

    Named extensions to apply to this field (see also #extension)

  • directives (Hash{Class => Hash}) (defaults to: EMPTY_HASH)

    Directives to apply to this field

  • trace (Boolean) (defaults to: nil)

    If true, a Tracing tracer will measure this scalar field

  • broadcastable (Boolean) (defaults to: NOT_CONFIGURED)

    Whether or not this field can be distributed in subscription broadcasts

  • ast_node (Language::Nodes::FieldDefinition, nil) (defaults to: nil)

    If this schema was parsed from definition, this AST node defined the field

  • method_conflict_warning (Boolean) (defaults to: true)

    If false, skip the warning if this field's method conflicts with a built-in method

  • validates (Array<Hash>) (defaults to: EMPTY_ARRAY)

    Configurations for validating this field

  • fallback_value (Object) (defaults to: NOT_CONFIGURED)

    A fallback value if the method is not defined

  • dynamic_introspection (Boolean) (defaults to: false)

    (Private, used by GraphQL-Ruby)

  • relay_node_field (Boolean) (defaults to: false)

    (Private, used by GraphQL-Ruby)

  • relay_nodes_field (Boolean) (defaults to: false)

    (Private, used by GraphQL-Ruby)

  • extras (Array<:ast_node, :parent, :lookahead, :owner, :execution_errors, :graphql_name, :argument_details, Symbol>) (defaults to: EMPTY_ARRAY)

    Extra arguments to be injected into the resolver for this field

  • definition_block (Proc)

    an additional block for configuring the field. Receive the field as a block param, or, if no block params are defined, then the block is instance_eval'd on the new GraphQL::Schema::Field.



217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
# File 'lib/graphql/schema/field.rb', line 217

def initialize(type: nil, name: nil, owner: nil, null: nil, description: NOT_CONFIGURED, comment: NOT_CONFIGURED, deprecation_reason: nil, method: nil, hash_key: nil, dig: nil, resolver_method: nil, connection: nil, max_page_size: NOT_CONFIGURED, default_page_size: NOT_CONFIGURED, scope: nil, introspection: false, camelize: true, trace: nil, complexity: nil, ast_node: nil, extras: EMPTY_ARRAY, extensions: EMPTY_ARRAY, connection_extension: self.class.connection_extension, resolver_class: nil, subscription_scope: nil, relay_node_field: false, relay_nodes_field: false, method_conflict_warning: true, broadcastable: NOT_CONFIGURED, arguments: EMPTY_HASH, directives: EMPTY_HASH, validates: EMPTY_ARRAY, fallback_value: NOT_CONFIGURED, dynamic_introspection: false, &definition_block)
  if name.nil?
    raise ArgumentError, "missing first `name` argument or keyword `name:`"
  end
  if !(resolver_class)
    if type.nil? && !block_given?
      raise ArgumentError, "missing second `type` argument, keyword `type:`, or a block containing `type(...)`"
    end
  end
  @original_name = name
  name_s = -name.to_s

  @underscored_name = -Member::BuildType.underscore(name_s)
  @name = -(camelize ? Member::BuildType.camelize(name_s) : name_s)
  NameValidator.validate!(@name)
  @description = description
  @comment = comment
  @type = @owner_type = @own_validators = @own_directives = @own_arguments = @arguments_statically_coercible = nil # these will be prepared later if necessary

  self.deprecation_reason = deprecation_reason

  if method && hash_key && dig
    raise ArgumentError, "Provide `method:`, `hash_key:` _or_ `dig:`, not multiple. (called with: `method: #{method.inspect}, hash_key: #{hash_key.inspect}, dig: #{dig.inspect}`)"
  end

  if resolver_method
    if method
      raise ArgumentError, "Provide `method:` _or_ `resolver_method:`, not both. (called with: `method: #{method.inspect}, resolver_method: #{resolver_method.inspect}`)"
    end

    if hash_key || dig
      raise ArgumentError, "Provide `hash_key:`, `dig:`, _or_ `resolver_method:`, not multiple. (called with: `hash_key: #{hash_key.inspect}, dig: #{dig.inspect}, resolver_method: #{resolver_method.inspect}`)"
    end
  end

  method_name = method || hash_key || name_s
  @dig_keys = dig
  if hash_key
    @hash_key = hash_key
    @hash_key_str = hash_key.to_s
  else
    @hash_key = NOT_CONFIGURED
    @hash_key_str = NOT_CONFIGURED
  end

  @method_str = -method_name.to_s
  @method_sym = method_name.to_sym
  @resolver_method = (resolver_method || name_s).to_sym
  @complexity = complexity
  @dynamic_introspection = dynamic_introspection
  @return_type_expr = type
  @return_type_null = if !null.nil?
    null
  elsif resolver_class
    nil
  else
    true
  end
  @connection = connection
  @max_page_size = max_page_size
  @default_page_size = default_page_size
  @introspection = introspection
  @extras = extras
  @broadcastable = broadcastable
  @resolver_class = resolver_class
  @scope = scope
  @trace = trace
  @relay_node_field = relay_node_field
  @relay_nodes_field = relay_nodes_field
  @ast_node = ast_node
  @method_conflict_warning = method_conflict_warning
  @fallback_value = fallback_value
  @definition_block = definition_block

  arguments.each do |name, arg|
    case arg
    when Hash
      argument(name: name, **arg)
    when GraphQL::Schema::Argument
      add_argument(arg)
    when Array
      arg.each { |a| add_argument(a) }
    else
      raise ArgumentError, "Unexpected argument config (#{arg.class}): #{arg.inspect}"
    end
  end

  @owner = owner
  @subscription_scope = subscription_scope

  @extensions = EMPTY_ARRAY
  @call_after_define = false
  set_pagination_extensions(connection_extension: NOT_CONFIGURED.equal?(connection_extension) ? self.class.connection_extension : connection_extension)
  # Do this last so we have as much context as possible when initializing them:
  if !extensions.empty?
    self.extensions(extensions)
  end

  if resolver_class && !resolver_class.extensions.empty?
    self.extensions(resolver_class.extensions)
  end

  if !directives.empty?
    directives.each do |(dir_class, options)|
      self.directive(dir_class, **options)
    end
  end

  if !validates.empty?
    self.validates(validates)
  end

  if @definition_block.nil?
    self.extensions.each(&:after_define_apply)
    @call_after_define = true
  end
end