File tree Expand file tree Collapse file tree 5 files changed +67
-9
lines changed
test/groovy/graphql/validation/rules Expand file tree Collapse file tree 5 files changed +67
-9
lines changed Original file line number Diff line number Diff line change @@ -11,8 +11,16 @@ public class SchemaUtil {
1111 public static boolean isLeafType (GraphQLType type ) {
1212 GraphQLUnmodifiedType unmodifiedType = getUnmodifiedType (type );
1313 return
14- unmodifiedType instanceof GraphQLScalarType ||
15- unmodifiedType instanceof GraphQLEnumType ;
14+ unmodifiedType instanceof GraphQLScalarType
15+ || unmodifiedType instanceof GraphQLEnumType ;
16+ }
17+
18+ public static boolean isInputType (GraphQLType graphQLType ) {
19+ GraphQLUnmodifiedType unmodifiedType = getUnmodifiedType (graphQLType );
20+ return
21+ unmodifiedType instanceof GraphQLScalarType
22+ || unmodifiedType instanceof GraphQLEnumType
23+ || unmodifiedType instanceof GraphQLInputObjectType ;
1624 }
1725
1826 public static GraphQLUnmodifiedType getUnmodifiedType (GraphQLType graphQLType ) {
Original file line number Diff line number Diff line change @@ -14,6 +14,7 @@ public enum ValidationErrorType {
1414 InlineFragmentTypeConditionInvalid ,
1515 FragmentTypeConditionInvalid ,
1616 UnknownArgument ,
17- UndefinedFragment
17+ UndefinedFragment ,
18+ NonInputTypeOnVariable
1819
1920}
Original file line number Diff line number Diff line change 11package graphql .validation ;
22
33
4+ import graphql .ShouldNotHappenException ;
45import graphql .language .*;
56import graphql .schema .*;
67
910
1011public class ValidationUtil {
1112
12- public Type getUnmodifiedType (Type type ) {
13+ public TypeName getUnmodifiedType (Type type ) {
1314 if (type instanceof ListType ) {
1415 return getUnmodifiedType (((ListType ) type ).getType ());
1516 } else if (type instanceof NonNullType ) {
1617 return getUnmodifiedType (((NonNullType ) type ).getType ());
18+ } else if (type instanceof TypeName ) {
19+ return (TypeName ) type ;
1720 }
18- return type ;
21+ throw new ShouldNotHappenException () ;
1922 }
2023
2124 public boolean isValidLiteralValue (Value value , GraphQLType type ) {
Original file line number Diff line number Diff line change 11package graphql .validation .rules ;
22
33
4- import graphql .validation .AbstractRule ;
5- import graphql .validation .ErrorCollector ;
6- import graphql .validation .ValidationContext ;
4+ import graphql .language .TypeName ;
5+ import graphql .language .VariableDefinition ;
6+ import graphql .schema .GraphQLType ;
7+ import graphql .schema .SchemaUtil ;
8+ import graphql .validation .*;
79
8- public class VariablesAreInputTypes extends AbstractRule {
10+ public class VariablesAreInputTypes extends AbstractRule {
911
1012 public VariablesAreInputTypes (ValidationContext validationContext , ErrorCollector errorCollector ) {
1113 super (validationContext , errorCollector );
1214 }
15+
16+ @ Override
17+ public void checkVariableDefinition (VariableDefinition variableDefinition ) {
18+ TypeName unmodifiedAstType = getValidationUtil ().getUnmodifiedType (variableDefinition .getType ());
19+
20+ GraphQLType type = SchemaUtil .findType (getValidationContext ().getSchema (), unmodifiedAstType .getName ());
21+ if (type == null ) return ;
22+ if (!SchemaUtil .isInputType (type )) {
23+ addError (new ValidationError (ValidationErrorType .NonInputTypeOnVariable ));
24+ }
25+ }
1326}
Original file line number Diff line number Diff line change 1+ package graphql.validation.rules
2+
3+ import graphql.StarWarsSchema
4+ import graphql.language.ListType
5+ import graphql.language.NonNullType
6+ import graphql.language.TypeName
7+ import graphql.language.VariableDefinition
8+ import graphql.validation.ErrorCollector
9+ import graphql.validation.ValidationContext
10+ import graphql.validation.ValidationErrorType
11+ import spock.lang.Specification
12+
13+
14+ class VariablesAreInputTypesTest extends Specification {
15+
16+ ValidationContext validationContext = Mock (ValidationContext )
17+ ErrorCollector errorCollector = new ErrorCollector ()
18+ VariablesAreInputTypes variablesAreInputTypes = new VariablesAreInputTypes (validationContext, errorCollector)
19+
20+
21+ def " the unmodified ast type is not a schema input type" () {
22+ given :
23+ def astType = new NonNullType (new ListType (new TypeName (StarWarsSchema . droidType. getName())))
24+ VariableDefinition variableDefinition = new VariableDefinition (" var" , astType)
25+ validationContext. getSchema() >> StarWarsSchema . starWarsSchema
26+
27+ when :
28+ variablesAreInputTypes. checkVariableDefinition(variableDefinition)
29+
30+ then :
31+ errorCollector. containsError(ValidationErrorType.NonInputTypeOnVariable )
32+ }
33+ }
You can’t perform that action at this time.
0 commit comments