@@ -108,8 +108,9 @@ def coerce_value(
108
108
coerced_value_list : List [Any ] = []
109
109
append_item = coerced_value_list .append
110
110
for index , item_value in enumerate (value ):
111
+ item_path = Path (path , index )
111
112
coerced_item = coerce_value (
112
- item_value , item_type , blame_node , at_path ( path , index )
113
+ item_value , item_type , blame_node , item_path
113
114
)
114
115
if coerced_item .errors :
115
116
errors = add (errors , * coerced_item .errors )
@@ -136,6 +137,7 @@ def coerce_value(
136
137
137
138
# Ensure every defined field is valid.
138
139
for field_name , field in fields .items ():
140
+ field_path = Path (path , field_name )
139
141
field_value = value .get (field_name , INVALID )
140
142
if field_value is INVALID :
141
143
if field .default_value is not INVALID :
@@ -147,14 +149,15 @@ def coerce_value(
147
149
errors = add (
148
150
errors ,
149
151
coercion_error (
150
- f"Field { print_path ( at_path ( path , field_name ) )} "
151
- f" of required type { field . type } was not provided" ,
152
+ f"Field of required type { inspect ( field . type )} "
153
+ " was not provided" ,
152
154
blame_node ,
155
+ field_path ,
153
156
),
154
157
)
155
158
else :
156
159
coerced_field = coerce_value (
157
- field_value , field .type , blame_node , at_path ( path , field_name )
160
+ field_value , field .type , blame_node , field_path
158
161
)
159
162
if coerced_field .errors :
160
163
errors = add (errors , * coerced_field .errors )
@@ -201,37 +204,26 @@ def add(
201
204
return (errors or []) + list (more_errors )
202
205
203
206
204
- def at_path (prev : Optional [Path ], key : Union [str , int ]) -> Path :
205
- return Path (prev , key )
206
-
207
-
208
207
def coercion_error (
209
208
message : str ,
210
209
blame_node : Node = None ,
211
210
path : Path = None ,
212
211
sub_message : str = None ,
213
212
original_error : Exception = None ,
214
213
) -> GraphQLError :
215
- """Return a GraphQLError instance"""
216
- path_str = print_path (path )
217
- if path_str :
218
- message += f" at { path_str } "
219
- message += "."
220
- if sub_message :
221
- message += sub_message
222
- # noinspection PyArgumentEqualDefault
223
- return GraphQLError (message , blame_node , None , None , None , original_error )
224
-
225
-
226
- def print_path (path : Optional [Path ]) -> str :
227
- """Build string describing the path into the value where error was found"""
228
- path_str = ""
229
- current_path : Optional [Path ] = path
230
- while current_path :
231
- path_str = (
232
- f".{ current_path .key } "
233
- if isinstance (current_path .key , str )
234
- else f"[{ current_path .key } ]"
235
- ) + path_str
236
- current_path = current_path .prev
237
- return f"value{ path_str } " if path_str else ""
214
+ """Return a coercion error with the given message describing the given path"""
215
+ full_message = message
216
+ # Build a string describing the path into the value where the error was found
217
+ if path :
218
+ segment_strings : List [str ] = []
219
+ current_path : Optional [Path ] = path
220
+ while current_path :
221
+ key = current_path .key
222
+ segment_strings .insert (0 , f".{ key } " if isinstance (key , str ) else f"[{ key } ]" )
223
+ current_path = current_path .prev
224
+ full_message += " at value" + "" .join (segment_strings )
225
+
226
+ full_message += "." + sub_message if sub_message else "."
227
+
228
+ # Return a GraphQLError instance
229
+ return GraphQLError (full_message , blame_node , None , None , None , original_error )
0 commit comments