graphql 1.12.16 → 1.12.20

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of graphql might be problematic. Click here for more details.

Files changed (48) hide show
  1. checksums.yaml +4 -4
  2. data/lib/generators/graphql/relay.rb +19 -11
  3. data/lib/generators/graphql/templates/schema.erb +13 -1
  4. data/lib/graphql/analysis/ast/field_usage.rb +1 -1
  5. data/lib/graphql/dataloader/source.rb +50 -2
  6. data/lib/graphql/dataloader.rb +39 -16
  7. data/lib/graphql/define/instance_definable.rb +1 -1
  8. data/lib/graphql/deprecated_dsl.rb +11 -3
  9. data/lib/graphql/deprecation.rb +1 -5
  10. data/lib/graphql/execution/interpreter/runtime.rb +10 -6
  11. data/lib/graphql/integer_encoding_error.rb +18 -2
  12. data/lib/graphql/introspection/input_value_type.rb +6 -0
  13. data/lib/graphql/pagination/connections.rb +35 -16
  14. data/lib/graphql/query/validation_pipeline.rb +1 -1
  15. data/lib/graphql/query.rb +4 -0
  16. data/lib/graphql/schema/argument.rb +71 -28
  17. data/lib/graphql/schema/field.rb +14 -4
  18. data/lib/graphql/schema/input_object.rb +5 -9
  19. data/lib/graphql/schema/member/has_arguments.rb +90 -44
  20. data/lib/graphql/schema/resolver.rb +24 -59
  21. data/lib/graphql/schema/subscription.rb +6 -8
  22. data/lib/graphql/schema/validator/allow_blank_validator.rb +29 -0
  23. data/lib/graphql/schema/validator/allow_null_validator.rb +26 -0
  24. data/lib/graphql/schema/validator/exclusion_validator.rb +3 -1
  25. data/lib/graphql/schema/validator/format_validator.rb +2 -1
  26. data/lib/graphql/schema/validator/inclusion_validator.rb +3 -1
  27. data/lib/graphql/schema/validator/length_validator.rb +5 -3
  28. data/lib/graphql/schema/validator/numericality_validator.rb +12 -2
  29. data/lib/graphql/schema/validator.rb +36 -25
  30. data/lib/graphql/schema.rb +18 -5
  31. data/lib/graphql/static_validation/base_visitor.rb +3 -0
  32. data/lib/graphql/static_validation/error.rb +3 -1
  33. data/lib/graphql/static_validation/rules/fields_will_merge.rb +40 -21
  34. data/lib/graphql/static_validation/rules/fields_will_merge_error.rb +25 -4
  35. data/lib/graphql/static_validation/rules/fragments_are_finite.rb +2 -2
  36. data/lib/graphql/static_validation/validation_context.rb +8 -2
  37. data/lib/graphql/static_validation/validator.rb +15 -12
  38. data/lib/graphql/string_encoding_error.rb +13 -3
  39. data/lib/graphql/subscriptions/action_cable_subscriptions.rb +7 -1
  40. data/lib/graphql/subscriptions/event.rb +47 -2
  41. data/lib/graphql/subscriptions/serialize.rb +1 -1
  42. data/lib/graphql/tracing/appsignal_tracing.rb +15 -0
  43. data/lib/graphql/types/int.rb +1 -1
  44. data/lib/graphql/types/string.rb +1 -1
  45. data/lib/graphql/unauthorized_error.rb +1 -1
  46. data/lib/graphql/version.rb +1 -1
  47. data/readme.md +1 -1
  48. metadata +5 -3
@@ -122,6 +122,9 @@ module GraphQL
122
122
  else
123
123
  kwargs[:type] = type
124
124
  end
125
+ if type.is_a?(Class) && type < GraphQL::Schema::Mutation
126
+ raise ArgumentError, "Use `field #{name.inspect}, mutation: Mutation, ...` to provide a mutation to this field instead"
127
+ end
125
128
  end
126
129
  new(**kwargs, &block)
127
130
  end
@@ -510,6 +513,7 @@ module GraphQL
510
513
  field_defn
511
514
  end
512
515
 
516
+ class MissingReturnTypeError < GraphQL::Error; end
513
517
  attr_writer :type
514
518
 
515
519
  def type
