Skip to content

Commit 947325b

Browse files
committed
simplify variable input type validator for clarity
1 parent 80df2e1 commit 947325b

File tree

1 file changed

+14
-22
lines changed

1 file changed

+14
-22
lines changed

src/validator/rules/VariablesAreInputTypes.js

Lines changed: 14 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,11 @@
99
*/
1010

1111
import type { ValidationContext } from '../index';
12-
import type { Name, VariableDefinition, Type } from '../../language/ast';
13-
import invariant from '../../utils/invariant';
12+
import type { VariableDefinition } from '../../language/ast';
1413
import { GraphQLError } from '../../error';
1514
import { print } from '../../language/printer';
1615
import { NAME } from '../../language/kinds';
17-
import {
18-
GraphQLScalarType,
19-
GraphQLEnumType,
20-
GraphQLInputObjectType
21-
} from '../../type/definition';
16+
import { isInputType } from '../../type/definition';
2217
import { nonInputTypeOnVarMessage } from '../errors';
2318

2419

@@ -33,13 +28,18 @@ export default function VariablesAreInputTypes(
3328
): any {
3429
return {
3530
VariableDefinition(node: VariableDefinition): ?GraphQLError {
36-
var typeName = getTypeASTName(node.type);
37-
var type = context.getSchema().getType(typeName);
38-
var isInputType =
39-
type instanceof GraphQLScalarType ||
40-
type instanceof GraphQLEnumType ||
41-
type instanceof GraphQLInputObjectType;
42-
if (!isInputType) {
31+
// Get the un-modified type from the variable definition, unwrapping
32+
// List and NonNull.
33+
var typeAST = node.type;
34+
while (typeAST.kind !== NAME) {
35+
typeAST = typeAST.type;
36+
}
37+
38+
// Get the type definition from the Schema.
39+
var typeDef = context.getSchema().getType(typeAST.value);
40+
41+
// If the variable type is not an input type, return an error.
42+
if (!isInputType(typeDef)) {
4343
var variableName = node.variable.name.value;
4444
return new GraphQLError(
4545
nonInputTypeOnVarMessage(variableName, print(node.type)),
@@ -49,11 +49,3 @@ export default function VariablesAreInputTypes(
4949
}
5050
};
5151
}
52-
53-
function getTypeASTName(typeAST: Type): string {
54-
if (typeAST.kind === NAME) {
55-
return (typeAST: Name).value;
56-
}
57-
invariant(typeAST.type, 'Must be wrapping type');
58-
return getTypeASTName(typeAST.type);
59-
}

0 commit comments

Comments
 (0)