Skip to content

Commit ed0f7f7

Browse files
Add type annotations and attach graphene query again
1 parent 37b78cf commit ed0f7f7

File tree

2 files changed

+25
-14
lines changed

2 files changed

+25
-14
lines changed

sentry_sdk/integrations/graphene.py

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121
if TYPE_CHECKING:
2222
from collections.abc import Generator
23-
from typing import Any, Dict, Union
23+
from typing import Any, Dict, Union, Callable
2424
from graphene.language.source import Source # type: ignore
2525
from graphql.execution import ExecutionResult
2626
from graphql.type import GraphQLSchema
@@ -48,7 +48,7 @@ def _patch_graphql():
4848
def _sentry_patched_graphql_sync(schema, source, *args, **kwargs):
4949
# type: (GraphQLSchema, Union[str, Source], Any, Any) -> ExecutionResult
5050
scope = sentry_sdk.get_isolation_scope()
51-
scope.add_event_processor(_event_processor)
51+
scope.add_event_processor(_make_event_processor(source))
5252

5353
with graphql_span(schema, source, kwargs):
5454
result = old_graphql_sync(schema, source, *args, **kwargs)
@@ -75,7 +75,7 @@ async def _sentry_patched_graphql_async(schema, source, *args, **kwargs):
7575
return await old_graphql_async(schema, source, *args, **kwargs)
7676

7777
scope = sentry_sdk.get_isolation_scope()
78-
scope.add_event_processor(_event_processor)
78+
scope.add_event_processor(_make_event_processor(source))
7979

8080
with graphql_span(schema, source, kwargs):
8181
result = await old_graphql_async(schema, source, *args, **kwargs)
@@ -99,16 +99,21 @@ async def _sentry_patched_graphql_async(schema, source, *args, **kwargs):
9999
graphene_schema.graphql = _sentry_patched_graphql_async
100100

101101

102-
def _event_processor(event, hint):
103-
# type: (Event, Dict[str, Any]) -> Event
104-
if should_send_default_pii():
105-
request_info = event.setdefault("request", {})
106-
request_info["api_target"] = "graphql"
102+
def _make_event_processor(source):
103+
# type: (Any) -> Callable[[Event, dict[str, Any]], Event]
104+
def _event_processor(event, hint):
105+
# type: (Event, Dict[str, Any]) -> Event
106+
if should_send_default_pii():
107+
request_info = event.setdefault("request", {})
108+
request_info["api_target"] = "graphql"
109+
request_info["data"] = {"query": source}
107110

108-
elif event.get("request", {}).get("data"):
109-
del event["request"]["data"]
111+
elif event.get("request", {}).get("data"):
112+
del event["request"]["data"]
110113

111-
return event
114+
return event
115+
116+
return _event_processor
112117

113118

114119
@contextmanager

sentry_sdk/integrations/starlette.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -423,33 +423,38 @@ def _is_async_callable(obj):
423423

424424

425425
def _patch_request(request):
426+
# type: (Request) -> None
426427
_original_body = request.body
427428
_original_json = request.json
428429
_original_form = request.form
429430

430431
def restore_original_methods():
432+
# type: () -> None
431433
request.body = _original_body
432434
request.json = _original_json
433435
request.form = _original_form
434436

435437
async def sentry_body():
438+
# type: () -> bytes
436439
request.scope.setdefault("state", {})["sentry_sdk.is_body_cached"] = True
437440
restore_original_methods()
438441
return await _original_body()
439442

440443
async def sentry_json():
444+
# type: () -> Any
441445
request.scope.setdefault("state", {})["sentry_sdk.is_body_cached"] = True
442446
restore_original_methods()
443447
return await _original_json()
444448

445449
async def sentry_form():
450+
# type: () -> Any
446451
request.scope.setdefault("state", {})["sentry_sdk.is_body_cached"] = True
447452
restore_original_methods()
448453
return await _original_form()
449454

450-
request.body = sentry_body
451-
request.json = sentry_json
452-
request.form = sentry_form
455+
request.body = sentry_body # type: ignore
456+
request.json = sentry_json # type: ignore
457+
request.form = sentry_form # type: ignore
453458

454459

455460
def patch_request_response():
@@ -673,6 +678,7 @@ async def extract_request_info(self):
673678
json = await self.json()
674679
if json:
675680
request_info["data"] = json
681+
print("in fastapi json", request_info)
676682
return request_info
677683

678684
# Add form as key/value pairs, if request has form data

0 commit comments

Comments
 (0)