From 9520f987ab6a3b9675cdd782fbb378251421a0ae Mon Sep 17 00:00:00 2001 From: Joel Turkel Date: Fri, 4 Sep 2020 10:41:31 -0400 Subject: [PATCH] Validate that deprecated argument are optional --- lib/graphql/schema/validation.rb | 8 ++++++ spec/graphql/schema/validation_spec.rb | 36 ++++++++++++++++++++++++++ 2 files changed, 44 insertions(+) diff --git a/lib/graphql/schema/validation.rb b/lib/graphql/schema/validation.rb index 3a150235079..011cb0f7654 100644 --- a/lib/graphql/schema/validation.rb +++ b/lib/graphql/schema/validation.rb @@ -133,6 +133,12 @@ def self.assert_named_items_are_valid(item_name, get_items_proc) end } + DEPRECATED_ARGUMENTS_ARE_OPTIONAL = ->(argument) { + if argument.deprecation_reason && argument.type.non_null? + "must be optional because it's deprecated" + end + } + TYPE_IS_VALID_INPUT_TYPE = ->(type) { outer_type = type.type 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) Rules::NAME_IS_STRING, Rules::RESERVED_NAME, Rules::DESCRIPTION_IS_STRING_OR_NIL, + Rules.assert_property(:deprecation_reason, String, NilClass), Rules::TYPE_IS_VALID_INPUT_TYPE, Rules::DEFAULT_VALUE_IS_VALID_FOR_TYPE, + Rules::DEPRECATED_ARGUMENTS_ARE_OPTIONAL, ], GraphQL::BaseType => [ Rules::NAME_IS_STRING, diff --git a/spec/graphql/schema/validation_spec.rb b/spec/graphql/schema/validation_spec.rb index 0408c3702f3..a0ca68b762d 100644 --- a/spec/graphql/schema/validation_spec.rb +++ b/spec/graphql/schema/validation_spec.rb @@ -344,6 +344,30 @@ def assert_validation_warns(object, warning) end } + let(:deprecated_optional_argument) { + GraphQL::Argument.define do + name "Something" + deprecation_reason "Don't use me" + type GraphQL::INT_TYPE + end + } + + let(:deprecated_required_argument) { + GraphQL::Argument.define do + name "Something" + deprecation_reason "Don't use me" + type !GraphQL::INT_TYPE + end + } + + let(:invalid_deprecation_reason_argument) { + GraphQL::Argument.define do + name "Something" + deprecation_reason 1 + type GraphQL::INT_TYPE + end + } + it "requires the type is a Base type" do assert_error_includes untyped_argument, "must be a valid input type (Scalar or InputObject), not Symbol" end @@ -359,6 +383,18 @@ def assert_validation_warns(object, warning) it "allows null default value for nullable argument" do assert_nil GraphQL::Schema::Validation.validate(null_default_value) end + + it "allows deprecated optional arguments" do + assert_nil GraphQL::Schema::Validation.validate(deprecated_optional_argument) + end + + it "disallows deprecated required arguments" do + assert_error_includes deprecated_required_argument, "must be optional because it's deprecated" + end + + it "disallows non-string deprecation reasons" do + assert_error_includes invalid_deprecation_reason_argument, "deprecation_reason must return String or NilClass, not Integer (1)" + end end describe "validating instrumentation" do