1212# See the License for the specific language governing permissions and
1313# limitations under the License.
1414
15- from newrelic .api .time_trace import notice_error , current_trace
1615from newrelic .api .error_trace import ErrorTrace , ErrorTraceWrapper
1716from newrelic .api .function_trace import FunctionTrace , FunctionTraceWrapper
1817from newrelic .api .graphql_trace import GraphQLResolverTrace , GraphQLOperationTrace
18+ from newrelic .api .time_trace import notice_error , current_trace
1919from newrelic .api .transaction import current_transaction
2020from newrelic .common .object_names import callable_name , parse_exc_info
2121from newrelic .common .object_wrapper import function_wrapper , wrap_function_wrapper
22+ from newrelic .core .graphql_utils import graphql_statement
2223from collections import deque
2324from copy import copy
2425
2526
27+ def graphql_version ():
28+ from graphql import __version__ as version
29+ return tuple (int (v ) for v in version .split ("." ))
30+
31+
2632def ignore_graphql_duplicate_exception (exc , val , tb ):
2733 from graphql .error import GraphQLError
2834
@@ -67,17 +73,6 @@ def ignore_graphql_duplicate_exception(exc, val, tb):
6773 return None # Follow original exception matching rules
6874
6975
70- def wrap_execute (wrapped , instance , args , kwargs ):
71- transaction = current_transaction ()
72- if transaction is None :
73- return wrapped (* args , ** kwargs )
74-
75- transaction .set_transaction_name (callable_name (wrapped ), priority = 1 )
76- with GraphQLOperationTrace ():
77- with ErrorTrace (ignore = ignore_graphql_duplicate_exception ):
78- return wrapped (* args , ** kwargs )
79-
80-
8176def wrap_executor_context_init (wrapped , instance , args , kwargs ):
8277 result = wrapped (* args , ** kwargs )
8378
@@ -250,10 +245,7 @@ def wrap_resolve_field(wrapped, instance, args, kwargs):
250245 if transaction is None :
251246 return wrapped (* args , ** kwargs )
252247
253- from graphql import __version__ as version
254- version = tuple (int (v ) for v in version .split ("." ))
255-
256- if version <= (3 , 0 , 0 ):
248+ if graphql_version () <= (3 , 0 , 0 ):
257249 bind_resolve_field = bind_resolve_field_v2
258250 else :
259251 bind_resolve_field = bind_resolve_field_v3
@@ -272,9 +264,46 @@ def wrap_resolve_field(wrapped, instance, args, kwargs):
272264 return wrapped (* args , ** kwargs )
273265
274266
267+ def bind_graphql_impl_query (schema , source , * args , ** kwargs ):
268+ return source
269+
270+
271+ def bind_execute_graphql_query (
272+ schema ,
273+ request_string = "" ,
274+ root = None ,
275+ context = None ,
276+ variables = None ,
277+ operation_name = None ,
278+ middleware = None ,
279+ backend = None ,
280+ ** execute_options ):
281+
282+ return request_string
283+
284+
285+ def wrap_graphql_impl (wrapped , instance , args , kwargs ):
286+ transaction = current_transaction ()
287+
288+ if not transaction :
289+ return wrapped (* args , ** kwargs )
290+
291+ if graphql_version () <= (3 , 0 , 0 ):
292+ bind_query = bind_execute_graphql_query
293+ else :
294+ bind_query = bind_graphql_impl_query
295+
296+ query = bind_query (* args , ** kwargs )
297+ if hasattr (query , "body" ):
298+ query = query .body
299+
300+ with GraphQLOperationTrace () as trace :
301+ trace .statement = graphql_statement (query )
302+ with ErrorTrace (ignore = ignore_graphql_duplicate_exception ):
303+ return wrapped (* args , ** kwargs )
304+
305+
275306def instrument_graphql_execute (module ):
276- if hasattr (module , "execute" ):
277- wrap_function_wrapper (module , "execute" , wrap_execute )
278307 if hasattr (module , "get_field_def" ):
279308 wrap_function_wrapper (module , "get_field_def" , wrap_get_field_def )
280309 if hasattr (module , "ExecutionContext" ):
@@ -323,3 +352,9 @@ def instrument_graphql_error_located_error(module):
323352
324353def instrument_graphql_validate (module ):
325354 wrap_function_wrapper (module , "validate" , wrap_validate )
355+
356+ def instrument_graphql (module ):
357+ if hasattr (module , "graphql_impl" ):
358+ wrap_function_wrapper (module , "graphql_impl" , wrap_graphql_impl )
359+ if hasattr (module , "execute_graphql" ):
360+ wrap_function_wrapper (module , "execute_graphql" , wrap_graphql_impl )
0 commit comments