class GraphQL::Schema::Argument

Constants

NO_DEFAULT

Attributes

default_value[R]

@return [Object] the value used when the client doesn't provide a value for this argument

description[W]
graphql_name[R]

@return [String] the GraphQL name for this argument, camelized unless `camelize: false` is provided

keyword[R]

@return [Symbol] This argument's name in Ruby keyword arguments

name[R]

@return [String] the GraphQL name for this argument, camelized unless `camelize: false` is provided

owner[R]

@return [GraphQL::Schema::Field, Class] The field or input object this argument belongs to

prepare[R]

@return [Symbol] A method to call to transform this value before sending it to field resolution method

Public Class Methods

new(arg_name = nil, type_expr = nil, desc = nil, required:, type: nil, name: nil, description: nil, default_value: NO_DEFAULT, as: nil, camelize: true, prepare: nil, owner:, &definition_block) click to toggle source

@param arg_name [Symbol] @param type_expr @param desc [String] @param required [Boolean] if true, this argument is non-null; if false, this argument is nullable @param description [String] @param default_value [Object] @param as [Symbol] Override the keyword name when passed to a method @param prepare [Symbol] A method to call to tranform this argument's valuebefore sending it to field resolution @param camelize [Boolean] if true, the name will be camelized when building the schema

# File lib/graphql/schema/argument.rb, line 33
def initialize(arg_name = nil, type_expr = nil, desc = nil, required:, type: nil, name: nil, description: nil, default_value: NO_DEFAULT, as: nil, camelize: true, prepare: nil, owner:, &definition_block)
  arg_name ||= name
  @name = camelize ? Member::BuildType.camelize(arg_name.to_s) : arg_name.to_s
  @type_expr = type_expr || type
  @description = desc || description
  @null = !required
  @default_value = default_value
  @owner = owner
  @as = as
  @keyword = as || Schema::Member::BuildType.underscore(@name).to_sym
  @prepare = prepare

  if definition_block
    if definition_block.arity == 1
      instance_exec(self, &definition_block)
    else
      instance_eval(&definition_block)
    end
  end
end

Public Instance Methods

accessible?(context) click to toggle source
# File lib/graphql/schema/argument.rb, line 77
def accessible?(context)
  true
end
authorized?(obj, ctx) click to toggle source
# File lib/graphql/schema/argument.rb, line 81
def authorized?(obj, ctx)
  true
end
default_value?() click to toggle source

@return [Boolean] True if this argument has a default value

# File lib/graphql/schema/argument.rb, line 58
def default_value?
  @default_value != NO_DEFAULT
end
description(text = nil) click to toggle source

@return [String] Documentation for this argument

# File lib/graphql/schema/argument.rb, line 65
def description(text = nil)
  if text
    @description = text
  else
    @description
  end
end
prepare_value(obj, value) click to toggle source

Apply the {prepare} configuration to `value`, using methods from `obj`. Used by the runtime. @api private

# File lib/graphql/schema/argument.rb, line 107
def prepare_value(obj, value)
  if @prepare.nil?
    value
  elsif @prepare.is_a?(String) || @prepare.is_a?(Symbol)
    obj.public_send(@prepare, value)
  elsif @prepare.respond_to?(:call)
    @prepare.call(value, obj.context)
  else
    raise "Invalid prepare for #{@owner.name}.name: #{@prepare.inspect}"
  end
end
to_graphql() click to toggle source
# File lib/graphql/schema/argument.rb, line 85
def to_graphql
  argument = GraphQL::Argument.new
  argument.name = @name
  argument.type = -> { type }
  argument.description = @description
  argument.metadata[:type_class] = self
  argument.as = @as
  if NO_DEFAULT != @default_value
    argument.default_value = @default_value
  end
  argument
end
type() click to toggle source
# File lib/graphql/schema/argument.rb, line 98
def type
  @type ||= Member::BuildType.parse_type(@type_expr, null: @null)
rescue StandardError => err
  raise ArgumentError, "Couldn't build type for Argument #{@owner.name}.#{name}: #{err.class.name}: #{err.message}", err.backtrace
end
visible?(context) click to toggle source
# File lib/graphql/schema/argument.rb, line 73
def visible?(context)
  true
end