Class: GraphQL::Argument

Inherits:
Object
  • Object
show all
Includes:
Define::InstanceDefinable
Defined in:
lib/graphql/argument.rb

Overview

Used for defined arguments (Field, InputObjectType)

#name must be a String.

Examples:

defining an argument for a field

GraphQL::Field.define do
  # ...
  argument :favoriteFood, types.String, "Favorite thing to eat", default_value: "pizza"
end

defining an argument for an InputObjectType

GraphQL::InputObjectType.define do
  argument :newName, !types.String
end

defining an argument with a prepare function

GraphQL::Field.define do
  argument :userId, types.ID, prepare: ->(userId) do
    User.find_by(id: userId)
  end
end

returning an ExecutionError from a prepare function

GraphQL::Field.define do
  argument :date do
    type !types.String
    prepare ->(date) do
      return GraphQL::ExecutionError.new("Invalid date format") unless DateValidator.valid?(date)
      Time.zone.parse(date)
    end
  end
end

Defined Under Namespace

Modules: DefaultPrepare

Constant Summary

NO_DEFAULT_VALUE =
Object.new

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Define::InstanceDefinable

#define, #metadata, #redefine

Constructor Details

#initializeArgument

Returns a new instance of Argument

[View source]

51
52
53
# File 'lib/graphql/argument.rb', line 51

def initialize
  @prepare_proc = DefaultPrepare
end

Instance Attribute Details

#asObject

Returns the value of attribute as


40
41
42
# File 'lib/graphql/argument.rb', line 40

def as
  @as
end

#ast_nodeObject

Returns the value of attribute ast_node


41
42
43
# File 'lib/graphql/argument.rb', line 41

def ast_node
  @ast_node
end

#default_valueObject

Returns the value of attribute default_value


39
40
41
# File 'lib/graphql/argument.rb', line 39

def default_value
  @default_value
end

#descriptionObject

Returns the value of attribute description


40
41
42
# File 'lib/graphql/argument.rb', line 40

def description
  @description
end

#nameString Also known as: graphql_name

Returns The name of this argument on its Field or InputObjectType

Returns:


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

def name
  @name
end

Class Method Details

.deep_stringify(val) ⇒ 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.

[View source]

133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/graphql/argument.rb', line 133

def self.deep_stringify(val)
  case val
  when Array
    val.map { |v| deep_stringify(v) }
  when Hash
    new_val = {}
    val.each do |k, v|
      new_val[k.to_s] = deep_stringify(v)
    end
    new_val
  else
    val
  end
end

.from_dsl(name, type_or_argument = nil, description = nil, default_value: NO_DEFAULT_VALUE, as: nil, prepare: DefaultPrepare, **kwargs, &block) ⇒ 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.

[View source]

107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/graphql/argument.rb', line 107

def self.from_dsl(name, type_or_argument = nil, description = nil, default_value: NO_DEFAULT_VALUE, as: nil, prepare: DefaultPrepare, **kwargs, &block)
  name_s = name.to_s

  # Move some positional args into keywords if they're present
  description && kwargs[:description] ||= description
  kwargs[:name] ||= name_s
  kwargs[:default_value] ||= default_value
  kwargs[:as] ||= as

  unless prepare == DefaultPrepare
    kwargs[:prepare] ||= prepare
  end

  if !type_or_argument.nil? && !type_or_argument.is_a?(GraphQL::Argument)
    # Maybe a string, proc or BaseType
    kwargs[:type] = type_or_argument
  end

  if type_or_argument.is_a?(GraphQL::Argument)
    type_or_argument.redefine(kwargs, &block)
  else
    GraphQL::Argument.define(kwargs, &block)
  end
end

Instance Method Details

#default_value?Boolean

Returns:

  • (Boolean)
[View source]

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

def default_value?
  !!@has_default_value
end

#expose_asString

Returns The name of this argument inside resolve functions

Returns:

  • (String)

    The name of this argument inside resolve functions

[View source]

88
89
90
# File 'lib/graphql/argument.rb', line 88

def expose_as
  @expose_as ||= (@as || @name).to_s
end

#initialize_copy(other) ⇒ Object

[View source]

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

def initialize_copy(other)
  @expose_as = nil
end

#prepare(value, ctx) ⇒ Object

Returns The prepared value for this argument or value itself if no prepare function exists.

Parameters:

Returns:

  • (Object)

    The prepared value for this argument or value itself if no prepare function exists.

[View source]

95
96
97
# File 'lib/graphql/argument.rb', line 95

def prepare(value, ctx)
  @prepare_proc.call(value, ctx)
end

#prepare=(prepare_proc) ⇒ Object

Assign a prepare function to prepare this argument’s value before resolve functions are called.

Parameters:

  • prepare_proc (#<call(value, ctx))

    ]

[View source]

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

def prepare=(prepare_proc)
  @prepare_proc = BackwardsCompatibility.wrap_arity(prepare_proc, from: 1, to: 2, name: "Argument#prepare(value, ctx)")
end

#typeGraphQL::BaseType

Returns the input type for this argument

Returns:

[View source]

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

def type
  @clean_type ||= GraphQL::BaseType.resolve_related_type(@dirty_type)
end

#type=(new_input_type) ⇒ Object

Parameters:

  • new_input_type (GraphQL::BaseType, Proc)

    Assign a new input type for this argument (if it’s a proc, it will be called after schema initialization)

[View source]

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

def type=(new_input_type)
  @clean_type = nil
  @dirty_type = new_input_type
end