9
9
*/
10
10
11
11
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' ;
14
13
import { GraphQLError } from '../../error' ;
15
14
import { print } from '../../language/printer' ;
16
15
import { NAME } from '../../language/kinds' ;
17
- import {
18
- GraphQLScalarType ,
19
- GraphQLEnumType ,
20
- GraphQLInputObjectType
21
- } from '../../type/definition' ;
16
+ import { isInputType } from '../../type/definition' ;
22
17
import { nonInputTypeOnVarMessage } from '../errors' ;
23
18
24
19
@@ -33,13 +28,18 @@ export default function VariablesAreInputTypes(
33
28
) : any {
34
29
return {
35
30
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 ) ) {
43
43
var variableName = node . variable . name . value ;
44
44
return new GraphQLError (
45
45
nonInputTypeOnVarMessage ( variableName , print ( node . type ) ) ,
@@ -49,11 +49,3 @@ export default function VariablesAreInputTypes(
49
49
}
50
50
} ;
51
51
}
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