@@ -517,14 +521,21 @@ module GraphQL
517
521
  Member::BuildType.parse_type(@function.type, null: false)
518
522
  elsif @field
519
523
  Member::BuildType.parse_type(@field.type, null: false)
524
+ elsif @return_type_expr.nil?
525
+ # Not enough info to determine type
526
+ message = "Can't determine the return type for #{self.path}"
527
+ if @resolver_class
528
+ message += " (it has `resolver: #{@resolver_class}`, consider configuration a `type ...` for that class)"
529
+ end
530
+ raise MissingReturnTypeError, message
520
531
  else
521
532
  Member::BuildType.parse_type(@return_type_expr, null: @return_type_null)
522
533
  end
523
- rescue GraphQL::Schema::InvalidDocumentError => err
534
+ rescue GraphQL::Schema::InvalidDocumentError, MissingReturnTypeError => err
524
535
  # Let this propagate up
525
536
  raise err
526
537
  rescue StandardError => err
527
- raise ArgumentError, "Failed to build return type for #{@owner.graphql_name}.#{name} from #{@return_type_expr.inspect}: (#{err.class}) #{err.message}", err.backtrace
538
+ raise MissingReturnTypeError, "Failed to build return type for #{@owner.graphql_name}.#{name} from #{@return_type_expr.inspect}: (#{err.class}) #{err.message}", err.backtrace
528
539
  end
529
540
 
530
541
  def visible?(context)
@@ -608,8 +619,7 @@ module GraphQL
608
619
  if is_authorized
609
620
  public_send_field(object, args, ctx)
610
621
  else
611
- err = GraphQL::UnauthorizedFieldError.new(object: application_object, type: object.class, context: ctx, field: self)
612
- ctx.schema.unauthorized_field(err)
622
+ raise GraphQL::UnauthorizedFieldError.new(object: application_object, type: object.class, context: ctx, field: self)
613
623
  end
614
624
  end
615
625
  rescue GraphQL::UnauthorizedFieldError => err
@@ -40,11 +40,7 @@ module GraphQL
40
40
  # With the interpreter, it's done during `coerce_arguments`
41
41
  if loads && !arg_defn.from_resolver? && !context.interpreter?
42
42
  value = @ruby_style_hash[ruby_kwargs_key]
43
- loaded_value = if arg_defn.type.list?
44
- value.map { |val| load_application_object(arg_defn, loads, val, context) }
45
- else
46
- load_application_object(arg_defn, loads, value, context)
47
- end
43
+ loaded_value = arg_defn.load_and_authorize_value(self, value, context)
48
44
  maybe_lazies << context.schema.after_lazy(loaded_value) do |loaded_value|
49
45
  overwrite_argument(ruby_kwargs_key, loaded_value)
50
46
  end
@@ -71,11 +67,11 @@ module GraphQL
71
67
  end
72
68
 
73
69
  def prepare
74
- if context
75
- context.schema.after_any_lazies(@maybe_lazies) do
76
- object = context[:current_object]
70
+ if @context
71
+ @context.schema.after_any_lazies(@maybe_lazies) do
72
+ object = @context[:current_object]
77
73
  # Pass this object's class with `as` so that messages are rendered correctly from inherited validators
78
- Schema::Validator.validate!(self.class.validators, object, context, @ruby_style_hash, as: self.class)
74
+ Schema::Validator.validate!(self.class.validators, object, @context, @ruby_style_hash, as: self.class)
79
75
  self
80
76
  end
81
77
  else
@@ -37,6 +37,40 @@ module GraphQL
37
37
  end
38
38
  arg_defn = self.argument_class.new(*args, **kwargs, &block)
39
39
  add_argument(arg_defn)
