lib/graphql/schema/argument.rb in graphql-1.13.6 vs lib/graphql/schema/argument.rb in graphql-1.13.7
- old
+ new
@@ -46,17 +46,25 @@
# @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: true, 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)
+ # @param replace_null_with_default [Boolean] if `true`, incoming values of `null` will be replaced with the configured `default_value`
+ def initialize(arg_name = nil, type_expr = nil, desc = nil, required: true, 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, replace_null_with_default: false, &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 != true
@default_value = default_value
+ if replace_null_with_default
+ if !default_value?
+ raise ArgumentError, "`replace_null_with_default: true` requires a default value, please provide one with `default_value: ...`"
+ end
+ @replace_null_with_default = true
+ end
+
@owner = owner
@as = as
@loads = loads
@keyword = as || (arg_name.is_a?(Symbol) ? arg_name : Schema::Member::BuildType.underscore(@name).to_sym)
@prepare = prepare
@@ -95,10 +103,14 @@
# @return [Boolean] True if this argument has a default value
def default_value?
@default_value != NO_DEFAULT
end
+ def replace_null_with_default?
+ @replace_null_with_default
+ end
+
attr_writer :description
# @return [String] Documentation for this argument
def description(text = nil)
if text
@@ -249,9 +261,14 @@
default_used = true
else
# no value at all
owner.validate_directive_argument(self, nil)
return
+ end
+
+ if value.nil? && replace_null_with_default?
+ value = default_value
+ default_used = true
end
loaded_value = nil
coerced_value = context.schema.error_handler.with_error_handling(context) do
type.coerce_input(value, context)