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:

  • arg_name (Symbol) (defaults to: nil)
  • type_expr (defaults to: nil)
  • desc (String) (defaults to: nil)
  • type (Class, Array<Class>) (defaults to: nil)

    Input type; positional argument also accepted

  • name (Symbol) (defaults to: nil)

    positional argument also accepted # @param loads [Class, Array] A GraphQL type to load for the given ID when one is present

  • definition_block (Proc)

    Called with the newly-created GraphQL::Schema::Argument

  • owner (Class)

    Private, used by GraphQL-Ruby during schema definition

  • required (Boolean, :nullable) (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.

  • description (String) (defaults to: nil)
  • default_value (Object) (defaults to: NOT_CONFIGURED)
  • loads (Class, Array<Class>) (defaults to: nil)

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

  • as (Symbol) (defaults to: nil)

    Override the keyword name when passed to a method

  • prepare (Symbol) (defaults to: nil)

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

  • camelize (Boolean) (defaults to: true)

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

  • from_resolver (Boolean) (defaults to: false)

    if true, a Resolver class defined this argument

  • directives (Hash{Class => Hash}) (defaults to: nil)
  • deprecation_reason (String) (defaults to: nil)
  • validates (Hash, nil) (defaults to: nil)

    Options for building validators, if any should be applied

  • replace_null_with_default (Boolean) (defaults to: false)

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

  • comment (String) (defaults to: nil)

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

  • ast_node (GraphQL::Language::Nodes::InputValueDefinition) (defaults to: nil)

    Private, used by GraphQL-Ruby when parsing schema files



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
97
98
99
100
101
102
103
# File 'lib/graphql/schema/argument.rb', line 60

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