Skip to content

Commit

Permalink
Validate that deprecated argument are optional
Browse files Browse the repository at this point in the history
  • Loading branch information
jturkel committed Sep 4, 2020
1 parent 906c9b1 commit 9520f98
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
8 changes: 8 additions & 0 deletions lib/graphql/schema/validation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down
36 changes: 36 additions & 0 deletions spec/graphql/schema/validation_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 9520f98

Please sign in to comment.