Skip to content

Commit 1ed72cc

Browse files
⚡️ Speed up function process_result by 100% in PR #3819 (feature/defer)
To optimize the provided code, we can reduce the number of checks by combining related conditionals and accessing properties once instead of multiple times. Furthermore, we can streamline the creation of the `data` dictionary. Here is an optimized version of the code. In this optimized version. - We access `result.errors` and `result.extensions` once and store their values in the `errors` and `extensions` variables, respectively. - We use dictionary unpacking with conditionals to add the "errors" and "extensions" keys to `data` only when they are present. This should provide a minor performance improvement while maintaining the same functionality.
1 parent a64b402 commit 1ed72cc

File tree

1 file changed

+6
-6
lines changed

1 file changed

+6
-6
lines changed

strawberry/http/__init__.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,12 @@ def process_result(result: ResultType) -> GraphQLHTTPResponse:
2020
if isinstance(result, GraphQLIncrementalExecutionResults):
2121
return result
2222

23-
data: GraphQLHTTPResponse = {"data": result.data}
24-
25-
if result.errors:
26-
data["errors"] = [err.formatted for err in result.errors]
27-
if result.extensions:
28-
data["extensions"] = result.extensions
23+
errors, extensions = result.errors, result.extensions
24+
data: GraphQLHTTPResponse = {
25+
"data": result.data,
26+
**({"errors": [err.formatted for err in errors]} if errors else {}),
27+
**({"extensions": extensions} if extensions else {}),
28+
}
2929

3030
return data
3131

0 commit comments

Comments
 (0)