27
27
raise DidNotEnable ("strawberry-graphql integration requires Python 3.8 or newer" )
28
28
29
29
try :
30
- import strawberry .schema .schema as strawberry_schema # type: ignore
31
30
from strawberry import Schema
32
- from strawberry .extensions import SchemaExtension # type: ignore
33
- from strawberry .extensions .tracing .utils import should_skip_tracing as strawberry_should_skip_tracing # type: ignore
34
- from strawberry .http import async_base_view , sync_base_view # type: ignore
31
+ from strawberry .extensions import SchemaExtension
32
+ from strawberry .extensions .tracing .utils import (
33
+ should_skip_tracing as strawberry_should_skip_tracing ,
34
+ )
35
+ from strawberry .http import async_base_view , sync_base_view
35
36
except ImportError :
36
37
raise DidNotEnable ("strawberry-graphql is not installed" )
37
38
38
39
try :
39
- from strawberry .extensions .tracing import ( # type: ignore
40
+ from strawberry .extensions .tracing import (
40
41
SentryTracingExtension as StrawberrySentryAsyncExtension ,
41
42
SentryTracingExtensionSync as StrawberrySentrySyncExtension ,
42
43
)
47
48
from typing import TYPE_CHECKING
48
49
49
50
if TYPE_CHECKING :
50
- from typing import Any , Callable , Generator , List , Optional , Union
51
- from graphql import GraphQLError , GraphQLResolveInfo # type: ignore
51
+ from typing import Any , Callable , Generator , List , Optional
52
+ from graphql import GraphQLError , GraphQLResolveInfo
52
53
from strawberry .http import GraphQLHTTPResponse
53
- from strawberry .types import ExecutionContext , ExecutionResult , SubscriptionExecutionResult # type: ignore
54
+ from strawberry .types import ExecutionContext
54
55
from sentry_sdk ._types import Event , EventProcessor
55
56
56
57
@@ -78,7 +79,6 @@ def setup_once():
78
79
_check_minimum_version (StrawberryIntegration , version , "strawberry-graphql" )
79
80
80
81
_patch_schema_init ()
81
- _patch_execute ()
82
82
_patch_views ()
83
83
84
84
@@ -124,10 +124,10 @@ def _sentry_patched_schema_init(self, *args, **kwargs):
124
124
125
125
return old_schema_init (self , * args , ** kwargs )
126
126
127
- Schema .__init__ = _sentry_patched_schema_init
127
+ Schema .__init__ = _sentry_patched_schema_init # type: ignore[method-assign]
128
128
129
129
130
- class SentryAsyncExtension (SchemaExtension ): # type: ignore
130
+ class SentryAsyncExtension (SchemaExtension ):
131
131
def __init__ (
132
132
self ,
133
133
* ,
@@ -140,7 +140,7 @@ def __init__(
140
140
@cached_property
141
141
def _resource_name (self ):
142
142
# type: () -> str
143
- query_hash = self .hash_query (self .execution_context .query )
143
+ query_hash = self .hash_query (self .execution_context .query ) # type: ignore
144
144
145
145
if self .execution_context .operation_name :
146
146
return "{}:{}" .format (self .execution_context .operation_name , query_hash )
@@ -180,6 +180,10 @@ def on_operation(self):
180
180
},
181
181
)
182
182
183
+ scope = sentry_sdk .get_isolation_scope ()
184
+ event_processor = _make_request_event_processor (self .execution_context )
185
+ scope .add_event_processor (event_processor )
186
+
183
187
span = sentry_sdk .get_current_span ()
184
188
if span :
185
189
self .graphql_span = span .start_child (
@@ -287,41 +291,6 @@ def resolve(self, _next, root, info, *args, **kwargs):
287
291
return _next (root , info , * args , ** kwargs )
288
292
289
293
290
- def _patch_execute ():
291
- # type: () -> None
292
- old_execute_async = strawberry_schema .execute
293
- old_execute_sync = strawberry_schema .execute_sync
294
-
295
- async def _sentry_patched_execute_async (* args , ** kwargs ):
296
- # type: (Any, Any) -> Union[ExecutionResult, SubscriptionExecutionResult]
297
- result = await old_execute_async (* args , ** kwargs )
298
-
299
- if sentry_sdk .get_client ().get_integration (StrawberryIntegration ) is None :
300
- return result
301
-
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 )
318
-
319
- return result
320
-
321
- strawberry_schema .execute = _sentry_patched_execute_async
322
- strawberry_schema .execute_sync = _sentry_patched_execute_sync
323
-
324
-
325
294
def _patch_views ():
326
295
# type: () -> None
327
296
old_async_view_handle_errors = async_base_view .AsyncBaseHTTPView ._handle_errors
@@ -359,10 +328,10 @@ def _sentry_patched_handle_errors(self, errors, response_data):
359
328
)
360
329
sentry_sdk .capture_event (event , hint = hint )
361
330
362
- async_base_view .AsyncBaseHTTPView ._handle_errors = (
331
+ async_base_view .AsyncBaseHTTPView ._handle_errors = ( # type: ignore[method-assign]
363
332
_sentry_patched_async_view_handle_errors
364
333
)
365
- sync_base_view .SyncBaseHTTPView ._handle_errors = (
334
+ sync_base_view .SyncBaseHTTPView ._handle_errors = ( # type: ignore[method-assign]
366
335
_sentry_patched_sync_view_handle_errors
367
336
)
368
337
@@ -378,8 +347,7 @@ def inner(event, hint):
378
347
request_data ["api_target" ] = "graphql"
379
348
380
349
if not request_data .get ("data" ):
381
- data = {"query" : execution_context .query }
382
-
350
+ data = {"query" : execution_context .query } # type: dict[str, Any]
383
351
if execution_context .variables :
384
352
data ["variables" ] = execution_context .variables
385
353
if execution_context .operation_name :
0 commit comments