diff --git a/src/main/java/graphql/schema/validation/DeprecatedInputObjectAndArgumentsAreValid.java b/src/main/java/graphql/schema/validation/DeprecatedInputObjectAndArgumentsAreValid.java index ce619223cc..f7c1c1c776 100644 --- a/src/main/java/graphql/schema/validation/DeprecatedInputObjectAndArgumentsAreValid.java +++ b/src/main/java/graphql/schema/validation/DeprecatedInputObjectAndArgumentsAreValid.java @@ -42,10 +42,9 @@ public TraversalControl visitGraphQLInputObjectField(GraphQLInputObjectField inp // An applied directive's argument cannot be deprecated. @Override public TraversalControl visitGraphQLArgument(GraphQLArgument argument, TraverserContext context) { - // There can only be at most one @deprecated, because it is not a repeatable directive - GraphQLAppliedDirective deprecatedDirective = argument.getAppliedDirective(Directives.DEPRECATED_DIRECTIVE_DEFINITION.getName()); - - if (deprecatedDirective != null && GraphQLTypeUtil.isNonNull(argument.getType()) && !argument.hasSetDefaultValue()) { + // even if an argument is built using SLD or direct via code, the isDeprecated() method works + boolean isDeprecated = argument.isDeprecated(); + if (isDeprecated && GraphQLTypeUtil.isNonNull(argument.getType()) && !argument.hasSetDefaultValue()) { if (context.getParentNode() instanceof GraphQLFieldDefinition) { GraphQLFieldDefinition fieldDefinition = (GraphQLFieldDefinition) context.getParentNode(); SchemaValidationErrorCollector errorCollector = context.getVarFromParents(SchemaValidationErrorCollector.class); diff --git a/src/test/groovy/graphql/schema/validation/DeprecatedInputObjectAndArgumentsAreValidTest.groovy b/src/test/groovy/graphql/schema/validation/DeprecatedInputObjectAndArgumentsAreValidTest.groovy index 056f134db4..4f82f5f688 100644 --- a/src/test/groovy/graphql/schema/validation/DeprecatedInputObjectAndArgumentsAreValidTest.groovy +++ b/src/test/groovy/graphql/schema/validation/DeprecatedInputObjectAndArgumentsAreValidTest.groovy @@ -1,6 +1,12 @@ package graphql.schema.validation +import graphql.Scalars import graphql.TestUtil +import graphql.schema.GraphQLArgument +import graphql.schema.GraphQLFieldDefinition +import graphql.schema.GraphQLNonNull +import graphql.schema.GraphQLObjectType +import graphql.schema.GraphQLSchema import spock.lang.Specification class DeprecatedInputObjectAndArgumentsAreValidTest extends Specification { @@ -293,4 +299,31 @@ class DeprecatedInputObjectAndArgumentsAreValidTest extends Specification { noExceptionThrown() } + def "schema build via code has the same validation rule"() { + when: + GraphQLArgument deprecatedArg = GraphQLArgument.newArgument() + .name("input") + .type(GraphQLNonNull.nonNull(Scalars.GraphQLString)) + .deprecate("Some very good reason") + .build() + + GraphQLFieldDefinition field = GraphQLFieldDefinition.newFieldDefinition() + .name("field") + .type(Scalars.GraphQLString) + .argument(deprecatedArg) + .build() + + GraphQLObjectType queryType = GraphQLObjectType.newObject() + .name("Query") + .field(field) + .build() + + GraphQLSchema.newSchema() + .query(queryType) + .build() + + then: + def invalidSchemaException = thrown(InvalidSchemaException) + invalidSchemaException.message.contains("Required argument 'input' on field 'field' cannot be deprecated") + } }