Skip to content

Commit bbf7982

Browse files
github-actions[bot]mabdinuremmettbutler
authored
fix(graphql): resolves errors raised while parsing graphql asts [backport 1.20] (#6990)
Backport e67dcb7 from #6960 to 1.20. Resolves: #6959 ## Checklist - [x] Change(s) are motivated and described in the PR description. - [x] Testing strategy is described if automated tests are not included in the PR. - [x] Risk is outlined (performance impact, potential for breakage, maintainability, etc). - [x] Change is maintainable (easy to change, telemetry, documentation). - [x] [Library release note guidelines](https://ddtrace.readthedocs.io/en/stable/releasenotes.html) are followed. If no release note is required, add label `changelog/no-changelog`. - [x] Documentation is included (in-code, generated user docs, [public corp docs](https://github.com/DataDog/documentation/)). - [x] Backport labels are set (if [applicable](https://ddtrace.readthedocs.io/en/latest/contributing.html#backporting)) ## Reviewer Checklist - [x] Title is accurate. - [x] No unnecessary changes are introduced. - [x] Description motivates each change. - [x] Avoids breaking [API](https://ddtrace.readthedocs.io/en/stable/versioning.html#interfaces) changes unless absolutely necessary. - [x] Testing strategy adequately addresses listed risk(s). - [x] Change is maintainable (easy to change, telemetry, documentation). - [x] Release note makes sense to a user of the library. - [x] Reviewer has explicitly acknowledged and discussed the performance implications of this PR as reported in the benchmarks PR comment. - [x] Backport labels are set in a manner that is consistent with the [release branch maintenance policy](https://ddtrace.readthedocs.io/en/latest/contributing.html#backporting) - [x] If this PR touches code that signs or publishes builds or packages, or handles credentials of any kind, I've requested a review from `@DataDog/security-design-and-guidance`. - [x] This PR doesn't touch any of that. Co-authored-by: Munir Abdinur <[email protected]> Co-authored-by: Emmett Butler <[email protected]>
1 parent 692ad8b commit bbf7982

File tree

4 files changed

+68
-1
lines changed

4 files changed

+68
-1
lines changed

ddtrace/contrib/graphql/patch.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@ def _get_source_str(obj):
283283
source_str = obj
284284
elif isinstance(obj, Source):
285285
source_str = obj.body
286-
elif isinstance(obj, Document):
286+
elif isinstance(obj, Document) and obj.loc is not None:
287287
source_str = obj.loc.source.body
288288
else:
289289
source_str = ""
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
fixes:
3+
- |
4+
graphql: Resolves ``AttributeError`` raised while parsing graphql Documents where AST Location is None.

tests/contrib/graphql/test_graphql.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,14 @@ def test_graphql_v2_with_document(test_schema, test_source_str):
115115
assert result.data == {"hello": "friend"}
116116

117117

118+
@snapshot()
119+
def test_graphql_with_document_with_no_location(test_schema, test_source_str):
120+
source = graphql.language.source.Source(test_source_str, "GraphQL request")
121+
document_ast = graphql.language.parser.parse(source, no_location=True)
122+
result = graphql.execute(test_schema, document_ast)
123+
assert result.data == {"hello": "friend"}
124+
125+
118126
@snapshot(token_override="tests.contrib.graphql.test_graphql.test_graphql")
119127
@pytest.mark.skipif(graphql_version < (3, 0), reason="graphql.graphql_sync is NOT suppoerted in v2.0")
120128
def test_graphql_sync(test_schema, test_source_str):
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
[[
2+
{
3+
"name": "graphql.parse",
4+
"service": "graphql",
5+
"resource": "graphql.parse",
6+
"trace_id": 0,
7+
"span_id": 1,
8+
"parent_id": 0,
9+
"type": "graphql",
10+
"meta": {
11+
"_dd.base_service": "",
12+
"_dd.p.dm": "-0",
13+
"component": "graphql",
14+
"graphql.source": "query HELLO { hello }",
15+
"language": "python",
16+
"runtime-id": "630b203d91914ec1bf98fe21da63344d"
17+
},
18+
"metrics": {
19+
"_dd.top_level": 1,
20+
"_dd.tracer_kr": 1.0,
21+
"_sampling_priority_v1": 1,
22+
"process_id": 29253
23+
},
24+
"duration": 284000,
25+
"start": 1695228158961101000
26+
}],
27+
[
28+
{
29+
"name": "graphql.execute",
30+
"service": "graphql",
31+
"resource": "graphql.execute",
32+
"trace_id": 1,
33+
"span_id": 1,
34+
"parent_id": 0,
35+
"type": "graphql",
36+
"meta": {
37+
"_dd.base_service": "",
38+
"_dd.p.dm": "-0",
39+
"component": "graphql",
40+
"graphql.operation.name": "HELLO",
41+
"graphql.operation.type": "query",
42+
"graphql.source": "",
43+
"language": "python",
44+
"runtime-id": "630b203d91914ec1bf98fe21da63344d"
45+
},
46+
"metrics": {
47+
"_dd.measured": 1,
48+
"_dd.top_level": 1,
49+
"_dd.tracer_kr": 1.0,
50+
"_sampling_priority_v1": 1,
51+
"process_id": 29253
52+
},
53+
"duration": 336000,
54+
"start": 1695228158977945000
55+
}]]

0 commit comments

Comments
 (0)