Skip to content

Commit

Permalink
Fix graphql wrapping types
Browse files Browse the repository at this point in the history
  • Loading branch information
Miguel Martins committed Dec 10, 2018
1 parent 86f8122 commit 4bf1c9a
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 12 deletions.
14 changes: 7 additions & 7 deletions src/plugins/graphql.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ function createWrapValidate (tracer, config) {
}

function wrapFields (type, tracer, config, responsePathAsArray) {
if (!type || type._datadog_patched) {
if (!type || !type._fields || type._datadog_patched) {
return
}

Expand All @@ -86,13 +86,13 @@ function wrapFields (type, tracer, config, responsePathAsArray) {
field.resolve = wrapResolve(field.resolve, tracer, config, responsePathAsArray)
}

if (field.type) {
if (field.type._fields) {
wrapFields(field.type, tracer, config, responsePathAsArray)
} else if (field.type.ofType && field.type.ofType._fields) {
wrapFields(field.type.ofType, tracer, config, responsePathAsArray)
}
let unwrappedType = field.type

while (unwrappedType.ofType) {
unwrappedType = unwrappedType.ofType
}

wrapFields(unwrappedType, tracer, config, responsePathAsArray)
})
}

Expand Down
40 changes: 35 additions & 5 deletions test/plugins/graphql.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ describe('Plugin', () => {
}
},
pets: {
type: new graphql.GraphQLList(new graphql.GraphQLObjectType({
type: new graphql.GraphQLList(new graphql.GraphQLNonNull(new graphql.GraphQLObjectType({
name: 'Pet',
fields: () => ({
type: {
Expand Down Expand Up @@ -70,7 +70,7 @@ describe('Plugin', () => {
}
}
})
})),
}))),
resolve (obj, args) {
return [{}, {}, {}]
}
Expand Down Expand Up @@ -338,7 +338,6 @@ describe('Plugin', () => {
agent
.use(traces => {
const spans = sort(traces[0])

expect(spans).to.have.length(14)

const execute = spans[3]
Expand Down Expand Up @@ -422,19 +421,30 @@ describe('Plugin', () => {
})

it('should instrument list field resolvers', done => {
const source = `{ friends { name } }`
const source = `{
friends {
name
pets {
name
}
}
}`

agent
.use(traces => {
const spans = sort(traces[0])

expect(spans).to.have.length(8)
expect(spans).to.have.length(12)

const execute = spans[3]
const friendsField = spans[4]
const friendsResolve = spans[5]
const friendNameField = spans[6]
const friendNameResolve = spans[7]
const petsField = spans[8]
const petsResolve = spans[9]
const petsNameField = spans[10]
const petsNameResolve = spans[11]

expect(execute).to.have.property('name', 'graphql.execute')

Expand All @@ -457,6 +467,26 @@ describe('Plugin', () => {
expect(friendNameResolve).to.have.property('resource', 'friends.*.name')
expect(friendNameResolve.meta).to.have.property('graphql.field.path', 'friends.*.name')
expect(friendNameResolve.parent_id.toString()).to.equal(friendNameField.span_id.toString())

expect(petsField).to.have.property('name', 'graphql.field')
expect(petsField).to.have.property('resource', 'friends.*.pets')
expect(petsField.meta).to.have.property('graphql.field.path', 'friends.*.pets')
expect(petsField.parent_id.toString()).to.equal(friendsField.span_id.toString())

expect(petsResolve).to.have.property('name', 'graphql.resolve')
expect(petsResolve).to.have.property('resource', 'friends.*.pets')
expect(petsResolve.meta).to.have.property('graphql.field.path', 'friends.*.pets')
expect(petsResolve.parent_id.toString()).to.equal(petsField.span_id.toString())

expect(petsNameField).to.have.property('name', 'graphql.field')
expect(petsNameField).to.have.property('resource', 'friends.*.pets.*.name')
expect(petsNameField.meta).to.have.property('graphql.field.path', 'friends.*.pets.*.name')
expect(petsNameField.parent_id.toString()).to.equal(petsField.span_id.toString())

expect(petsNameResolve).to.have.property('name', 'graphql.resolve')
expect(petsNameResolve).to.have.property('resource', 'friends.*.pets.*.name')
expect(petsNameResolve.meta).to.have.property('graphql.field.path', 'friends.*.pets.*.name')
expect(petsNameResolve.parent_id.toString()).to.equal(petsNameField.span_id.toString())
})
.then(done)
.catch(done)
Expand Down

0 comments on commit 4bf1c9a

Please sign in to comment.