Skip to content

Commit de7854e

Browse files
fix(gql): Revert signature change of patched gql.Client.execute (#5289)
Restore signature of patched `gql.Client.execute`. Prior to v4.0.0, `gql.Client.execute` accepted a `document` parameter that users can supply with a keyword argument. The signature change in commit 5747863 therefore introduced a regression by renaming the parameter.
1 parent 4c4f260 commit de7854e

File tree

2 files changed

+54
-11
lines changed

2 files changed

+54
-11
lines changed

sentry_sdk/integrations/gql.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,18 +95,23 @@ def _request_info_from_transport(
9595
def _patch_execute() -> None:
9696
real_execute = gql.Client.execute
9797

98+
# Maintain signature for backwards compatibility.
99+
# gql.Client.execute() accepts a positional-only "request"
100+
# parameter with version 4.0.0.
98101
@ensure_integration_enabled(GQLIntegration, real_execute)
99102
def sentry_patched_execute(
100103
self: "gql.Client",
101-
document_or_request: "DocumentNode",
104+
document: "DocumentNode",
102105
*args: "Any",
103106
**kwargs: "Any",
104107
) -> "Any":
105108
scope = sentry_sdk.get_isolation_scope()
106-
scope.add_event_processor(_make_gql_event_processor(self, document_or_request))
109+
# document is a gql.GraphQLRequest with gql v4.0.0.
110+
scope.add_event_processor(_make_gql_event_processor(self, document))
107111

108112
try:
109-
return real_execute(self, document_or_request, *args, **kwargs)
113+
# document is a gql.GraphQLRequest with gql v4.0.0.
114+
return real_execute(self, document, *args, **kwargs)
110115
except TransportQueryError as e:
111116
event, hint = event_from_exception(
112117
e,

tests/integrations/gql/test_gql.py

Lines changed: 46 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,13 @@
33
import responses
44
from gql import gql
55
from gql import Client
6+
from gql import __version__
67
from gql.transport.exceptions import TransportQueryError
78
from gql.transport.requests import RequestsHTTPTransport
89
from sentry_sdk.integrations.gql import GQLIntegration
10+
from sentry_sdk.utils import parse_version
11+
12+
GQL_VERSION = parse_version(__version__)
913

1014

1115
@responses.activate
@@ -32,7 +36,36 @@ def _execute_mock_query(response_json):
3236
return client.execute(query)
3337

3438

35-
def _make_erroneous_query(capture_events):
39+
@responses.activate
40+
def _execute_mock_query_with_keyword_document(response_json):
41+
url = "http://example.com/graphql"
42+
query_string = """
43+
query Example {
44+
example
45+
}
46+
"""
47+
48+
# Mock the GraphQL server response
49+
responses.add(
50+
method=responses.POST,
51+
url=url,
52+
json=response_json,
53+
status=200,
54+
)
55+
56+
transport = RequestsHTTPTransport(url=url)
57+
client = Client(transport=transport)
58+
query = gql(query_string)
59+
60+
return client.execute(document=query)
61+
62+
63+
_execute_query_funcs = [_execute_mock_query]
64+
if GQL_VERSION < (4,):
65+
_execute_query_funcs.append(_execute_mock_query_with_keyword_document)
66+
67+
68+
def _make_erroneous_query(capture_events, execute_query):
3669
"""
3770
Make an erroneous GraphQL query, and assert that the error was reraised, that
3871
exactly one event was recorded, and that the exception recorded was a
@@ -42,7 +75,7 @@ def _make_erroneous_query(capture_events):
4275
response_json = {"errors": ["something bad happened"]}
4376

4477
with pytest.raises(TransportQueryError):
45-
_execute_mock_query(response_json)
78+
execute_query(response_json)
4679

4780
assert len(events) == 1, (
4881
"the sdk captured %d events, but 1 event was expected" % len(events)
@@ -67,7 +100,8 @@ def test_gql_init(sentry_init):
67100
sentry_init(integrations=[GQLIntegration()])
68101

69102

70-
def test_real_gql_request_no_error(sentry_init, capture_events):
103+
@pytest.mark.parametrize("execute_query", _execute_query_funcs)
104+
def test_real_gql_request_no_error(sentry_init, capture_events, execute_query):
71105
"""
72106
Integration test verifying that the GQLIntegration works as expected with successful query.
73107
"""
@@ -77,7 +111,7 @@ def test_real_gql_request_no_error(sentry_init, capture_events):
77111
response_data = {"example": "This is the example"}
78112
response_json = {"data": response_data}
79113

80-
result = _execute_mock_query(response_json)
114+
result = execute_query(response_json)
81115

82116
assert result == response_data, (
83117
"client.execute returned a different value from what it received from the server"
@@ -87,27 +121,31 @@ def test_real_gql_request_no_error(sentry_init, capture_events):
87121
)
88122

89123

90-
def test_real_gql_request_with_error_no_pii(sentry_init, capture_events):
124+
@pytest.mark.parametrize("execute_query", _execute_query_funcs)
125+
def test_real_gql_request_with_error_no_pii(sentry_init, capture_events, execute_query):
91126
"""
92127
Integration test verifying that the GQLIntegration works as expected with query resulting
93128
in a GraphQL error, and that PII is not sent.
94129
"""
95130
sentry_init(integrations=[GQLIntegration()])
96131

97-
event = _make_erroneous_query(capture_events)
132+
event = _make_erroneous_query(capture_events, execute_query)
98133

99134
assert "data" not in event["request"]
100135
assert "response" not in event["contexts"]
101136

102137

103-
def test_real_gql_request_with_error_with_pii(sentry_init, capture_events):
138+
@pytest.mark.parametrize("execute_query", _execute_query_funcs)
139+
def test_real_gql_request_with_error_with_pii(
140+
sentry_init, capture_events, execute_query
141+
):
104142
"""
105143
Integration test verifying that the GQLIntegration works as expected with query resulting
106144
in a GraphQL error, and that PII is not sent.
107145
"""
108146
sentry_init(integrations=[GQLIntegration()], send_default_pii=True)
109147

110-
event = _make_erroneous_query(capture_events)
148+
event = _make_erroneous_query(capture_events, execute_query)
111149

112150
assert "data" in event["request"]
113151
assert "response" in event["contexts"]

0 commit comments

Comments
 (0)