Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 24 additions & 16 deletions src/plugins/graphql.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ function wrapFields (fields, tracer, config, responsePathAsArray) {
Object.keys(fields).forEach(key => {
const field = fields[key]

if (typeof field.resolve === 'function') {
if (typeof field.resolve === 'function' && !field.resolve._datadog_patched) {
field.resolve = wrapResolve(field.resolve, tracer, config, responsePathAsArray)
}

Expand All @@ -67,26 +67,34 @@ function wrapFields (fields, tracer, config, responsePathAsArray) {
}

function wrapResolve (resolve, tracer, config, responsePathAsArray) {
return function resolveWithTrace (source, args, contextValue, info) {
const path = responsePathAsArray(info.path)
const fieldParent = getFieldParent(contextValue, path)
const childOf = createSpan('graphql.field', tracer, config, fieldParent, path)
const deferred = defer(tracer)
if (resolve._datadog_patched) {
return resolve
} else {
const resolveWithTrace = function resolveWithTrace (source, args, contextValue, info) {
const path = responsePathAsArray(info.path)
const fieldParent = getFieldParent(contextValue, path)
const childOf = createSpan('graphql.field', tracer, config, fieldParent, path)
const deferred = defer(tracer)

let result

contextValue._datadog_fields[path.join('.')] = {
span: childOf,
parent: fieldParent
}

let result
tracer.trace('graphql.resolve', { childOf }, span => {
addTags(span, tracer, config, path)

contextValue._datadog_fields[path.join('.')] = {
span: childOf,
parent: fieldParent
}
result = call(resolve, this, arguments, deferred, err => finish(span, contextValue, path, err))
})

tracer.trace('graphql.resolve', { childOf }, span => {
addTags(span, tracer, config, path)
return result
}

result = call(resolve, this, arguments, deferred, err => finish(span, contextValue, path, err))
})
resolveWithTrace._datadog_patched = true

return result
return resolveWithTrace
}
}

Expand Down
3 changes: 3 additions & 0 deletions test/plugins/graphql.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ describe('Plugin', () => {
return Promise.resolve({})
}
},
person: {
type: Human
},
friends: {
type: new graphql.GraphQLList(Human),
resolve () {
Expand Down