From 7d2856d0fd3816c50a92b6a10adf22d90969d7b6 Mon Sep 17 00:00:00 2001 From: Sahil Jolly Date: Mon, 12 Jun 2023 12:41:54 -0700 Subject: [PATCH 1/2] Defensively check GQL path parts in framework_graphql --- newrelic/hooks/framework_graphql.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/newrelic/hooks/framework_graphql.py b/newrelic/hooks/framework_graphql.py index d261b2e9fd..e21587664e 100644 --- a/newrelic/hooks/framework_graphql.py +++ b/newrelic/hooks/framework_graphql.py @@ -133,7 +133,7 @@ def wrap_execute_operation(wrapped, instance, args, kwargs): trace.operation_type = get_node_value(operation, "operation", "name").lower() or "" if operation.selection_set is not None: - fields = operation.selection_set.selections + fields = [f for f in operation.selection_set.selections if f] # Ignore transactions for introspection queries if not (transaction.settings and transaction.settings.instrumentation.graphql.capture_introspection_queries): # If all selected fields are introspection fields @@ -141,7 +141,8 @@ def wrap_execute_operation(wrapped, instance, args, kwargs): ignore_transaction() fragments = execution_context.fragments - trace.deepest_path = ".".join(traverse_deepest_unique_path(fields, fragments)) or "" + path_parts = [p for p in traverse_deepest_unique_path(fields, fragments) if p] + trace.deepest_path = ".".join(path_parts) if path_parts else "" transaction.set_transaction_name(callable_name(wrapped), "GraphQL", priority=11) result = wrapped(*args, **kwargs) @@ -417,7 +418,7 @@ def bind_execute_graphql_query( operation_name=None, middleware=None, backend=None, - **execute_options + **execute_options, ): return schema, request_string From ad06e76145c855a9c5dfe036701a6d8d20702905 Mon Sep 17 00:00:00 2001 From: Sahil Jolly Date: Mon, 26 Jun 2023 16:52:49 -0700 Subject: [PATCH 2/2] add test query reproducing scenario --- tests/framework_graphql/test_application.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/tests/framework_graphql/test_application.py b/tests/framework_graphql/test_application.py index dd49ee37f1..5c5fd14199 100644 --- a/tests/framework_graphql/test_application.py +++ b/tests/framework_graphql/test_application.py @@ -494,6 +494,11 @@ def _test(): "{ library(index: 0) { book { ...MyFragment } magazine { ...MagFragment } } } fragment MyFragment on Book { author { first_name } } fragment MagFragment on Magazine { name }", "/library", ), + # fragement on type union + ( + '{ search(contains: "A") { ...MyFragment } fragment MyFragment on Item { __typename ... on Book { name } } }', + "/search.name", + ), ]