@@ -307,22 +307,15 @@ def build(
307307 is_awaitable ,
308308 )
309309
310+ @staticmethod
310311 def build_response (
311- self , data : AwaitableOrValue [ Optional [Dict [str , Any ]]]
312- ) -> AwaitableOrValue [ ExecutionResult ] :
312+ data : Optional [Dict [str , Any ]], errors : List [ GraphQLError ]
313+ ) -> ExecutionResult :
313314 """Build response.
314315
315316 Given a completed execution context and data, build the (data, errors) response
316317 defined by the "Response" section of the GraphQL spec.
317318 """
318- if self .is_awaitable (data ):
319-
320- async def build_response_async () -> ExecutionResult :
321- return self .build_response (await data ) # type: ignore
322-
323- return build_response_async ()
324- data = cast (Optional [Dict [str , Any ]], data )
325- errors = self .errors
326319 if not errors :
327320 return ExecutionResult (data , None )
328321 # Sort the error list in order to make it deterministic, since we might have
@@ -357,30 +350,11 @@ def execute_operation(
357350
358351 path = None
359352
360- # Errors from sub-fields of a NonNull type may propagate to the top level, at
361- # which point we still log the error and null the parent field, which in this
362- # case is the entire response.
363- try :
364- # noinspection PyArgumentList
365- result = (
366- self .execute_fields_serially
367- if operation .operation == OperationType .MUTATION
368- else self .execute_fields
369- )(root_type , root_value , path , root_fields )
370- except GraphQLError as error :
371- self .errors .append (error )
372- return None
373- else :
374- if self .is_awaitable (result ):
375- # noinspection PyShadowingNames
376- async def await_result () -> Any :
377- try :
378- return await result # type: ignore
379- except GraphQLError as error :
380- self .errors .append (error )
381-
382- return await_result ()
383- return result
353+ return (
354+ self .execute_fields_serially
355+ if operation .operation == OperationType .MUTATION
356+ else self .execute_fields
357+ )(root_type , root_value , path , root_fields )
384358
385359 def execute_fields_serially (
386360 self ,
@@ -815,6 +789,7 @@ def complete_abstract_value(
815789 runtime_type = resolve_type_fn (result , info , return_type ) # type: ignore
816790
817791 if self .is_awaitable (runtime_type ):
792+ runtime_type = cast (Awaitable , runtime_type )
818793
819794 async def await_complete_object_value () -> Any :
820795 value = self .complete_object_value (
@@ -1037,9 +1012,31 @@ def execute(
10371012 # its descendants will be omitted, and sibling fields will still be executed. An
10381013 # execution which encounters errors will still result in a coroutine object that
10391014 # can be executed without errors.
1040-
1041- data = exe_context .execute_operation (exe_context .operation , root_value )
1042- return exe_context .build_response (data )
1015+ #
1016+ # Errors from sub-fields of a NonNull type may propagate to the top level,
1017+ # at which point we still log the error and null the parent field, which
1018+ # in this case is the entire response.
1019+ errors = exe_context .errors
1020+ build_response = exe_context .build_response
1021+ try :
1022+ operation = exe_context .operation
1023+ result = exe_context .execute_operation (operation , root_value )
1024+
1025+ if exe_context .is_awaitable (result ):
1026+ # noinspection PyShadowingNames
1027+ async def await_result () -> Any :
1028+ try :
1029+ return build_response (await result , errors ) # type: ignore
1030+ except GraphQLError as error :
1031+ errors .append (error )
1032+ return build_response (None , errors )
1033+
1034+ return await_result ()
1035+ except GraphQLError as error :
1036+ errors .append (error )
1037+ return build_response (None , errors )
1038+ else :
1039+ return build_response (result , errors ) # type: ignore
10431040
10441041
10451042def assume_not_awaitable (_value : Any ) -> bool :
0 commit comments