Method: GraphQL::Schema::Argument#initialize

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

#initialize(arg_name = nil, type_expr = nil, desc = nil, required: true, type: nil, name: nil, loads: nil, description: nil, comment: nil, ast_node: nil, default_value: NOT_CONFIGURED, as: nil, from_resolver: false, camelize: true, prepare: nil, owner:, validates: nil, directives: nil, deprecation_reason: nil, replace_null_with_default: false, &definition_block) ⇒ Argument

Returns a new instance of Argument.

Parameters:

  • (defaults to: nil)
  • (defaults to: nil)
  • (defaults to: nil)
  • (defaults to: true)

    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.

  • (defaults to: nil)
  • (defaults to: NOT_CONFIGURED)
  • (defaults to: nil)

    Override the keyword name when passed to a method

  • (defaults to: nil)

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

  • (defaults to: true)

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

  • (defaults to: false)

    if true, a Resolver class defined this argument

  • (defaults to: nil)
  • (defaults to: nil)
  • (defaults to: nil)

    Options for building validators, if any should be applied

  • (defaults to: false)

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



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/graphql/schema/argument.rb', line 53

def initialize(arg_name = nil, type_expr = nil, desc = nil, required: true, type: nil, name: nil, loads: nil, description: nil, comment: nil, ast_node: nil, default_value: NOT_CONFIGURED, as: nil, from_resolver: false, camelize: true, prepare: nil, owner:, validates: nil, directives: nil, deprecation_reason: nil, replace_null_with_default: false, &definition_block)
  arg_name ||= name
  @name = -(camelize ? Member::BuildType.camelize(arg_name.to_s) : arg_name.to_s)
  NameValidator.validate!(@name)
  @type_expr = type_expr || type
  @description = desc || description
  @comment = comment
  @null = required != true
  @default_value = default_value
  if replace_null_with_default
    if !default_value?
      raise ArgumentError, "`replace_null_with_default: true` requires a default value, please provide one with `default_value: ...`"
    end
    @replace_null_with_default = true
  end

  @owner = owner
  @as = as
  @loads = loads
  @keyword = as || (arg_name.is_a?(Symbol) ? arg_name : Schema::Member::BuildType.underscore(@name).to_sym)
  @prepare = prepare
  @ast_node = ast_node
  @from_resolver = from_resolver
  self.deprecation_reason = deprecation_reason

  if directives
    directives.each do |dir_class, dir_options|
      directive(dir_class, **dir_options)
    end
  end

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

  if required == :nullable
    self.owner.validates(required: { argument: arg_name })
  end

  if definition_block
    # `self` will still be self, it will also be the first argument to the block:
    instance_exec(self, &definition_block)
  end
end