99 */
1010
1111import 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' ;
1413import { GraphQLError } from '../../error' ;
1514import { print } from '../../language/printer' ;
1615import { NAME } from '../../language/kinds' ;
17- import {
18- GraphQLScalarType ,
19- GraphQLEnumType ,
20- GraphQLInputObjectType
21- } from '../../type/definition' ;
16+ import { isInputType } from '../../type/definition' ;
2217import { 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