Skip to content

Commit 83565eb

Browse files
committed
fix(strawberry): Patch Schema.execute instead of execute
1 parent 081df7e commit 83565eb

File tree

1 file changed

+69
-44
lines changed

1 file changed

+69
-44
lines changed

sentry_sdk/integrations/strawberry.py

Lines changed: 69 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,10 @@
4747
from typing import TYPE_CHECKING
4848

4949
if TYPE_CHECKING:
50-
from typing import Any, Callable, Generator, List, Optional, Union
50+
from typing import Any, Callable, Generator, List, Optional
5151
from graphql import GraphQLError, GraphQLResolveInfo # type: ignore
5252
from strawberry.http import GraphQLHTTPResponse
53-
from strawberry.types import ExecutionContext, ExecutionResult, SubscriptionExecutionResult # type: ignore
53+
from strawberry.types import ExecutionContext, ExecutionResult # type: ignore
5454
from sentry_sdk._types import Event, EventProcessor
5555

5656

@@ -78,7 +78,7 @@ def setup_once():
7878
_check_minimum_version(StrawberryIntegration, version, "strawberry-graphql")
7979

8080
_patch_schema_init()
81-
_patch_execute()
81+
_patch_schema_execute()
8282
_patch_views()
8383

8484

@@ -287,55 +287,80 @@ def resolve(self, _next, root, info, *args, **kwargs):
287287
return _next(root, info, *args, **kwargs)
288288

289289

290-
def _patch_execute():
291-
# type: () -> None
292-
old_execute_async = strawberry_schema.execute
293-
old_execute_sync = strawberry_schema.execute_sync
290+
def _patch_schema_execute():
291+
old_execute_sync = strawberry_schema.Schema.execute_sync
292+
old_execute_async = strawberry_schema.Schema.execute
294293

295-
async def _sentry_patched_execute_async(*args, **kwargs):
296-
# type: (Any, Any) -> Union[ExecutionResult, SubscriptionExecutionResult]
297-
result = await old_execute_async(*args, **kwargs)
294+
def _sentry_patched_execute_sync(
295+
self,
296+
query,
297+
variable_values,
298+
context_value,
299+
root_value,
300+
operation_name,
301+
*args,
302+
**kwargs,
303+
):
304+
# type: (strawberry_schema.Schema, Optional[str], Optional[dict[str, Any]], Optional[Any], Optional[Any], Optional[str], Any, Any) -> ExecutionResult
305+
result = old_execute_sync(
306+
self,
307+
query,
308+
variable_values,
309+
context_value,
310+
root_value,
311+
operation_name,
312+
*args,
313+
**kwargs,
314+
)
298315

299316
if sentry_sdk.get_client().get_integration(StrawberryIntegration) is None:
300317
return result
301318

302-
if "execution_context" in kwargs:
303-
scope = sentry_sdk.get_isolation_scope()
304-
event_processor = _make_request_event_processor(kwargs["execution_context"])
305-
scope.add_event_processor(event_processor)
306-
307-
return result
308-
309-
@ensure_integration_enabled(StrawberryIntegration, old_execute_sync)
310-
def _sentry_patched_execute_sync(*args, **kwargs):
311-
# type: (Any, Any) -> ExecutionResult
312-
result = old_execute_sync(*args, **kwargs)
313-
314-
if "execution_context" in kwargs:
315-
scope = sentry_sdk.get_isolation_scope()
316-
event_processor = _make_request_event_processor(kwargs["execution_context"])
317-
scope.add_event_processor(event_processor)
319+
scope = sentry_sdk.get_isolation_scope()
320+
event_processor = _make_request_event_processor(
321+
query, variable_values, operation_name
322+
)
323+
scope.add_event_processor(event_processor)
318324

319325
return result
320326

321-
strawberry_schema.execute = _sentry_patched_execute_async
322-
strawberry_schema.execute_sync = _sentry_patched_execute_sync
323-
327+
async def _sentry_patched_execute_async(
328+
self,
329+
query,
330+
variable_values,
331+
context_value,
332+
root_value,
333+
operation_name,
334+
*args,
335+
**kwargs,
336+
):
337+
# type: (strawberry_schema.Schema, Optional[str], Optional[dict[str, Any]], Optional[Any], Optional[Any], Optional[str], Any, Any) -> ExecutionResult
338+
result = await old_execute_async(
339+
self,
340+
query,
341+
variable_values,
342+
context_value,
343+
root_value,
344+
operation_name,
345+
*args,
346+
**kwargs,
347+
)
324348

325-
def _patch_execute_new():
326-
old_execute_sync = strawberry_schema.Schema.execute_sync
327-
old_execute_async = strawberry_schema.Schema.execute
349+
if sentry_sdk.get_client().get_integration(StrawberryIntegration) is None:
350+
return result
328351

329-
def _sentry_patched_execute_sync(self, query, variable_values, context_value, root_value, operation_name, *args, **kwargs):
330-
# type: (strawberry_schema.Schema, Optional[str], Optional[dict[str, Any]], Optional[Any], Optional[Any], Optional[str]) -> ExecutionResult
331-
pass
352+
scope = sentry_sdk.get_isolation_scope()
353+
event_processor = _make_request_event_processor(
354+
query, variable_values, operation_name
355+
)
356+
scope.add_event_processor(event_processor)
332357

333-
def _sentry_patched_execute_async(...):
334-
pass
358+
return result
335359

336360
strawberry_schema.Schema.execute_sync = _sentry_patched_execute_sync
337361
strawberry_schema.Schema.execute = _sentry_patched_execute_async
338362

363+
339364
def _patch_views():
340365
# type: () -> None
341366
old_async_view_handle_errors = async_base_view.AsyncBaseHTTPView._handle_errors
@@ -381,8 +406,8 @@ def _sentry_patched_handle_errors(self, errors, response_data):
381406
)
382407

383408

384-
def _make_request_event_processor(execution_context):
385-
# type: (ExecutionContext) -> EventProcessor
409+
def _make_request_event_processor(query, variables=None, operation_name=None):
410+
# type: (Optional[str], Optional[dict[str, Any]], Optional[str]) -> EventProcessor
386411

387412
def inner(event, hint):
388413
# type: (Event, dict[str, Any]) -> Event
@@ -392,12 +417,12 @@ def inner(event, hint):
392417
request_data["api_target"] = "graphql"
393418

394419
if not request_data.get("data"):
395-
data = {"query": execution_context.query}
420+
data = {"query": query}
396421

397-
if execution_context.variables:
398-
data["variables"] = execution_context.variables
399-
if execution_context.operation_name:
400-
data["operationName"] = execution_context.operation_name
422+
if variables:
423+
data["variables"] = variables
424+
if operation_name:
425+
data["operationName"] = operation_name
401426

402427
request_data["data"] = data
403428

0 commit comments

Comments
 (0)