Class: GraphQL::Schema::Argument
- Inherits:
-
Object
- Object
- GraphQL::Schema::Argument
- Includes:
- Member::AcceptsDefinition, Member::CachedGraphQLDefinition, Member::HasAstNode, Member::HasPath
- Defined in:
- lib/graphql/schema/argument.rb
Constant Summary collapse
- NO_DEFAULT =
:__no_default__
Instance Attribute Summary collapse
-
#default_value ⇒ Object
readonly
The value used when the client doesn’t provide a value for this argument.
-
#description(text = nil) ⇒ String
Documentation for this argument.
-
#keyword ⇒ Symbol
readonly
This argument’s name in Ruby keyword arguments.
-
#loads ⇒ Class, ...
readonly
If this argument should load an application object, this is the type of object to load.
-
#name ⇒ String
(also: #graphql_name)
readonly
The GraphQL name for this argument, camelized unless
camelize: false
is provided. -
#owner ⇒ GraphQL::Schema::Field, Class
readonly
The field or input object this argument belongs to.
-
#prepare ⇒ Symbol
readonly
A method to call to transform this value before sending it to field resolution method.
-
#type ⇒ Object
Instance Method Summary collapse
-
#accessible?(context) ⇒ Boolean
-
#authorized?(obj, value, ctx) ⇒ Boolean
-
#authorized_as_type?(obj, value, ctx, as_type:) ⇒ Boolean
-
#default_value? ⇒ Boolean
True if this argument has a default value.
-
#from_resolver? ⇒ Boolean
True if a resolver defined this argument.
-
#initialize(arg_name = nil, type_expr = nil, desc = nil, required:, type: nil, name: nil, loads: nil, description: nil, ast_node: nil, default_value: NO_DEFAULT, as: nil, from_resolver: false, camelize: true, prepare: nil, method_access: true, owner:, &definition_block) ⇒ Argument
constructor
A new instance of Argument.
-
#prepare_value(obj, value, context: nil) ⇒ Object
private
Apply the #prepare configuration to
value
, using methods fromobj
. -
#statically_coercible? ⇒ Boolean
-
#to_graphql ⇒ Object
-
#visible?(context) ⇒ Boolean
Methods included from Member::HasAstNode
Methods included from Member::HasPath
Methods included from Member::CachedGraphQLDefinition
#graphql_definition, #initialize_copy, #type_class
Constructor Details
#initialize(arg_name = nil, type_expr = nil, desc = nil, required:, type: nil, name: nil, loads: nil, description: nil, ast_node: nil, default_value: NO_DEFAULT, as: nil, from_resolver: false, camelize: true, prepare: nil, method_access: true, owner:, &definition_block) ⇒ Argument
Returns a new instance of Argument.
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/graphql/schema/argument.rb', line 48 def initialize(arg_name = nil, type_expr = nil, desc = nil, required:, type: nil, name: nil, loads: nil, description: nil, ast_node: nil, default_value: NO_DEFAULT, as: nil, from_resolver: false, camelize: true, prepare: nil, method_access: true, 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 @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 @method_access = method_access 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_value ⇒ Object (readonly)
Returns the value used when the client doesn’t provide a value for this argument.
74 75 76 |
# File 'lib/graphql/schema/argument.rb', line 74 def default_value @default_value end |
#description(text = nil) ⇒ String
Returns Documentation for this argument.
84 85 86 87 88 89 90 |
# File 'lib/graphql/schema/argument.rb', line 84 def description(text = nil) if text @description = text else @description end end |
#keyword ⇒ Symbol (readonly)
Returns This argument’s name in Ruby keyword arguments.
27 28 29 |
# File 'lib/graphql/schema/argument.rb', line 27 def keyword @keyword end |
#loads ⇒ Class, ... (readonly)
Returns If this argument should load an application object, this is the type of object to load.
30 31 32 |
# File 'lib/graphql/schema/argument.rb', line 30 def loads @loads end |
#name ⇒ String (readonly) Also known as: graphql_name
Returns the GraphQL name for this argument, camelized unless camelize: false
is provided.
17 18 19 |
# File 'lib/graphql/schema/argument.rb', line 17 def name @name end |
#owner ⇒ GraphQL::Schema::Field, Class (readonly)
Returns The field or input object this argument belongs to.
21 22 23 |
# File 'lib/graphql/schema/argument.rb', line 21 def owner @owner end |
#prepare ⇒ Symbol (readonly)
Returns A method to call to transform this value before sending it to field resolution method.
24 25 26 |
# File 'lib/graphql/schema/argument.rb', line 24 def prepare @prepare end |
#type ⇒ Object
151 152 153 154 155 |
# File 'lib/graphql/schema/argument.rb', line 151 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.}", err.backtrace end |
Instance Method Details
#accessible?(context) ⇒ Boolean
96 97 98 |
# File 'lib/graphql/schema/argument.rb', line 96 def accessible?(context) true end |
#authorized?(obj, value, ctx) ⇒ Boolean
100 101 102 |
# File 'lib/graphql/schema/argument.rb', line 100 def (obj, value, ctx) (obj, value, ctx, as_type: type) end |
#authorized_as_type?(obj, value, ctx, as_type:) ⇒ Boolean
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/graphql/schema/argument.rb', line 104 def (obj, value, ctx, as_type:) if value.nil? return true end if as_type.kind.non_null? as_type = as_type.of_type end if as_type.kind.list? value.each do |v| if !(obj, v, ctx, as_type: as_type.of_type) return false end end elsif as_type.kind.input_object? as_type.arguments.each do |_name, input_obj_arg| input_obj_arg = input_obj_arg.type_class # TODO: this skips input objects whose values were alread replaced with application objects. # See: https://github.com/rmosolgo/graphql-ruby/issues/2633 if value.respond_to?(:key?) && value.key?(input_obj_arg.keyword) && !input_obj_arg.(obj, value[input_obj_arg.keyword], ctx) return false end end end # None of the early-return conditions were activated, # so this is authorized. true end |
#default_value? ⇒ Boolean
Returns True if this argument has a default value.
77 78 79 |
# File 'lib/graphql/schema/argument.rb', line 77 def default_value? @default_value != NO_DEFAULT end |
#from_resolver? ⇒ Boolean
Returns true if a resolver defined this argument.
33 34 35 |
# File 'lib/graphql/schema/argument.rb', line 33 def from_resolver? @from_resolver end |
#prepare_value(obj, value, context: nil) ⇒ 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.
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 |
# File 'lib/graphql/schema/argument.rb', line 166 def prepare_value(obj, value, context: nil) if value.is_a?(GraphQL::Schema::InputObject) value = value.prepare end if @prepare.nil? value elsif @prepare.is_a?(String) || @prepare.is_a?(Symbol) if obj.nil? # The problem here is, we _used to_ prepare while building variables. # But now we don't have the runtime object there. # # This will have to be called later, when the runtime object _is_ available. value else obj.public_send(@prepare, value) end elsif @prepare.respond_to?(:call) @prepare.call(value, context || obj.context) else raise "Invalid prepare for #{@owner.name}.name: #{@prepare.inspect}" end end |
#statically_coercible? ⇒ Boolean
157 158 159 160 161 |
# File 'lib/graphql/schema/argument.rb', line 157 def statically_coercible? return @statically_coercible if defined?(@statically_coercible) @statically_coercible = !@prepare.is_a?(String) && !@prepare.is_a?(Symbol) end |
#to_graphql ⇒ Object
134 135 136 137 138 139 140 141 142 143 144 145 146 147 |
# File 'lib/graphql/schema/argument.rb', line 134 def to_graphql argument = GraphQL::Argument.new argument.name = @name argument.type = -> { type } argument.description = @description argument.[:type_class] = self argument.as = @as argument.ast_node = ast_node argument.method_access = @method_access if NO_DEFAULT != @default_value argument.default_value = @default_value end argument end |
#visible?(context) ⇒ Boolean
92 93 94 |
# File 'lib/graphql/schema/argument.rb', line 92 def visible?(context) true end |