@@ -831,54 +831,63 @@ def execute_sync(
831831 operation_extensions , sync = True
832832 )
833833
834+ pre_execution_result : ExecutionResult | None = None
835+
834836 try :
835837 with extensions_runner .operation ():
836838 # Note: In graphql-core the schema would be validated here but in
837839 # Strawberry we are validating it at initialisation time instead
838- if (
839- pre_execution_result := self ._prepare_operation_sync (
840- execution_context , extensions_runner
841- )
842- ) is not None :
843- return pre_execution_result
844-
845- assert execution_context .graphql_document is not None
846- with extensions_runner .executing ():
847- if not execution_context .result :
848- result = execute_function (
849- self ._schema ,
850- execution_context .graphql_document ,
851- root_value = execution_context .root_value ,
852- middleware = middleware_manager ,
853- variable_values = execution_context .variables ,
854- operation_name = execution_context .operation_name ,
855- context_value = execution_context .context ,
856- is_awaitable = optimized_is_awaitable ,
857- ** execution_context_class_kwargs (
858- self .execution_context_class
859- ),
860- ** custom_context_kwargs ,
861- )
840+ pre_execution_result = self ._prepare_operation_sync (
841+ execution_context , extensions_runner
842+ )
862843
863- if isawaitable (result ):
864- result = cast ("Awaitable[GraphQLExecutionResult]" , result )
865- ensure_future (result ).cancel ()
866- raise RuntimeError ( # noqa: TRY301
867- "GraphQL execution failed to complete synchronously."
844+ if pre_execution_result is None :
845+ assert execution_context .graphql_document is not None
846+ with extensions_runner .executing ():
847+ if not execution_context .result :
848+ result = execute_function (
849+ self ._schema ,
850+ execution_context .graphql_document ,
851+ root_value = execution_context .root_value ,
852+ middleware = middleware_manager ,
853+ variable_values = execution_context .variables ,
854+ operation_name = execution_context .operation_name ,
855+ context_value = execution_context .context ,
856+ is_awaitable = optimized_is_awaitable ,
857+ ** execution_context_class_kwargs (
858+ self .execution_context_class
859+ ),
860+ ** custom_context_kwargs ,
868861 )
869862
870- result = cast ("GraphQLExecutionResult" , result )
871- execution_context .result = result
872- # Also set errors on the context so that it's easier
873- # to access in extensions
874- if result .errors :
875- execution_context .pre_execution_errors = result .errors
876-
877- # Run the `Schema.process_errors` function here before
878- # extensions have a chance to modify them (see the MaskErrors
879- # extension). That way we can log the original errors but
880- # only return a sanitised version to the client.
881- self ._process_errors (result .errors , execution_context )
863+ if isawaitable (result ):
864+ result = cast (
865+ "Awaitable[GraphQLExecutionResult]" , result
866+ )
867+ ensure_future (result ).cancel ()
868+ raise RuntimeError ( # noqa: TRY301
869+ "GraphQL execution failed to complete synchronously."
870+ )
871+
872+ result = cast ("GraphQLExecutionResult" , result )
873+ execution_context .result = result
874+ # Also set errors on the context so that it's easier
875+ # to access in extensions
876+ if result .errors :
877+ execution_context .pre_execution_errors = result .errors
878+
879+ # Run the `Schema.process_errors` function here before
880+ # extensions have a chance to modify them (see the
881+ # MaskErrors extension). That way we can log the original
882+ # errors but only return a sanitised version to the client.
883+ self ._process_errors (result .errors , execution_context )
884+
885+ if pre_execution_result is not None :
886+ # Operation hooks may replace pre-execution errors (for example,
887+ # MaskErrors anonymises them), so finalise the returned result only
888+ # after those hooks have completed.
889+ pre_execution_result .errors = execution_context .pre_execution_errors
890+ return pre_execution_result
882891 except (
883892 MissingQueryError ,
884893 CannotGetOperationTypeError ,
@@ -894,6 +903,8 @@ def execute_sync(
894903 errors = errors ,
895904 extensions = extensions_runner .get_extensions_results_sync (),
896905 )
906+
907+ assert execution_context .result is not None
897908 return ExecutionResult (
898909 data = execution_context .result .data ,
899910 errors = execution_context .result .errors ,
0 commit comments