From 6a3c2161acb1d617a2b1cd0bdd36045791d75093 Mon Sep 17 00:00:00 2001 From: Munir Abdinur Date: Wed, 20 Sep 2023 11:12:06 -0400 Subject: [PATCH 1/3] add regression test --- tests/contrib/graphql/test_graphql.py | 8 +++ ...raphql_with_document_with_no_location.json | 68 +++++++++++++++++++ 2 files changed, 76 insertions(+) create mode 100644 tests/snapshots/tests.contrib.graphql.test_graphql.test_graphql_with_document_with_no_location.json diff --git a/tests/contrib/graphql/test_graphql.py b/tests/contrib/graphql/test_graphql.py index 72ffe363e7f..331d19fdd16 100644 --- a/tests/contrib/graphql/test_graphql.py +++ b/tests/contrib/graphql/test_graphql.py @@ -115,6 +115,14 @@ def test_graphql_v2_with_document(test_schema, test_source_str): assert result.data == {"hello": "friend"} +@snapshot() +def test_graphql_with_document_with_no_location(test_schema, test_source_str, test_middleware): + source = graphql.language.source.Source(test_source_str, "GraphQL request") + document_ast = graphql.language.parser.parse(source, no_location=True) + result = graphql.execute(test_schema, document_ast, middleware=[test_middleware]) + assert result.data == {"hello": "friend"} + + @snapshot(token_override="tests.contrib.graphql.test_graphql.test_graphql") @pytest.mark.skipif(graphql_version < (3, 0), reason="graphql.graphql_sync is NOT suppoerted in v2.0") def test_graphql_sync(test_schema, test_source_str): diff --git a/tests/snapshots/tests.contrib.graphql.test_graphql.test_graphql_with_document_with_no_location.json b/tests/snapshots/tests.contrib.graphql.test_graphql.test_graphql_with_document_with_no_location.json new file mode 100644 index 00000000000..5d2acbb08c7 --- /dev/null +++ b/tests/snapshots/tests.contrib.graphql.test_graphql.test_graphql_with_document_with_no_location.json @@ -0,0 +1,68 @@ +[[ + { + "name": "graphql.parse", + "service": "graphql", + "resource": "graphql.parse", + "trace_id": 0, + "span_id": 1, + "parent_id": 0, + "type": "graphql", + "meta": { + "_dd.base_service": "", + "_dd.p.dm": "-0", + "component": "graphql", + "graphql.source": "query HELLO { hello }", + "language": "python", + "runtime-id": "266304fcdb694d519fb71ecd23eba240" + }, + "metrics": { + "_dd.top_level": 1, + "_dd.tracer_kr": 1.0, + "_sampling_priority_v1": 1, + "process_id": 95457 + }, + "duration": 396000, + "start": 1695222423000392000 + }], +[ + { + "name": "graphql.execute", + "service": "graphql", + "resource": "query HELLO { hello }", + "trace_id": 1, + "span_id": 1, + "parent_id": 0, + "type": "graphql", + "meta": { + "_dd.base_service": "", + "_dd.p.dm": "-0", + "component": "graphql", + "graphql.operation.name": "HELLO", + "graphql.operation.type": "query", + "graphql.source": "query HELLO { hello }", + "language": "python", + "runtime-id": "266304fcdb694d519fb71ecd23eba240" + }, + "metrics": { + "_dd.measured": 1, + "_dd.top_level": 1, + "_dd.tracer_kr": 1.0, + "_sampling_priority_v1": 1, + "process_id": 95457 + }, + "duration": 384000, + "start": 1695222423018277000 + }, + { + "name": "test_middleware", + "service": "graphql", + "resource": "test_middleware", + "trace_id": 1, + "span_id": 2, + "parent_id": 1, + "meta": { + "_dd.base_service": "" + }, + "duration": 16000, + "start": 1695222423018620000 + }]] From d6ea319cc6798101b6abd7615dff45efec173299 Mon Sep 17 00:00:00 2001 From: Munir Abdinur Date: Wed, 20 Sep 2023 11:21:48 -0400 Subject: [PATCH 2/3] add release note and fix --- ddtrace/contrib/graphql/patch.py | 2 +- ...graphql-document-fix-ef580e19ff2c4db4.yaml | 4 +++ tests/contrib/graphql/test_graphql.py | 4 +-- ...raphql_with_document_with_no_location.json | 35 ++++++------------- 4 files changed, 18 insertions(+), 27 deletions(-) create mode 100644 releasenotes/notes/graphql-document-fix-ef580e19ff2c4db4.yaml diff --git a/ddtrace/contrib/graphql/patch.py b/ddtrace/contrib/graphql/patch.py index 735d3b7bdb0..f7369b2e40d 100644 --- a/ddtrace/contrib/graphql/patch.py +++ b/ddtrace/contrib/graphql/patch.py @@ -283,7 +283,7 @@ def _get_source_str(obj): source_str = obj elif isinstance(obj, Source): source_str = obj.body - elif isinstance(obj, Document): + elif isinstance(obj, Document) and obj.loc is not None: source_str = obj.loc.source.body else: source_str = "" diff --git a/releasenotes/notes/graphql-document-fix-ef580e19ff2c4db4.yaml b/releasenotes/notes/graphql-document-fix-ef580e19ff2c4db4.yaml new file mode 100644 index 00000000000..3742473569b --- /dev/null +++ b/releasenotes/notes/graphql-document-fix-ef580e19ff2c4db4.yaml @@ -0,0 +1,4 @@ +--- +fixes: + - | + graphql: Resolves ``AttributeError`` raised while parsing graphql Documents where AST Location is None diff --git a/tests/contrib/graphql/test_graphql.py b/tests/contrib/graphql/test_graphql.py index 331d19fdd16..c5ce77f894d 100644 --- a/tests/contrib/graphql/test_graphql.py +++ b/tests/contrib/graphql/test_graphql.py @@ -116,10 +116,10 @@ def test_graphql_v2_with_document(test_schema, test_source_str): @snapshot() -def test_graphql_with_document_with_no_location(test_schema, test_source_str, test_middleware): +def test_graphql_with_document_with_no_location(test_schema, test_source_str): source = graphql.language.source.Source(test_source_str, "GraphQL request") document_ast = graphql.language.parser.parse(source, no_location=True) - result = graphql.execute(test_schema, document_ast, middleware=[test_middleware]) + result = graphql.execute(test_schema, document_ast) assert result.data == {"hello": "friend"} diff --git a/tests/snapshots/tests.contrib.graphql.test_graphql.test_graphql_with_document_with_no_location.json b/tests/snapshots/tests.contrib.graphql.test_graphql.test_graphql_with_document_with_no_location.json index 5d2acbb08c7..2c9345ccc06 100644 --- a/tests/snapshots/tests.contrib.graphql.test_graphql.test_graphql_with_document_with_no_location.json +++ b/tests/snapshots/tests.contrib.graphql.test_graphql.test_graphql_with_document_with_no_location.json @@ -13,22 +13,22 @@ "component": "graphql", "graphql.source": "query HELLO { hello }", "language": "python", - "runtime-id": "266304fcdb694d519fb71ecd23eba240" + "runtime-id": "630b203d91914ec1bf98fe21da63344d" }, "metrics": { "_dd.top_level": 1, "_dd.tracer_kr": 1.0, "_sampling_priority_v1": 1, - "process_id": 95457 + "process_id": 29253 }, - "duration": 396000, - "start": 1695222423000392000 + "duration": 284000, + "start": 1695228158961101000 }], [ { "name": "graphql.execute", "service": "graphql", - "resource": "query HELLO { hello }", + "resource": "graphql.execute", "trace_id": 1, "span_id": 1, "parent_id": 0, @@ -39,30 +39,17 @@ "component": "graphql", "graphql.operation.name": "HELLO", "graphql.operation.type": "query", - "graphql.source": "query HELLO { hello }", + "graphql.source": "", "language": "python", - "runtime-id": "266304fcdb694d519fb71ecd23eba240" + "runtime-id": "630b203d91914ec1bf98fe21da63344d" }, "metrics": { "_dd.measured": 1, "_dd.top_level": 1, "_dd.tracer_kr": 1.0, "_sampling_priority_v1": 1, - "process_id": 95457 + "process_id": 29253 }, - "duration": 384000, - "start": 1695222423018277000 - }, - { - "name": "test_middleware", - "service": "graphql", - "resource": "test_middleware", - "trace_id": 1, - "span_id": 2, - "parent_id": 1, - "meta": { - "_dd.base_service": "" - }, - "duration": 16000, - "start": 1695222423018620000 - }]] + "duration": 336000, + "start": 1695228158977945000 + }]] From 5f0398fbbc82e52428c3bcd3956739f17d0e8a7f Mon Sep 17 00:00:00 2001 From: Zachary Groves <32471391+ZStriker19@users.noreply.github.com> Date: Thu, 21 Sep 2023 16:48:31 -0400 Subject: [PATCH 3/3] Update releasenotes/notes/graphql-document-fix-ef580e19ff2c4db4.yaml --- releasenotes/notes/graphql-document-fix-ef580e19ff2c4db4.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/releasenotes/notes/graphql-document-fix-ef580e19ff2c4db4.yaml b/releasenotes/notes/graphql-document-fix-ef580e19ff2c4db4.yaml index 3742473569b..1f645234942 100644 --- a/releasenotes/notes/graphql-document-fix-ef580e19ff2c4db4.yaml +++ b/releasenotes/notes/graphql-document-fix-ef580e19ff2c4db4.yaml @@ -1,4 +1,4 @@ --- fixes: - | - graphql: Resolves ``AttributeError`` raised while parsing graphql Documents where AST Location is None + graphql: Resolves ``AttributeError`` raised while parsing graphql Documents where AST Location is None.