@@ -108,8 +108,9 @@ def coerce_value(
108108 coerced_value_list : List [Any ] = []
109109 append_item = coerced_value_list .append
110110 for index , item_value in enumerate (value ):
111+ item_path = Path (path , index )
111112 coerced_item = coerce_value (
112- item_value , item_type , blame_node , at_path ( path , index )
113+ item_value , item_type , blame_node , item_path
113114 )
114115 if coerced_item .errors :
115116 errors = add (errors , * coerced_item .errors )
@@ -136,6 +137,7 @@ def coerce_value(
136137
137138 # Ensure every defined field is valid.
138139 for field_name , field in fields .items ():
140+ field_path = Path (path , field_name )
139141 field_value = value .get (field_name , INVALID )
140142 if field_value is INVALID :
141143 if field .default_value is not INVALID :
@@ -147,14 +149,15 @@ def coerce_value(
147149 errors = add (
148150 errors ,
149151 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" ,
152154 blame_node ,
155+ field_path ,
153156 ),
154157 )
155158 else :
156159 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
158161 )
159162 if coerced_field .errors :
160163 errors = add (errors , * coerced_field .errors )
@@ -201,37 +204,26 @@ def add(
201204 return (errors or []) + list (more_errors )
202205
203206
204- def at_path (prev : Optional [Path ], key : Union [str , int ]) -> Path :
205- return Path (prev , key )
206-
207-
208207def coercion_error (
209208 message : str ,
210209 blame_node : Node = None ,
211210 path : Path = None ,
212211 sub_message : str = None ,
213212 original_error : Exception = None ,
214213) -> 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