40
+
41
+ if self.is_a?(Class) && !method_defined?(:"load_#{arg_defn.keyword}")
42
+ method_owner = if self < GraphQL::Schema::InputObject || self < GraphQL::Schema::Directive
43
+ "self."
44
+ elsif self < GraphQL::Schema::Resolver
45
+ ""
46
+ else
47
+ raise "Unexpected argument owner: #{self}"
48
+ end
49
+ if loads && arg_defn.type.list?
50
+ class_eval <<-RUBY, __FILE__, __LINE__ + 1
51
+ def #{method_owner}load_#{arg_defn.keyword}(values, context = nil)
52
+ argument = get_argument("#{arg_defn.graphql_name}")
53
+ (context || self.context).schema.after_lazy(values) do |values2|
54
+ GraphQL::Execution::Lazy.all(values2.map { |value| load_application_object(argument, value, context || self.context) })
55
+ end
56
+ end
57
+ RUBY
58
+ elsif loads
59
+ class_eval <<-RUBY, __FILE__, __LINE__ + 1
60
+ def #{method_owner}load_#{arg_defn.keyword}(value, context = nil)
61
+ argument = get_argument("#{arg_defn.graphql_name}")
62
+ load_application_object(argument, value, context || self.context)
63
+ end
64
+ RUBY
65
+ else
66
+ class_eval <<-RUBY, __FILE__, __LINE__ + 1
67
+ def #{method_owner}load_#{arg_defn.keyword}(value, _context = nil)
68
+ value
69
+ end
70
+ RUBY
71
+ end
72
+ end
73
+ arg_defn
40
74
  end
41
75
 
42
76
  # Register this argument with the class.
@@ -94,54 +128,52 @@ module GraphQL
94
128
  arg_defns = self.arguments
95
129
  total_args_count = arg_defns.size
96
130
 
97
- if total_args_count == 0
98
- final_args = GraphQL::Execution::Interpreter::Arguments::EMPTY
99
- if block_given?
100
- block.call(final_args)
101
- nil
131
+ finished_args = nil
132
+ prepare_finished_args = -> {
133
+ if total_args_count == 0
134
+ finished_args = GraphQL::Execution::Interpreter::Arguments::EMPTY
135
+ if block_given?
136
+ block.call(finished_args)
137
+ end
102
138
  else
103
- final_args
104
- end
105
- else
106
- finished_args = nil
107
- argument_values = {}
108
- resolved_args_count = 0
109
- raised_error = false
110
- arg_defns.each do |arg_name, arg_defn|
111
- context.dataloader.append_job do
112
- begin
113
- arg_defn.coerce_into_values(parent_object, values, context, argument_values)
114
- rescue GraphQL::ExecutionError, GraphQL::UnauthorizedError => err
115
- raised_error = true
116
- if block_given?
117
- block.call(err)
118
- else
139
+ argument_values = {}
140
+ resolved_args_count = 0
141
+ raised_error = false
142
+ arg_defns.each do |arg_name, arg_defn|
143
+ context.dataloader.append_job do
144
+ begin
145
+ arg_defn.coerce_into_values(parent_object, values, context, argument_values)
146
+ rescue GraphQL::ExecutionError, GraphQL::UnauthorizedError => err
147
+ raised_error = true
119
148
  finished_args = err
149
+ if block_given?
150
+ block.call(finished_args)
151
+ end
120
152
  end
121
- end
122
-
123
- resolved_args_count += 1
124
- if resolved_args_count == total_args_count && !raised_error
125
- finished_args = context.schema.after_any_lazies(argument_values.values) {
126
- GraphQL::Execution::Interpreter::Arguments.new(
127
- argument_values: argument_values,
128
- )
129
- }
130
153
 
131
- if block_given?
132
- block.call(finished_args)
154
+ resolved_args_count += 1
155
+ if resolved_args_count == total_args_count && !raised_error
156
+ finished_args = context.schema.after_any_lazies(argument_values.values) {
157
+ GraphQL::Execution::Interpreter::Arguments.new(
158
+ argument_values: argument_values,
159
+ )
160
+ }
161
+ if block_given?
162
+ block.call(finished_args)
163
+ end
133
164
  end
134
165
  end
135
166
  end
136
167
  end
168
+ }
137
169
 
138
- if block_given?
139
- nil
140
- else
141
- # This API returns eagerly, gotta run it now
142
- context.dataloader.run
143
- finished_args
144
- end
170
+ if block_given?
171
+ prepare_finished_args.call
172
+ nil
173
+ else
174
+ # This API returns eagerly, gotta run it now
175
+ context.dataloader.run_isolated(&prepare_finished_args)
176
+ finished_args
145
177
  end
146
178
  end
147
179
 
@@ -186,12 +218,20 @@ module GraphQL
186
218
  context.schema.object_from_id(id, context)
