Skip to content

Commit 4e2c13a

Browse files
committed
Validate that deprecated argument are optional
1 parent 906c9b1 commit 4e2c13a

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed

lib/graphql/schema/validation.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,12 @@ def self.assert_named_items_are_valid(item_name, get_items_proc)
133133
end
134134
}
135135

136+
DEPRECATED_ARGUMENTS_ARE_OPTIONAL = ->(argument) {
137+
if argument.deprecation_reason && argument.type.non_null?
138+
"must be optional because it's deprecated"
139+
end
140+
}
141+
136142
TYPE_IS_VALID_INPUT_TYPE = ->(type) {
137143
outer_type = type.type
138144
inner_type = outer_type.respond_to?(:unwrap) ? outer_type.unwrap : nil
@@ -265,8 +271,10 @@ def self.assert_named_items_are_valid(item_name, get_items_proc)
265271
Rules::NAME_IS_STRING,
266272
Rules::RESERVED_NAME,
267273
Rules::DESCRIPTION_IS_STRING_OR_NIL,
274+
Rules.assert_property(:deprecation_reason, String, NilClass),
268275
Rules::TYPE_IS_VALID_INPUT_TYPE,
269276
Rules::DEFAULT_VALUE_IS_VALID_FOR_TYPE,
277+
Rules::DEPRECATED_ARGUMENTS_ARE_OPTIONAL,
270278
],
271279
GraphQL::BaseType => [
272280
Rules::NAME_IS_STRING,

spec/graphql/schema/validation_spec.rb

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,30 @@ def assert_validation_warns(object, warning)
344344
end
345345
}
346346

347+
let(:deprecated_optional_argument) {
348+
GraphQL::Argument.define do
349+
name "Something"
350+
deprecation_reason "Don't use me"
351+
type GraphQL::INT_TYPE
352+
end
353+
}
354+
355+
let(:deprecated_required_argument) {
356+
GraphQL::Argument.define do
357+
name "Something"
358+
deprecation_reason "Don't use me"
359+
type !GraphQL::INT_TYPE
360+
end
361+
}
362+
363+
let(:invalid_deprecation_reason_argument) {
364+
GraphQL::Argument.define do
365+
name "Something"
366+
deprecation_reason 1
367+
type GraphQL::INT_TYPE
368+
end
369+
}
370+
347371
it "requires the type is a Base type" do
348372
assert_error_includes untyped_argument, "must be a valid input type (Scalar or InputObject), not Symbol"
349373
end
@@ -359,6 +383,18 @@ def assert_validation_warns(object, warning)
359383
it "allows null default value for nullable argument" do
360384
assert_nil GraphQL::Schema::Validation.validate(null_default_value)
361385
end
386+
387+
it "allows deprecated optional arguments" do
388+
assert_nil GraphQL::Schema::Validation.validate(deprecated_optional_argument)
389+
end
390+
391+
it "disallows deprecated required arguments" do
392+
assert_error_includes deprecated_required_argument, "must be optional because it's deprecated"
393+
end
394+
395+
it "disallows non-string deprecation reasons" do
396+
assert_error_includes invalid_deprecation_reason_argument, "deprecation_reason must return String or NilClass, not #{integer_class_name} (1)"
397+
end
362398
end
363399

364400
describe "validating instrumentation" do

0 commit comments

Comments
 (0)