@@ -108,11 +108,12 @@ export function coerceValue(
108
108
let errors ;
109
109
const coercedValue = [ ] ;
110
110
forEach ( ( value : any ) , ( itemValue , index ) => {
111
+ const itemPath = { prev : path , key : index } ;
111
112
const coercedItem = coerceValue (
112
113
itemValue ,
113
114
itemType ,
114
115
blameNode ,
115
- atPath ( path , index ) ,
116
+ itemPath ,
116
117
) ;
117
118
if ( coercedItem . errors ) {
118
119
errors = add ( errors , coercedItem . errors ) ;
@@ -143,6 +144,7 @@ export function coerceValue(
143
144
144
145
// Ensure every defined field is valid.
145
146
for ( const field of objectValues ( fields ) ) {
147
+ const fieldPath = { prev : path , key : field . name } ;
146
148
const fieldValue = value [ field . name ] ;
147
149
if ( fieldValue === undefined ) {
148
150
if ( field . defaultValue !== undefined ) {
@@ -151,9 +153,9 @@ export function coerceValue(
151
153
errors = add (
152
154
errors ,
153
155
coercionError (
154
- `Field ${ printPath ( atPath ( path , field . name ) ) } of required ` +
155
- `type ${ inspect ( field . type ) } was not provided` ,
156
+ `Field of required type ${ inspect ( field . type ) } was not provided` ,
156
157
blameNode ,
158
+ fieldPath ,
157
159
) ,
158
160
) ;
159
161
}
@@ -162,7 +164,7 @@ export function coerceValue(
162
164
fieldValue ,
163
165
field . type ,
164
166
blameNode ,
165
- atPath ( path , field . name ) ,
167
+ fieldPath ,
166
168
) ;
167
169
if ( coercedField . errors ) {
168
170
errors = add ( errors , coercedField . errors ) ;
@@ -208,17 +210,21 @@ function add(errors, moreErrors) {
208
210
return ( errors || [ ] ) . concat ( moreErrors ) ;
209
211
}
210
212
211
- function atPath ( prev , key ) {
212
- return { prev , key } ;
213
- }
214
-
215
213
function coercionError ( message , blameNode , path , subMessage , originalError ) {
216
- const pathStr = printPath ( path ) ;
217
214
let fullMessage = message ;
218
215
219
- if ( pathStr ) {
220
- fullMessage += ' at ' + pathStr ;
216
+ // Build a string describing the path into the value where the error was found
217
+ if ( path ) {
218
+ const segmentStrings = [ ] ;
219
+ for ( let currentPath = path ; currentPath ; currentPath = currentPath . prev ) {
220
+ const { key } = currentPath ;
221
+ segmentStrings . unshift (
222
+ typeof key === 'string' ? '.' + key : '[' + key . toString ( ) + ']' ,
223
+ ) ;
224
+ }
225
+ fullMessage += ' at value' + segmentStrings . join ( '' ) ;
221
226
}
227
+
222
228
fullMessage += subMessage ? '.' + subMessage : '.' ;
223
229
224
230
// Return a GraphQLError instance
@@ -231,17 +237,3 @@ function coercionError(message, blameNode, path, subMessage, originalError) {
231
237
originalError ,
232
238
) ;
233
239
}
234
-
235
- // Build a string describing the path into the value where the error was found
236
- function printPath ( path ) {
237
- let pathStr = '';
238
- let currentPath = path ;
239
- while ( currentPath ) {
240
- pathStr =
241
- ( typeof currentPath . key === 'string'
242
- ? '.' + currentPath . key
243
- : '[' + String ( currentPath . key ) + ']' ) + pathStr ;
244
- currentPath = currentPath . prev ;
245
- }
246
- return pathStr ? 'value ' + pathStr : '' ;
247
- }
0 commit comments