187
219
  end
188
220
 
189
- def load_application_object(argument, lookup_as_type, id, context)
221
+ def load_application_object(argument, id, context)
190
222
  # See if any object can be found for this ID
191
223
  if id.nil?
192
224
  return nil
193
225
  end
194
- loaded_application_object = object_from_id(lookup_as_type, id, context)
226
+ object_from_id(argument.loads, id, context)
227
+ end
228
+
229
+ def load_and_authorize_application_object(argument, id, context)
230
+ loaded_application_object = load_application_object(argument, id, context)
231
+ authorize_application_object(argument, id, context, loaded_application_object)
232
+ end
233
+
234
+ def authorize_application_object(argument, id, context, loaded_application_object)
195
235
  context.schema.after_lazy(loaded_application_object) do |application_object|
196
236
  if application_object.nil?
197
237
  err = GraphQL::LoadApplicationObjectFailedError.new(argument: argument, id: id, object: application_object)
@@ -199,9 +239,9 @@ module GraphQL
199
239
  end
200
240
  # Double-check that the located object is actually of this type
201
241
  # (Don't want to allow arbitrary access to objects this way)
202
- resolved_application_object_type = context.schema.resolve_type(lookup_as_type, application_object, context)
242
+ resolved_application_object_type = context.schema.resolve_type(argument.loads, application_object, context)
203
243
  context.schema.after_lazy(resolved_application_object_type) do |application_object_type|
204
- possible_object_types = context.warden.possible_types(lookup_as_type)
244
+ possible_object_types = context.warden.possible_types(argument.loads)
205
245
  if !possible_object_types.include?(application_object_type)
206
246
  err = GraphQL::LoadApplicationObjectFailedError.new(argument: argument, id: id, object: application_object)
207
247
  load_application_object_failed(err)
@@ -214,11 +254,17 @@ module GraphQL
214
254
  if authed
215
255
  application_object
216
256
  else
