Class: GraphQL::Schema::Argument

Inherits:
Object
  • Object
show all
Includes:
Member::AcceptsDefinition, Member::CachedGraphQLDefinition, Member::HasPath
Defined in:
lib/graphql/schema/argument.rb

Constant Summary collapse

NO_DEFAULT =
:__no_default__

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Member::HasPath

#path

Methods included from Member::CachedGraphQLDefinition

#graphql_definition, #initialize_copy

Constructor Details

#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) ⇒ Argument

Returns a new instance of Argument

Parameters:

  • arg_name (Symbol) (defaults to: nil)
  • type_expr (defaults to: nil)
  • desc (String) (defaults to: nil)
  • required (Boolean)

    if true, this argument is non-null; if false, this argument is nullable

  • description (String)
  • default_value (Object)
  • 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



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# 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_str = camelize ? Member::BuildType.camelize(arg_name.to_s) : arg_name.to_s
  @name = name_str.freeze
  @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

Instance Attribute Details

#default_valueObject (readonly)

Returns the value used when the client doesn’t provide a value for this argument

Returns:

  • (Object)

    the value used when the client doesn’t provide a value for this argument



56
57
58
# File 'lib/graphql/schema/argument.rb', line 56

def default_value
  @default_value
end

#description(text = nil) ⇒ String

Returns Documentation for this argument

Returns:

  • (String)

    Documentation for this argument



66
67
68
69
70
71
72
# File 'lib/graphql/schema/argument.rb', line 66

def description(text = nil)
  if text
    @description = text
  else
    @description
  end
end

#keywordSymbol (readonly)

Returns This argument’s name in Ruby keyword arguments

Returns:

  • (Symbol)

    This argument’s name in Ruby keyword arguments



22
23
24
# File 'lib/graphql/schema/argument.rb', line 22

def keyword
  @keyword
end

#nameString (readonly) Also known as: graphql_name

Returns the GraphQL name for this argument, camelized unless camelize: false is provided

Returns:

  • (String)

    the GraphQL name for this argument, camelized unless camelize: false is provided



12
13
14
# File 'lib/graphql/schema/argument.rb', line 12

def name
  @name
end

#ownerGraphQL::Schema::Field, Class (readonly)

Returns The field or input object this argument belongs to

Returns:



16
17
18
# File 'lib/graphql/schema/argument.rb', line 16

def owner
  @owner
end

#prepareSymbol (readonly)

Returns A method to call to transform this value before sending it to field resolution method

Returns:

  • (Symbol)

    A method to call to transform this value before sending it to field resolution method



19
20
21
# File 'lib/graphql/schema/argument.rb', line 19

def prepare
  @prepare
end

Instance Method Details

#accessible?(context) ⇒ Boolean

Returns:

  • (Boolean)


78
79
80
# File 'lib/graphql/schema/argument.rb', line 78

def accessible?(context)
  true
end

#authorized?(obj, ctx) ⇒ Boolean

Returns:

  • (Boolean)


82
83
84
# File 'lib/graphql/schema/argument.rb', line 82

def authorized?(obj, ctx)
  true
end

#default_value?Boolean

Returns True if this argument has a default value

Returns:

  • (Boolean)

    True if this argument has a default value



59
60
61
# File 'lib/graphql/schema/argument.rb', line 59

def default_value?
  @default_value != NO_DEFAULT
end

#prepare_value(obj, value) ⇒ Object

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.

Apply the #prepare configuration to value, using methods from obj. Used by the runtime.



108
109
110
111
112
113
114
115
116
117
118
# File 'lib/graphql/schema/argument.rb', line 108

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_graphqlObject



86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/graphql/schema/argument.rb', line 86

def to_graphql
  argument = GraphQL::Argument.new
  argument.name = @name
  argument.type = -> { type }
  argument.description = @description
  argument.[:type_class] = self
  argument.as = @as
  if NO_DEFAULT != @default_value
    argument.default_value = @default_value
  end
  argument
end

#typeObject



99
100
101
102
103
# File 'lib/graphql/schema/argument.rb', line 99

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) ⇒ Boolean

Returns:

  • (Boolean)


74
75
76
# File 'lib/graphql/schema/argument.rb', line 74

def visible?(context)
  true
end