Skip to content

Commit 1e4877b

Browse files
authored
Update version parsing to prevent errors with graphql-core pre-release versions. (#329)
* Update version parsing to prevent errors with pre-release versions. * [Mega-Linter] Apply linters fixes Co-authored-by: umaannamalai <[email protected]>
1 parent 95bc31a commit 1e4877b

File tree

1 file changed

+25
-45
lines changed

1 file changed

+25
-45
lines changed

newrelic/hooks/framework_graphql.py

Lines changed: 25 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,16 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15+
from collections import deque
16+
1517
from newrelic.api.error_trace import ErrorTrace
1618
from newrelic.api.function_trace import FunctionTrace
17-
from newrelic.api.graphql_trace import GraphQLResolverTrace, GraphQLOperationTrace
18-
from newrelic.api.time_trace import notice_error, current_trace
19+
from newrelic.api.graphql_trace import GraphQLOperationTrace, GraphQLResolverTrace
20+
from newrelic.api.time_trace import current_trace, notice_error
1921
from newrelic.api.transaction import current_transaction, ignore_transaction
2022
from newrelic.common.object_names import callable_name, parse_exc_info
2123
from newrelic.common.object_wrapper import function_wrapper, wrap_function_wrapper
2224
from newrelic.core.graphql_utils import graphql_statement
23-
from collections import deque
24-
2525

2626
GRAPHQL_IGNORED_FIELDS = frozenset(("id", "__typename"))
2727
GRAPHQL_INTROSPECTION_FIELDS = frozenset(("__schema", "__type"))
@@ -30,7 +30,8 @@
3030
def graphql_version():
3131
from graphql import __version__ as version
3232

33-
return tuple(int(v) for v in version.split("."))
33+
# Take first two values in version to avoid ValueErrors with pre-releases (ex: 3.2.0a0)
34+
return tuple(int(v) for v in version.split(".")[:2])
3435

3536

3637
def ignore_graphql_duplicate_exception(exc, val, tb):
@@ -99,7 +100,7 @@ def wrap_execute_operation(wrapped, instance, args, kwargs):
99100
except TypeError:
100101
return wrapped(*args, **kwargs)
101102

102-
if graphql_version() < (3, 0, 0):
103+
if graphql_version() < (3, 0):
103104
execution_context = args[0]
104105
else:
105106
execution_context = instance
@@ -157,10 +158,8 @@ def is_fragment(field):
157158
try:
158159
from graphql.language.ast import FragmentSpread, InlineFragment
159160
except ImportError:
160-
from graphql import (
161-
FragmentSpreadNode as FragmentSpread,
162-
InlineFragmentNode as InlineFragment,
163-
)
161+
from graphql import FragmentSpreadNode as FragmentSpread
162+
from graphql import InlineFragmentNode as InlineFragment
164163

165164
_fragment_types = (InlineFragment, FragmentSpread)
166165

@@ -182,9 +181,7 @@ def is_named_fragment(field):
182181

183182

184183
def filter_ignored_fields(fields):
185-
filtered_fields = [
186-
f for f in fields if get_node_value(f, "name") not in GRAPHQL_IGNORED_FIELDS
187-
]
184+
filtered_fields = [f for f in fields if get_node_value(f, "name") not in GRAPHQL_IGNORED_FIELDS]
188185
return filtered_fields
189186

190187

@@ -209,9 +206,7 @@ def traverse_deepest_unique_path(fields, fragments):
209206

210207
# list(fragments.values())[0] 's index is OK because the previous line
211208
# ensures that there is only one field in the list
212-
full_fragment_selection_set = list(fragments.values())[
213-
0
214-
].selection_set.selections
209+
full_fragment_selection_set = list(fragments.values())[0].selection_set.selections
215210
fragment_selection_set = filter_ignored_fields(full_fragment_selection_set)
216211

217212
if len(fragment_selection_set) != 1:
@@ -244,9 +239,7 @@ def wrap_get_middleware_resolvers(wrapped, instance, args, kwargs):
244239
except TypeError:
245240
return wrapped(*args, **kwargs)
246241

247-
middlewares = [
248-
wrap_middleware(m) if not hasattr(m, "_nr_wrapped") else m for m in middlewares
249-
]
242+
middlewares = [wrap_middleware(m) if not hasattr(m, "_nr_wrapped") else m for m in middlewares]
250243
for m in middlewares:
251244
m._nr_wrapped = True
252245

@@ -356,9 +349,7 @@ def bind_resolve_field_v3(parent_type, source, field_nodes, path):
356349
return parent_type, field_nodes, path
357350

358351

359-
def bind_resolve_field_v2(
360-
exe_context, parent_type, source, field_asts, parent_info, field_path
361-
):
352+
def bind_resolve_field_v2(exe_context, parent_type, source, field_asts, parent_info, field_path):
362353
return parent_type, field_asts, field_path
363354

364355

@@ -367,7 +358,7 @@ def wrap_resolve_field(wrapped, instance, args, kwargs):
367358
if transaction is None:
368359
return wrapped(*args, **kwargs)
369360

370-
if graphql_version() <= (3, 0, 0):
361+
if graphql_version() < (3, 0):
371362
bind_resolve_field = bind_resolve_field_v2
372363
else:
373364
bind_resolve_field = bind_resolve_field_v3
@@ -419,12 +410,13 @@ def wrap_graphql_impl(wrapped, instance, args, kwargs):
419410
if not transaction:
420411
return wrapped(*args, **kwargs)
421412

422-
version = graphql_version()
423-
framework_version = ".".join(map(str, version))
413+
from graphql import __version__ as version
414+
415+
version = tuple(v for v in version.split("."))
416+
framework_version = ".".join(version)
424417

425418
transaction.add_framework_info(name="GraphQL", version=framework_version)
426-
427-
if graphql_version() <= (3, 0, 0):
419+
if graphql_version() < (3, 0):
428420
bind_query = bind_execute_graphql_query
429421
else:
430422
bind_query = bind_graphql_impl_query
@@ -450,17 +442,11 @@ def instrument_graphql_execute(module):
450442
if hasattr(module, "get_field_def"):
451443
wrap_function_wrapper(module, "get_field_def", wrap_get_field_def)
452444
if hasattr(module, "ExecutionContext"):
453-
wrap_function_wrapper(
454-
module, "ExecutionContext.__init__", wrap_executor_context_init
455-
)
445+
wrap_function_wrapper(module, "ExecutionContext.__init__", wrap_executor_context_init)
456446
if hasattr(module.ExecutionContext, "resolve_field"):
457-
wrap_function_wrapper(
458-
module, "ExecutionContext.resolve_field", wrap_resolve_field
459-
)
447+
wrap_function_wrapper(module, "ExecutionContext.resolve_field", wrap_resolve_field)
460448
if hasattr(module.ExecutionContext, "execute_operation"):
461-
wrap_function_wrapper(
462-
module, "ExecutionContext.execute_operation", wrap_execute_operation
463-
)
449+
wrap_function_wrapper(module, "ExecutionContext.execute_operation", wrap_execute_operation)
464450

465451
if hasattr(module, "resolve_field"):
466452
wrap_function_wrapper(module, "resolve_field", wrap_resolve_field)
@@ -471,20 +457,14 @@ def instrument_graphql_execute(module):
471457

472458
def instrument_graphql_execution_utils(module):
473459
if hasattr(module, "ExecutionContext"):
474-
wrap_function_wrapper(
475-
module, "ExecutionContext.__init__", wrap_executor_context_init
476-
)
460+
wrap_function_wrapper(module, "ExecutionContext.__init__", wrap_executor_context_init)
477461

478462

479463
def instrument_graphql_execution_middleware(module):
480464
if hasattr(module, "get_middleware_resolvers"):
481-
wrap_function_wrapper(
482-
module, "get_middleware_resolvers", wrap_get_middleware_resolvers
483-
)
465+
wrap_function_wrapper(module, "get_middleware_resolvers", wrap_get_middleware_resolvers)
484466
if hasattr(module, "MiddlewareManager"):
485-
wrap_function_wrapper(
486-
module, "MiddlewareManager.get_field_resolver", wrap_get_field_resolver
487-
)
467+
wrap_function_wrapper(module, "MiddlewareManager.get_field_resolver", wrap_get_field_resolver)
488468

489469

490470
def instrument_graphql_error_located_error(module):

0 commit comments

Comments
 (0)