217
- raise GraphQL::UnauthorizedError.new(
257
+ err = GraphQL::UnauthorizedError.new(
218
258
  object: application_object,
219
259
  type: class_based_type,
220
260
  context: context,
221
261
  )
262
+ if self.respond_to?(:unauthorized_object)
263
+ err.set_backtrace(caller)
264
+ unauthorized_object(err)
265
+ else
266
+ raise err
267
+ end
222
268
  end
223
269
  end
224
270
  else
@@ -28,7 +28,7 @@ module GraphQL
28
28
  include Schema::Member::HasPath
29
29
  extend Schema::Member::HasPath
30
30
 
31
- # @param object [Object] the initialize object, pass to {Query.initialize} as `root_value`
31
+ # @param object [Object] The application object that this field is being resolved on
32
32
  # @param context [GraphQL::Query::Context]
33
33
  # @param field [GraphQL::Schema::Field]
34
34
  def initialize(object:, context:, field:)
@@ -40,7 +40,6 @@ module GraphQL
40
40
  self.class.arguments.each do |name, arg|
41
41
  @arguments_by_keyword[arg.keyword] = arg
42
42
  end
43
- @arguments_loads_as_type = self.class.arguments_loads_as_type
44
43
  @prepared_arguments = nil
45
44
  end
46
45
 
@@ -110,7 +109,7 @@ module GraphQL
110
109
  public_send(self.class.resolve_method)
111
110
  end
112
111
  else
113
- nil
112
+ raise GraphQL::UnauthorizedFieldError.new(context: context, object: object, type: field.owner, field: field)
114
113
  end
115
114
  end
116
115
  end
@@ -161,6 +160,16 @@ module GraphQL
161
160
  end
162
161
  end
163
162
 
163
+ # Called when an object loaded by `loads:` fails the `.authorized?` check for its resolved GraphQL object type.
164
+ #
165
+ # By default, the error is re-raised and passed along to {{Schema.unauthorized_object}}.
166
+ #
167
+ # Any value returned here will be used _instead of_ of the loaded object.
168
+ # @param err [GraphQL::UnauthorizedError]
169
+ def unauthorized_object(err)
170
+ raise err
171
+ end
172
+
164
173
  private
165
174
 
166
175
  def load_arguments(args)
@@ -170,18 +179,14 @@ module GraphQL
170
179
  args.each do |key, value|
171
180
  arg_defn = @arguments_by_keyword[key]
172
181
  if arg_defn
173
- if value.nil?
174
- prepared_args[key] = value
175
- else
176
- prepped_value = prepared_args[key] = load_argument(key, value)
177
- if context.schema.lazy?(prepped_value)
178
- prepare_lazies << context.schema.after_lazy(prepped_value) do |finished_prepped_value|
179
- prepared_args[key] = finished_prepped_value
180
- end
182
+ prepped_value = prepared_args[key] = arg_defn.load_and_authorize_value(self, value, context)
183
+ if context.schema.lazy?(prepped_value)
184
+ prepare_lazies << context.schema.after_lazy(prepped_value) do |finished_prepped_value|
185
+ prepared_args[key] = finished_prepped_value
181
186
  end
182
187
  end
183
188
  else
184
- # These are `extras: [...]`
189
+ # these are `extras:`
185
190
  prepared_args[key] = value
186
191
  end
187
192
  end
@@ -194,8 +199,8 @@ module GraphQL
194
199
  end
195
200
  end
196
201
 
197
- def load_argument(name, value)
198
- public_send("load_#{name}", value)
202
+ def get_argument(name)
203
+ self.class.get_argument(name)
199
204
  end
200
205
 
201
206
  class << self
@@ -218,8 +223,10 @@ module GraphQL
218
223
  own_extras + (superclass.respond_to?(:extras) ? superclass.extras : [])
219
224
  end
220
225
 
221
- # Specifies whether or not the field is nullable. Defaults to `true`
222
- # TODO unify with {#type}
226
+ # If `true` (default), then the return type for this resolver will be nullable.
227
+ # If `false`, then the return type is non-null.
228
+ #
229
+ # @see #type which sets the return type of this field and accepts a `null:` option
223
230
  # @param allow_null [Boolean] Whether or not the response can be null
224
231
  def null(allow_null = nil)
225
232
  if !allow_null.nil?
@@ -332,47 +339,9 @@ module GraphQL
332
339
  # also add some preparation hook methods which will be used for this argument
333
340
  # @see {GraphQL::Schema::Argument#initialize} for the signature
334
341
  def argument(*args, **kwargs, &block)
335
- loads = kwargs[:loads]
336
342
  # Use `from_resolver: true` to short-circuit the InputObject's own `loads:` implementation
337
343
  # so that we can support `#load_{x}` methods below.
338
- arg_defn = super(*args, from_resolver: true, **kwargs)
339
- own_arguments_loads_as_type[arg_defn.keyword] = loads if loads
340
-
341
- if !method_defined?(:"load_#{arg_defn.keyword}")
342
- if loads && arg_defn.type.list?
343
- class_eval <<-RUBY, __FILE__, __LINE__ + 1
344
- def load_#{arg_defn.keyword}(values)
345
- argument = @arguments_by_keyword[:#{arg_defn.keyword}]
346
- lookup_as_type = @arguments_loads_as_type[:#{arg_defn.keyword}]
347
- context.schema.after_lazy(values) do |values2|
348
- GraphQL::Execution::Lazy.all(values2.map { |value| load_application_object(argument, lookup_as_type, value, context) })
349
- end
350
- end
351
- RUBY
352
- elsif loads
353
- class_eval <<-RUBY, __FILE__, __LINE__ + 1
354
- def load_#{arg_defn.keyword}(value)
355
- argument = @arguments_by_keyword[:#{arg_defn.keyword}]
356
- lookup_as_type = @arguments_loads_as_type[:#{arg_defn.keyword}]
357
- load_application_object(argument, lookup_as_type, value, context)
358
- end
359
- RUBY
360
- else
361
- class_eval <<-RUBY, __FILE__, __LINE__ + 1
362
- def load_#{arg_defn.keyword}(value)
363
- value
364
- end
365
- RUBY
366
- end
367
- end
368
-
369
- arg_defn
370
- end
371
-
372
- # @api private
373
- def arguments_loads_as_type
374
- inherited_lookups = superclass.respond_to?(:arguments_loads_as_type) ? superclass.arguments_loads_as_type : {}
375
- inherited_lookups.merge(own_arguments_loads_as_type)
344
+ super(*args, from_resolver: true, **kwargs)
376
345
  end
377
346
 
378
347
  # Registers new extension
@@ -408,10 +377,6 @@ module GraphQL
408
377
  def own_extensions
409
378
  @own_extensions
410
379
  end
411
-
412
- def own_arguments_loads_as_type
413
- @own_arguments_loads_as_type ||= {}
414
- end
415
380
  end
416
381
  end
417
382
  end
@@ -14,7 +14,7 @@ module GraphQL
14
14
  class Subscription < GraphQL::Schema::Resolver
15
15
  extend GraphQL::Schema::Resolver::HasPayloadType
16
16
  extend GraphQL::Schema::Member::HasFields
17
-
17
+ NO_UPDATE = :no_update
18
18
  # The generated payload type is required; If there's no payload,
19
19
  # propagate null.
20
20
  null false
@@ -58,11 +58,9 @@ module GraphQL
58
58
  end
59
59
  end
60
60
 
61
- # Default implementation returns the root object.
61
+ # The default implementation returns nothing on subscribe.
62
62
  # Override it to return an object or
63
- # `:no_response` to return nothing.
64
- #
65
- # The default is `:no_response`.
63
+ # `:no_response` to (explicitly) return nothing.
66
64
  def subscribe(args = {})
67
65
  :no_response
68
66
  end
@@ -70,7 +68,7 @@ module GraphQL
70
68
  # Wrap the user-provided `#update` hook
71
69
  def resolve_update(**args)
72
70
  ret_val = args.any? ? update(**args) : update
73
- if ret_val == :no_update
71
+ if ret_val == NO_UPDATE
74
72
  context.namespace(:subscriptions)[:no_update] = true
75
73
  context.skip
76
74
  else
@@ -79,7 +77,7 @@ module GraphQL
79
77
  end
80
78
 
81
79
  # The default implementation returns the root object.
82
- # Override it to return `:no_update` if you want to
80
+ # Override it to return {NO_UPDATE} if you want to
83
81
  # skip updates sometimes. Or override it to return a different object.
84
82
  def update(args = {})
85
83
  object
@@ -124,7 +122,7 @@ module GraphQL
124
122
  # In that implementation, only `.trigger` calls with _exact matches_ result in updates to subscribers.
125
123
  #
126
124
  # To implement a filtered stream-type subscription flow, override this method to return a string with field name and subscription scope.
127
- # Then, implement {#update} to compare its arguments to the current `object` and return `:no_update` when an
125
+ # Then, implement {#update} to compare its arguments to the current `object` and return {NO_UPDATE} when an
128
126
  # update should be filtered out.
129
127
  #
130
128
  # @see {#update} for how to skip updates when an event comes with a matching topic.
@@ -0,0 +1,29 @@
1
+ # frozen_string_literal: true
2
+
3
+ module GraphQL
4
+ class Schema
5
+ class Validator
6
+ # Use this to specifically reject values that respond to `.blank?` and respond truthy for that method.
7
+ #
8
+ # @example Require a non-empty string for an argument
9
+ # argument :name, String, required: true, validate: { allow_blank: false }
10
+ class AllowBlankValidator < Validator
11
+ def initialize(allow_blank_positional, allow_blank: nil, message: "%{validated} can't be blank", **default_options)
12
+ @message = message
13
+ super(**default_options)
14
+ @allow_blank = allow_blank.nil? ? allow_blank_positional : allow_blank
15
+ end
16
+
17
+ def validate(_object, _context, value)
18
+ if value.respond_to?(:blank?) && value.blank?
19
+ if (value.nil? && @allow_null) || @allow_blank
20
+ # pass
21
+ else
22
+ @message
23
+ end
24
+ end
25
+ end
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,26 @@
1
+ # frozen_string_literal: true
2
+
3
+ module GraphQL
4
+ class Schema
5
+ class Validator
6
+ # Use this to specifically reject or permit `nil` values (given as `null` from GraphQL).
7
+ #
8
+ # @example require a non-null value for an argument if it is provided
9
+ # argument :name, String, required: false, validates: { allow_null: false }
10
+ class AllowNullValidator < Validator
11
+ MESSAGE = "%{validated} can't be null"
12
+ def initialize(allow_null_positional, allow_null: nil, message: MESSAGE, **default_options)
13
+ @message = message
14
+ super(**default_options)
15
+ @allow_null = allow_null.nil? ? allow_null_positional : allow_null
16
+ end
17
+
18
+ def validate(_object, _context, value)
19
+ if value.nil? && !@allow_null
20
+ @message
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end
26
+ end
@@ -21,7 +21,9 @@ module GraphQL
21
21
  end
22
22
 
23
23
  def validate(_object, _context, value)
24
- if @in_list.include?(value)
24
+ if permitted_empty_value?(value)
25
+ # pass
26
+ elsif @in_list.include?(value)
25
27
  @message
26
28
  end
27
29
  end
@@ -38,7 +38,8 @@ module GraphQL
38
38
  end
39
39
 
40
40
  def validate(_object, _context, value)
41
- if (@with_pattern && !value.match?(@with_pattern)) ||
41
+ if value.nil? ||
42
+ (@with_pattern && !value.match?(@with_pattern)) ||
42
43
  (@without_pattern && value.match?(@without_pattern))
43
44
  @message
44
45
  end
@@ -23,7 +23,9 @@ module GraphQL
23
23
  end
24
24
 
25
25
  def validate(_object, _context, value)
26
- if !@in_list.include?(value)
26
+ if permitted_empty_value?(value)
27
+ # pass
28
+ elsif !@in_list.include?(value)
27
29
  @message
28
30
  end
29
31
  end
@@ -43,11 +43,13 @@ module GraphQL
43
43
  end
44
44
 
45
45
  def validate(_object, _context, value)
46
- if @maximum && value.length > @maximum
46
+ return if permitted_empty_value?(value) # pass in this case
47
+ length = value.nil? ? 0 : value.length
48
+ if @maximum && length > @maximum
47
49
  partial_format(@too_long, { count: @maximum })
48
- elsif @minimum && value.length < @minimum
50
+ elsif @minimum && length < @minimum
49
51
  partial_format(@too_short, { count: @minimum })
50
- elsif @is && value.length != @is
52
+ elsif @is && length != @is
51
53
  partial_format(@wrong_length, { count: @is })
52
54
  end
53
55
  end
@@ -24,13 +24,15 @@ module GraphQL
24
24
  # @param other_than [Integer]
25
25
  # @param odd [Boolean]
26
26
  # @param even [Boolean]
27
+ # @param within [Range]
27
28
  # @param message [String] used for all validation failures
28
29
  def initialize(
29
30
  greater_than: nil, greater_than_or_equal_to: nil,
30
31
  less_than: nil, less_than_or_equal_to: nil,
31
32
  equal_to: nil, other_than: nil,
32
- odd: nil, even: nil,
33
+ odd: nil, even: nil, within: nil,
33
34
  message: "%{validated} must be %{comparison} %{target}",
35
+ null_message: Validator::AllowNullValidator::MESSAGE,
34
36
  **default_options
35
37
  )
36
38
 
@@ -42,12 +44,18 @@ module GraphQL
42
44
  @other_than = other_than
43
45
  @odd = odd
44
46
  @even = even
47
+ @within = within
45
48
  @message = message
49
+ @null_message = null_message
46
50
  super(**default_options)
47
51
  end
48
52
 
49
53
  def validate(object, context, value)
50
- if @greater_than && value <= @greater_than
54
+ if permitted_empty_value?(value)
55
+ # pass in this case
56
+ elsif value.nil? # @allow_null is handled in the parent class
57
+ @null_message
58
+ elsif @greater_than && value <= @greater_than
51
59
  partial_format(@message, { comparison: "greater than", target: @greater_than })
52
60
  elsif @greater_than_or_equal_to && value < @greater_than_or_equal_to
53
61
  partial_format(@message, { comparison: "greater than or equal to", target: @greater_than_or_equal_to })
@@ -63,6 +71,8 @@ module GraphQL
63
71
  (partial_format(@message, { comparison: "even", target: "" })).strip
64
72
  elsif @odd && !value.odd?
65
73
  (partial_format(@message, { comparison: "odd", target: "" })).strip
74
+ elsif @within && !@within.include?(value)
75
+ partial_format(@message, { comparison: "within", target: @within })
66
76
  end
67
77
  end
68
78
  end