55import graphql .language .Value ;
66import graphql .schema .CoercingParseValueException ;
77import graphql .schema .GraphQLArgument ;
8+ import graphql .schema .GraphQLDirective ;
89import graphql .schema .GraphQLInputType ;
910import graphql .schema .GraphQLSchema ;
1011import graphql .schema .GraphQLSchemaElement ;
1415import graphql .util .TraverserContext ;
1516import graphql .validation .ValidationUtil ;
1617
17- import static graphql .schema .GraphQLTypeUtil .simplePrint ;
1818import static java .lang .String .format ;
1919
2020public class AppliedDirectiveArgumentsAreValid extends GraphQLTypeVisitorStub {
2121
2222 private ValidationUtil validationUtil = new ValidationUtil ();
2323
2424
25- public TraversalControl visitGraphQLArgument (GraphQLArgument argument , TraverserContext <GraphQLSchemaElement > context ) {
26- // a directive argument is represented as GraphQLArgument.value
25+ @ Override
26+ public TraversalControl visitGraphQLDirective (GraphQLDirective directive , TraverserContext <GraphQLSchemaElement > context ) {
27+ // if there is no parent it means it is just a directive definition and not an applied directive
28+ if (context .getParentNode () != null ) {
29+ for (GraphQLArgument graphQLArgument : directive .getArguments ()) {
30+ checkArgument (directive , graphQLArgument , context );
31+ }
32+ }
33+ return TraversalControl .CONTINUE ;
34+ }
35+
36+ private void checkArgument (GraphQLDirective directive , GraphQLArgument argument , TraverserContext <GraphQLSchemaElement > context ) {
2737 if (!argument .hasSetValue ()) {
28- return TraversalControl . CONTINUE ;
38+ return ;
2939 }
3040 GraphQLSchema schema = context .getVarFromParents (GraphQLSchema .class );
3141 SchemaValidationErrorCollector errorCollector = context .getVarFromParents (SchemaValidationErrorCollector .class );
@@ -39,10 +49,9 @@ public TraversalControl visitGraphQLArgument(GraphQLArgument argument, Traverser
3949 invalid = true ;
4050 }
4151 if (invalid ) {
42- String message = format ("Invalid argument %s for type %s for applied directive " , argument .getValueState (), simplePrint ( argument . getType () ));
52+ String message = format ("Invalid argument '%s' for applied directive of name '%s' " , argument .getName (), directive . getName ( ));
4353 errorCollector .addError (new SchemaValidationError (SchemaValidationErrorType .InvalidAppliedDirectiveArgument , message ));
4454 }
45- return TraversalControl .CONTINUE ;
4655 }
4756
4857 private boolean isValidExternalValue (GraphQLSchema schema , Object externalValue , GraphQLInputType type ) {
0 commit comments