Skip to content

Commit 5747863

Browse files
authored
feat(integrations): Support gql 4.0-style execute (#4779)
gql 4.0 [changed](https://github.com/graphql-python/gql/pull/556/files) the signature of the `execute` function which we were patching. Instead of a `DocumentNode` it now gets a `GraphQLRequest` (which contains the `document` attribute with the `DocumentNode`). This means we need to update the way we're extracting additional data in the event processor.
1 parent 16f2c3d commit 5747863

File tree

1 file changed

+22
-5
lines changed

1 file changed

+22
-5
lines changed

sentry_sdk/integrations/gql.py

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,13 @@
1818
)
1919
from gql.transport import Transport, AsyncTransport # type: ignore[import-not-found]
2020
from gql.transport.exceptions import TransportQueryError # type: ignore[import-not-found]
21+
22+
try:
23+
# gql 4.0+
24+
from gql import GraphQLRequest
25+
except ImportError:
26+
GraphQLRequest = None
27+
2128
except ImportError:
2229
raise DidNotEnable("gql is not installed")
2330

@@ -92,13 +99,13 @@ def _patch_execute():
9299
real_execute = gql.Client.execute
93100

94101
@ensure_integration_enabled(GQLIntegration, real_execute)
95-
def sentry_patched_execute(self, document, *args, **kwargs):
102+
def sentry_patched_execute(self, document_or_request, *args, **kwargs):
96103
# type: (gql.Client, DocumentNode, Any, Any) -> Any
97104
scope = sentry_sdk.get_isolation_scope()
98-
scope.add_event_processor(_make_gql_event_processor(self, document))
105+
scope.add_event_processor(_make_gql_event_processor(self, document_or_request))
99106

100107
try:
101-
return real_execute(self, document, *args, **kwargs)
108+
return real_execute(self, document_or_request, *args, **kwargs)
102109
except TransportQueryError as e:
103110
event, hint = event_from_exception(
104111
e,
@@ -112,8 +119,8 @@ def sentry_patched_execute(self, document, *args, **kwargs):
112119
gql.Client.execute = sentry_patched_execute
113120

114121

115-
def _make_gql_event_processor(client, document):
116-
# type: (gql.Client, DocumentNode) -> EventProcessor
122+
def _make_gql_event_processor(client, document_or_request):
123+
# type: (gql.Client, Union[DocumentNode, gql.GraphQLRequest]) -> EventProcessor
117124
def processor(event, hint):
118125
# type: (Event, dict[str, Any]) -> Event
119126
try:
@@ -130,6 +137,16 @@ def processor(event, hint):
130137
)
131138

132139
if should_send_default_pii():
140+
if GraphQLRequest is not None and isinstance(
141+
document_or_request, GraphQLRequest
142+
):
143+
# In v4.0.0, gql moved to using GraphQLRequest instead of
144+
# DocumentNode in execute
145+
# https://github.com/graphql-python/gql/pull/556
146+
document = document_or_request.document
147+
else:
148+
document = document_or_request
149+
133150
request["data"] = _data_from_document(document)
134151
contexts = event.setdefault("contexts", {})
135152
response = contexts.setdefault("response", {})

0 commit comments

Comments
 (0)