forked from rmosolgo/graphql-ruby
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathargument.rb
More file actions
333 lines (291 loc) · 12 KB
/
argument.rb
File metadata and controls
333 lines (291 loc) · 12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
# frozen_string_literal: true
module GraphQL
class Schema
class Argument
if !String.method_defined?(:-@)
using GraphQL::StringDedupBackport
end
include GraphQL::Schema::Member::CachedGraphQLDefinition
include GraphQL::Schema::Member::AcceptsDefinition
include GraphQL::Schema::Member::HasPath
include GraphQL::Schema::Member::HasAstNode
include GraphQL::Schema::Member::HasDirectives
include GraphQL::Schema::Member::HasDeprecationReason
include GraphQL::Schema::Member::HasValidators
include GraphQL::Schema::FindInheritedValue::EmptyObjects
NO_DEFAULT = :__no_default__
# @return [String] the GraphQL name for this argument, camelized unless `camelize: false` is provided
attr_reader :name
alias :graphql_name :name
# @return [GraphQL::Schema::Field, Class] The field or input object this argument belongs to
attr_reader :owner
# @return [Symbol] A method to call to transform this value before sending it to field resolution method
attr_reader :prepare
# @return [Symbol] This argument's name in Ruby keyword arguments
attr_reader :keyword
# @return [Class, Module, nil] If this argument should load an application object, this is the type of object to load
attr_reader :loads
# @return [Boolean] true if a resolver defined this argument
def from_resolver?
@from_resolver
end
# @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 transform this argument's valuebefore sending it to field resolution
# @param camelize [Boolean] if true, the name will be camelized when building the schema
# @param from_resolver [Boolean] if true, a Resolver class defined this argument
# @param method_access [Boolean] If false, don't build method access on legacy {Query::Arguments} instances.
# @param directives [Hash{Class => Hash}]
# @param deprecation_reason [String]
# @param validates [Hash, nil] Options for building validators, if any should be applied
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:, validates: nil, directives: nil, deprecation_reason: nil, &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
self.deprecation_reason = deprecation_reason
if directives
directives.each do |dir_class, dir_options|
directive(dir_class, **dir_options)
end
end
self.validates(validates)
if definition_block
if definition_block.arity == 1
instance_exec(self, &definition_block)
else
instance_eval(&definition_block)
end
end
end
# @return [Object] the value used when the client doesn't provide a value for this argument
attr_reader :default_value
# @return [Boolean] True if this argument has a default value
def default_value?
@default_value != NO_DEFAULT
end
attr_writer :description
# @return [String] Documentation for this argument
def description(text = nil)
if text
@description = text
else
@description
end
end
# @return [String] Deprecation reason for this argument
def deprecation_reason(text = nil)
if text
self.deprecation_reason = text
else
super()
end
end
def deprecation_reason=(new_reason)
validate_deprecated_or_optional(null: @null, deprecation_reason: new_reason)
super
end
def visible?(context)
true
end
def accessible?(context)
true
end
def authorized?(obj, value, ctx)
authorized_as_type?(obj, value, ctx, as_type: type)
end
def authorized_as_type?(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 !authorized_as_type?(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.authorized?(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
def to_graphql
argument = GraphQL::Argument.new
argument.name = @name
argument.type = -> { type }
argument.description = @description
argument.metadata[: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
if self.deprecation_reason
argument.deprecation_reason = self.deprecation_reason
end
argument
end
def type=(new_type)
validate_input_type(new_type)
# This isn't true for LateBoundTypes, but we can assume those will
# be updated via this codepath later in schema setup.
if new_type.respond_to?(:non_null?)
validate_deprecated_or_optional(null: !new_type.non_null?, deprecation_reason: deprecation_reason)
end
@type = new_type
end
def type
@type ||= begin
parsed_type = begin
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
# Use the setter method to get validations
self.type = parsed_type
end
end
def statically_coercible?
return @statically_coercible if defined?(@statically_coercible)
@statically_coercible = !@prepare.is_a?(String) && !@prepare.is_a?(Symbol)
end
# Apply the {prepare} configuration to `value`, using methods from `obj`.
# Used by the runtime.
# @api private
def prepare_value(obj, value, context: nil)
if value.is_a?(GraphQL::Schema::InputObject)
value = value.prepare
end
Schema::Validator.validate!(validators, obj, context, value)
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
# @api private
def coerce_into_values(parent_object, values, context, argument_values)
arg_name = graphql_name
arg_key = keyword
has_value = false
default_used = false
if values.key?(arg_name)
has_value = true
value = values[arg_name]
elsif values.key?(arg_key)
has_value = true
value = values[arg_key]
elsif default_value?
has_value = true
value = default_value
default_used = true
end
if has_value
loaded_value = nil
coerced_value = context.schema.error_handler.with_error_handling(context) do
type.coerce_input(value, context)
end
# TODO this should probably be inside after_lazy
if loads && !from_resolver?
loaded_value = if type.list?
loaded_values = coerced_value.map { |val| owner.load_application_object(self, loads, val, context) }
context.schema.after_any_lazies(loaded_values) { |result| result }
else
context.query.with_error_handling do
owner.load_application_object(self, loads, coerced_value, context)
end
end
end
coerced_value = if loaded_value
loaded_value
else
coerced_value
end
# If this isn't lazy, then the block returns eagerly and assigns the result here
# If it _is_ lazy, then we write the lazy to the hash, then update it later
argument_values[arg_key] = context.schema.after_lazy(coerced_value) do |coerced_value|
owner.validate_directive_argument(self, coerced_value)
prepared_value = context.schema.error_handler.with_error_handling(context) do
prepare_value(parent_object, coerced_value, context: context)
end
# TODO code smell to access such a deeply-nested constant in a distant module
argument_values[arg_key] = GraphQL::Execution::Interpreter::ArgumentValue.new(
value: prepared_value,
definition: self,
default_used: default_used,
)
end
else
# has_value is false
owner.validate_directive_argument(self, nil)
end
end
private
def validate_input_type(input_type, wrapped: false)
if input_type.is_a?(String) || input_type.is_a?(GraphQL::Schema::LateBoundType)
# Do nothing; assume this will be validated later
false
elsif input_type.kind.non_null? || input_type.kind.list?
type_ready = validate_input_type(input_type.unwrap, wrapped: true)
validate_default_value(default_value, input_type) if default_value? && type_ready && !wrapped
type_ready
elsif !input_type.kind.input?
raise ArgumentError, "Invalid input type for #{path}: #{input_type.graphql_name}. Must be scalar, enum, or input object, not #{input_type.kind.name}."
else
# It's an input type, the type itself is OK but make sure the default conforms
validate_default_value(default_value, input_type) if default_value? && !wrapped
true
end
end
def validate_default_value(default_value, input_type)
default_value = input_type.coerce_isolated_result(default_value) unless default_value.nil?
raise "BOOM" unless input_type.valid_isolated_input?(default_value)
end
def validate_deprecated_or_optional(null:, deprecation_reason:)
if deprecation_reason && !null
raise ArgumentError, "Required arguments cannot be deprecated: #{path}."
end
end
end
end
end