Skip to content

Commit 56f18dd

Browse files
authored
chore(graphql): improve path logic in graphql (#7557)
1 parent 46aa1f0 commit 56f18dd

File tree

3 files changed

+25
-38
lines changed

3 files changed

+25
-38
lines changed

packages/datadog-instrumentations/src/graphql.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,7 @@ function assertField (rootCtx, info, args) {
277277
let field = fields[pathString]
278278

279279
if (!field) {
280-
const fieldCtx = { info, rootCtx, args }
280+
const fieldCtx = { info, rootCtx, args, path, pathString }
281281
startResolveCh.publish(fieldCtx)
282282
field = fields[pathString] = {
283283
error: null,

packages/datadog-plugin-graphql/src/execute.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,8 @@ function addVariableTags (config, span, variableValues) {
5858

5959
if (variableValues && config.variables) {
6060
const variables = config.variables(variableValues)
61-
for (const param in variables) {
62-
tags[`graphql.variables.${param}`] = variables[param]
61+
for (const [param, value] of Object.entries(variables)) {
62+
tags[`graphql.variables.${param}`] = value
6363
}
6464
}
6565

packages/datadog-plugin-graphql/src/resolve.js

Lines changed: 22 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@ class GraphQLResolvePlugin extends TracingPlugin {
1010
static operation = 'resolve'
1111

1212
start (fieldCtx) {
13-
const { info, rootCtx, args } = fieldCtx
13+
const { info, rootCtx, args, path: pathAsArray, pathString } = fieldCtx
1414

15-
const path = getPath(info, this.config)
15+
const path = getPath(this.config, pathAsArray)
1616

1717
// we need to get the parent span to the field if it exists for correct span parenting
1818
// of nested fields
19-
const parentField = getParentField(rootCtx, pathToArray(info && info.path))
19+
const parentField = getParentField(rootCtx, pathString)
2020
const childOf = parentField?.ctx?.currentStore?.span
2121

2222
fieldCtx.parent = parentField
@@ -76,9 +76,9 @@ class GraphQLResolvePlugin extends TracingPlugin {
7676
super(...args)
7777

7878
this.addTraceSub('updateField', (ctx) => {
79-
const { field, info, error } = ctx
79+
const { field, error, path: pathAsArray } = ctx
8080

81-
const path = getPath(info, this.config)
81+
const path = getPath(this.config, pathAsArray)
8282

8383
if (!shouldInstrument(this.config, path)) return
8484

@@ -118,28 +118,14 @@ function shouldInstrument (config, path) {
118118
return config.depth < 0 || config.depth >= depth
119119
}
120120

121-
function getPath (info, config) {
122-
const responsePathAsArray = config.collapse
123-
? withCollapse(pathToArray)
124-
: pathToArray
125-
return responsePathAsArray(info && info.path)
121+
function getPath (config, pathAsArray) {
122+
return config.collapse
123+
? withCollapse(pathAsArray)
124+
: pathAsArray
126125
}
127126

128-
function pathToArray (path) {
129-
const flattened = []
130-
let curr = path
131-
while (curr) {
132-
flattened.push(curr.key)
133-
curr = curr.prev
134-
}
135-
return flattened.reverse()
136-
}
137-
138-
function withCollapse (responsePathAsArray) {
139-
return function () {
140-
return responsePathAsArray.apply(this, arguments)
141-
.map(segment => typeof segment === 'number' ? '*' : segment)
142-
}
127+
function withCollapse (pathAsArray) {
128+
return pathAsArray.map(segment => typeof segment === 'number' ? '*' : segment)
143129
}
144130

145131
function getResolverInfo (info, args) {
@@ -173,19 +159,20 @@ function getResolverInfo (info, args) {
173159
return resolverInfo
174160
}
175161

176-
function getParentField (parentCtx, path) {
177-
for (let i = path.length - 1; i > 0; i--) {
178-
const field = getField(parentCtx, path.slice(0, i))
179-
if (field) {
180-
return field
181-
}
162+
function getParentField (parentCtx, pathToString) {
163+
let current = pathToString
164+
165+
while (current) {
166+
const lastJoin = current.lastIndexOf('.')
167+
if (lastJoin === -1) break
168+
169+
current = current.slice(0, lastJoin)
170+
const field = parentCtx.fields[current]
171+
172+
if (field) return field
182173
}
183174

184175
return null
185176
}
186177

187-
function getField (parentCtx, path) {
188-
return parentCtx.fields[path.join('.')]
189-
}
190-
191178
module.exports = GraphQLResolvePlugin

0 commit comments

Comments
 (0)