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 # type: ignore[import-not-found]
25+ except ImportError :
26+ GraphQLRequest = None
27+
2128except 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, execute moved to 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