Method: GraphQL::Schema::Member::HasArguments#argument

Defined in:
lib/graphql/schema/member/has_arguments.rb

#argument(arg_name = nil, type_expr = nil, desc = nil, **kwargs, &definition_block) ⇒ GraphQL::Schema::Argument

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns An instance of #argument_class created from these arguments.

Parameters:

  • arg_name (Symbol) (defaults to: nil)

    The underscore-cased name of this argument, name: keyword also accepted

  • type_expr (defaults to: nil)

    The GraphQL type of this argument; type: keyword also accepted

  • desc (String) (defaults to: nil)

    Argument description, description: keyword also accepted

  • definition_block (Proc)

    Called with the newly-created Argument

  • kwargs (Hash)

    Keywords for defining an argument. Any keywords not documented here must be handled by your base Argument class.

Options Hash (**kwargs):

  • :required (Boolean, :nullable)

    if true, this argument is non-null; if false, this argument is nullable. If :nullable, then the argument must be provided, though it may be null.

  • :description (String)

    Positional argument also accepted

  • :type (Class, Array<Class>)

    Input type; positional argument also accepted

  • :name (Symbol)

    positional argument also accepted

  • :default_value (Object)
  • :loads (Class, Array<Class>)

    A GraphQL type to load for the given ID when one is present

  • :as (Symbol)

    Override the keyword name when passed to a method

  • :prepare (Symbol)

    A method to call to transform this argument's valuebefore sending it to field resolution

  • :camelize (Boolean)

    if true, the name will be camelized when building the schema

  • :from_resolver (Boolean)

    if true, a Resolver class defined this argument

  • :directives (Hash{Class => Hash})
  • :deprecation_reason (String)
  • :comment (String)

    Private, used by GraphQL-Ruby when parsing GraphQL schema files

  • :ast_node (GraphQL::Language::Nodes::InputValueDefinition)

    Private, used by GraphQL-Ruby when parsing schema files

  • :validates (Hash, nil)

    Options for building validators, if any should be applied

  • :replace_null_with_default (Boolean)

    if true, incoming values of null will be replaced with the configured default_value

Returns:



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/graphql/schema/member/has_arguments.rb', line 39

def argument(arg_name = nil, type_expr = nil, desc = nil, **kwargs, &definition_block)
  if kwargs[:loads]
    loads_name = arg_name || kwargs[:name]
    loads_name_as_string = loads_name.to_s

    inferred_arg_name = case loads_name_as_string
    when /_id$/
      loads_name_as_string.sub(/_id$/, "").to_sym
    when /_ids$/
      loads_name_as_string.sub(/_ids$/, "")
        .sub(/([^s])$/, "\\1s")
        .to_sym
    else
      loads_name
    end

    kwargs[:as] ||= inferred_arg_name
  end
  kwargs[:owner] = self
  arg_defn = self.argument_class.new(
    arg_name, type_expr, desc,
    **kwargs,
    &definition_block
  )
  add_argument(arg_defn)
  arg_defn
end