@@ -10,8 +10,9 @@ import suggestionList from '../../jsutils/suggestionList';
10
10
11
11
import { GraphQLError } from '../../error/GraphQLError' ;
12
12
13
- import { type ValueNode } from '../../language/ast ' ;
13
+ import { Kind } from '../../language/kinds ' ;
14
14
import { print } from '../../language/printer' ;
15
+ import { type ValueNode } from '../../language/ast' ;
15
16
import { type ASTVisitor } from '../../language/visitor' ;
16
17
17
18
import {
@@ -76,27 +77,19 @@ export function unknownFieldMessage(
76
77
*/
77
78
export function ValuesOfCorrectType ( context : ValidationContext ) : ASTVisitor {
78
79
return {
79
- NullValue ( node ) {
80
- const type = context . getInputType ( ) ;
81
- if ( isNonNullType ( type ) ) {
82
- context . reportError (
83
- new GraphQLError ( badValueMessage ( inspect ( type ) , print ( node ) ) , node ) ,
84
- ) ;
85
- }
86
- } ,
87
80
ListValue ( node ) {
88
81
// Note: TypeInfo will traverse into a list's item type, so look to the
89
82
// parent input type to check if it is a list.
90
83
const type = getNullableType ( context . getParentInputType ( ) ) ;
91
84
if ( ! isListType ( type ) ) {
92
- isValidScalar ( context , node ) ;
85
+ isValidValueNode ( context , node ) ;
93
86
return false ; // Don't traverse further.
94
87
}
95
88
} ,
96
89
ObjectValue ( node ) {
97
90
const type = getNamedType ( context . getInputType ( ) ) ;
98
91
if ( ! isInputObjectType ( type ) ) {
99
- isValidScalar ( context , node ) ;
92
+ isValidValueNode ( context , node ) ;
100
93
return false ; // Don't traverse further.
101
94
}
102
95
// Ensure every required field exists.
@@ -130,35 +123,27 @@ export function ValuesOfCorrectType(context: ValidationContext): ASTVisitor {
130
123
) ;
131
124
}
132
125
} ,
133
- EnumValue ( node ) {
134
- const type = getNamedType ( context . getInputType ( ) ) ;
135
- if ( ! isEnumType ( type ) ) {
136
- isValidScalar ( context , node ) ;
137
- } else if ( ! type . getValue ( node . value ) ) {
126
+ NullValue ( node ) {
127
+ const type = context . getInputType ( ) ;
128
+ if ( isNonNullType ( type ) ) {
138
129
context . reportError (
139
- new GraphQLError (
140
- badEnumValueMessage (
141
- type . name ,
142
- print ( node ) ,
143
- enumTypeSuggestion ( type , node ) ,
144
- ) ,
145
- node ,
146
- ) ,
130
+ new GraphQLError ( badValueMessage ( inspect ( type ) , print ( node ) ) , node ) ,
147
131
) ;
148
132
}
149
133
} ,
150
- IntValue : node => isValidScalar ( context , node ) ,
151
- FloatValue : node => isValidScalar ( context , node ) ,
152
- StringValue : node => isValidScalar ( context , node ) ,
153
- BooleanValue : node => isValidScalar ( context , node ) ,
134
+ EnumValue : node => isValidValueNode ( context , node ) ,
135
+ IntValue : node => isValidValueNode ( context , node ) ,
136
+ FloatValue : node => isValidValueNode ( context , node ) ,
137
+ StringValue : node => isValidValueNode ( context , node ) ,
138
+ BooleanValue : node => isValidValueNode ( context , node ) ,
154
139
} ;
155
140
}
156
141
157
142
/**
158
143
* Any value literal may be a valid representation of a Scalar, depending on
159
144
* that scalar type.
160
145
*/
161
- function isValidScalar ( context : ValidationContext , node : ValueNode ) : void {
146
+ function isValidValueNode ( context : ValidationContext , node : ValueNode ) : void {
162
147
// Report any error at the full type expected by the location.
163
148
const locationType = context . getInputType ( ) ;
164
149
if ( ! locationType ) {
@@ -167,15 +152,29 @@ function isValidScalar(context: ValidationContext, node: ValueNode): void {
167
152
168
153
const type = getNamedType ( locationType ) ;
169
154
155
+ if ( isEnumType ( type ) ) {
156
+ if ( node . kind !== Kind . ENUM || ! type . getValue ( node . value ) ) {
157
+ context . reportError (
158
+ new GraphQLError (
159
+ badEnumValueMessage (
160
+ type . name ,
161
+ print ( node ) ,
162
+ enumTypeSuggestion ( type , node ) ,
163
+ ) ,
164
+ node ,
165
+ ) ,
166
+ ) ;
167
+ }
168
+ return ;
169
+ }
170
+
170
171
if ( ! isScalarType ( type ) ) {
171
- const message = isEnumType ( type )
172
- ? badEnumValueMessage (
173
- inspect ( locationType ) ,
174
- print ( node ) ,
175
- enumTypeSuggestion ( type , node ) ,
176
- )
177
- : badValueMessage ( inspect ( locationType ) , print ( node ) ) ;
178
- context . reportError ( new GraphQLError ( message , node ) ) ;
172
+ context . reportError (
173
+ new GraphQLError (
174
+ badValueMessage ( inspect ( locationType ) , print ( node ) ) ,
175
+ node ,
176
+ ) ,
177
+ ) ;
179
178
return ;
180
179
}
181
180
0 commit comments