Skip to content

Commit

Permalink
fix graphql unsupported operation errors and add support for mutations (
Browse files Browse the repository at this point in the history
  • Loading branch information
rochdev committed Jul 11, 2018
1 parent 67f9506 commit 1b90fd1
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 2 deletions.
10 changes: 8 additions & 2 deletions src/plugins/graphql.js
Expand Up @@ -21,6 +21,8 @@ function createWrapExecute (tracer, config, defaultFieldResolver, responsePathAs

if (!schema._datadog_patched) {
wrapFields(schema._queryType, tracer, config, responsePathAsArray)
wrapFields(schema._mutationType, tracer, config, responsePathAsArray)

schema._datadog_patched = true
}

Expand Down Expand Up @@ -53,7 +55,7 @@ function createWrapParse () {
}

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

Expand All @@ -78,6 +80,10 @@ function wrapFields (type, tracer, config, responsePathAsArray) {

function wrapResolve (resolve, tracer, config, responsePathAsArray) {
function resolveWithTrace (source, args, contextValue, info) {
if (!contextValue || !contextValue._datadog_fields) {
return resolve.apply(arguments)
}

const path = responsePathAsArray(info.path)
const fieldParent = getFieldParent(contextValue, path)

Expand Down Expand Up @@ -254,7 +260,7 @@ function getOperation (document) {
return
}

const types = ['query', 'mutations']
const types = ['query', 'mutation']
const definition = document.definitions.find(def => types.indexOf(def.operation) !== -1)

return definition
Expand Down
51 changes: 51 additions & 0 deletions test/plugins/graphql.spec.js
Expand Up @@ -106,6 +106,30 @@ describe('Plugin', () => {
}
}
}
}),

mutation: new graphql.GraphQLObjectType({
name: 'RootMutationType',
fields: {
human: {
type: Human,
resolve () {
return Promise.resolve({ name: 'human name' })
}
}
}
}),

subscription: new graphql.GraphQLObjectType({
name: 'RootSubscriptionType',
fields: {
human: {
type: Human,
resolve () {
return Promise.resolve({ name: 'human name' })
}
}
}
})
})
}
Expand Down Expand Up @@ -324,6 +348,22 @@ describe('Plugin', () => {
graphql.graphql(schema, source).catch(done)
})

it('should instrument mutations', done => {
const source = `mutation { human { name } }`

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

expect(spans).to.have.length(5)
expect(spans[0]).to.have.property('name', 'graphql.mutation')
})
.then(done)
.catch(done)

graphql.graphql(schema, source).catch(done)
})

it('should handle a circular schema', done => {
const source = `{ human { pets { owner { name } } } }`

Expand Down Expand Up @@ -446,6 +486,17 @@ describe('Plugin', () => {
})
})

it('should handle unsupported operations', () => {
const query = `query MyQuery { hello(name: "world") }`
const subscription = `subscription { human { name } }`

return graphql.graphql(schema, query)
.then(() => graphql.graphql(schema, subscription))
.then(result => {
expect(result).to.not.have.property('errors')
})
})

it('should handle calling low level APIs directly', done => {
const source = `query MyQuery { hello(name: "world") }`
const document = graphql.parse(source)
Expand Down

0 comments on commit 1b90fd1

Please sign in to comment.