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 collapse

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.



48
49
50
# File 'lib/graphql/argument.rb', line 48

def initialize
  @prepare_proc = DefaultPrepare
end

Instance Attribute Details

#asObject

Returns the value of attribute as.



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

def as
  @as
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.



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

def description
  @description
end

#nameString

Returns The name of this argument on its Field or InputObjectType.

Returns:



65
66
67
# File 'lib/graphql/argument.rb', line 65

def name
  @name
end

#typeGraphQL::BaseType

Returns the input type for this argument.

Returns:

  • the input type for this argument



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

def type
  @type
end

Class Method Details

.from_dsl(name, type = 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.

API:

  • private



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/graphql/argument.rb', line 99

def self.from_dsl(name, type = nil, description = nil, default_value: NO_DEFAULT_VALUE, as: nil, prepare: DefaultPrepare, **kwargs, &block)
  argument = if block_given?
    GraphQL::Argument.define(&block)
  else
    GraphQL::Argument.define(**kwargs)
  end

  argument.name = name.to_s
  type && argument.type = type
  description && argument.description = description
  if default_value != NO_DEFAULT_VALUE
    argument.default_value = default_value
  end
  as && argument.as = as
  if prepare != DefaultPrepare
    argument.prepare = prepare
  end

  argument
end

Instance Method Details

#default_value?Boolean

Returns:



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

def default_value?
  !!@has_default_value
end

#expose_asString

Returns The name of this argument inside resolve functions.

Returns:

  • The name of this argument inside resolve functions



80
81
82
# File 'lib/graphql/argument.rb', line 80

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

#initialize_copy(other) ⇒ Object



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

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:

  • The incoming value from variables or query string literal

Returns:

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



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

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:

  • ]



93
94
95
# File 'lib/graphql/argument.rb', line 93

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