Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix support for graphql 16 #1670

Merged
merged 1 commit into from
Nov 1, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
26 changes: 16 additions & 10 deletions packages/datadog-plugin-graphql/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,17 @@ let tools
function createWrapExecute (tracer, config, defaultFieldResolver) {
return function wrapExecute (execute) {
return function executeWithTrace () {
const args = normalizeArgs(arguments)
const args = normalizeArgs(arguments, tracer, config, defaultFieldResolver)
const schema = args.schema
const document = args.document
const source = document && document._datadog_source
const fieldResolver = args.fieldResolver || defaultFieldResolver
const contextValue = args.contextValue = args.contextValue || {}
const contextValue = args.contextValue
const operation = getOperation(document, args.operationName)

if (contextValue._datadog_graphql) {
return execute.apply(this, arguments)
}

args.fieldResolver = wrapResolve(fieldResolver, tracer, config)

if (schema) {
wrapFields(schema._queryType, tracer, config)
wrapFields(schema._mutationType, tracer, config)
Expand All @@ -32,7 +29,7 @@ function createWrapExecute (tracer, config, defaultFieldResolver) {

contextValue._datadog_graphql = { source, span, fields: {} }

return call(execute, span, this, [args], (err, res) => {
return call(execute, span, this, arguments, (err, res) => {
finishResolvers(contextValue, config)

setError(span, err || (res && res.errors && res.errors[0]))
Expand Down Expand Up @@ -212,10 +209,19 @@ function getField (contextValue, path) {
return contextValue._datadog_graphql.fields[path.join('.')]
}

function normalizeArgs (args) {
if (args.length === 1) {
return args[0]
}
function normalizeArgs (args, tracer, config, defaultFieldResolver) {
if (args.length !== 1) return normalizePositional(args, tracer, config, defaultFieldResolver)

args[0].contextValue = args[0].contextValue || {}
args[0].fieldResolver = wrapResolve(args[0].fieldResolver || defaultFieldResolver, tracer, config)

return args[0]
}

function normalizePositional (args, tracer, config, defaultFieldResolver) {
args[3] = args[3] || {} // contextValue
args[6] = wrapResolve(args[6] || defaultFieldResolver, tracer, config) // fieldResolver
args.length = Math.max(args.length, 7)

return {
schema: args[0],
Expand Down
99 changes: 71 additions & 28 deletions packages/datadog-plugin-graphql/test/index.spec.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
'use strict'

const { expect } = require('chai')
const semver = require('semver')
const agent = require('../../dd-trace/test/plugins/agent')
const plugin = require('../src')

Expand Down Expand Up @@ -175,6 +177,7 @@ describe('Plugin', () => {

it('should instrument parsing', done => {
const source = `query MyQuery { hello(name: "world") }`
const variableValues = { who: 'world' }

agent
.use(traces => {
Expand All @@ -189,11 +192,12 @@ describe('Plugin', () => {
.then(done)
.catch(done)

graphql.graphql(schema, source, null, null, { who: 'world' }).catch(done)
graphql.graphql({ schema, source, variableValues }).catch(done)
})

it('should instrument validation', done => {
const source = `query MyQuery { hello(name: "world") }`
const variableValues = { who: 'world' }

agent
.use(traces => {
Expand All @@ -208,11 +212,12 @@ describe('Plugin', () => {
.then(done)
.catch(done)

graphql.graphql(schema, source, null, null, { who: 'world' }).catch(done)
graphql.graphql({ schema, source, variableValues }).catch(done)
})

it('should instrument execution', done => {
const source = `query MyQuery { hello(name: "world") }`
const variableValues = { who: 'world' }

agent
.use(traces => {
Expand All @@ -229,11 +234,12 @@ describe('Plugin', () => {
.then(done)
.catch(done)

graphql.graphql(schema, source, null, null, { who: 'world' }).catch(done)
graphql.graphql({ schema, source, variableValues }).catch(done)
})

it('should not include variables by default', done => {
const source = `query MyQuery($who: String!) { hello(name: $who) }`
const variableValues = { who: 'world' }

agent
.use(traces => {
Expand All @@ -243,7 +249,7 @@ describe('Plugin', () => {
.then(done)
.catch(done)

graphql.graphql(schema, source, null, null, { who: 'world' }).catch(done)
graphql.graphql({ schema, source, variableValues }).catch(done)
})

it('should instrument schema resolvers', done => {
Expand All @@ -267,7 +273,7 @@ describe('Plugin', () => {
.then(done)
.catch(done)

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

it('should instrument nested field resolvers', done => {
Expand Down Expand Up @@ -325,7 +331,7 @@ describe('Plugin', () => {
.then(done)
.catch(done)

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

it('should instrument list field resolvers', done => {
Expand Down Expand Up @@ -375,7 +381,7 @@ describe('Plugin', () => {
.then(done)
.catch(done)

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

it('should instrument mutations', done => {
Expand All @@ -390,7 +396,7 @@ describe('Plugin', () => {
.then(done)
.catch(done)

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

it('should instrument subscriptions', done => {
Expand All @@ -405,13 +411,13 @@ describe('Plugin', () => {
.then(done)
.catch(done)

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

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

graphql.graphql(schema, source)
graphql.graphql({ schema, source })
.then((result) => {
expect(result.data.human.pets[0].owner.name).to.equal('test')
})
Expand All @@ -427,6 +433,7 @@ describe('Plugin', () => {
`)

const source = `{ hello }`
const rootValue = { hello: 'world' }

agent
.use(traces => {
Expand All @@ -439,7 +446,7 @@ describe('Plugin', () => {
.then(done)
.catch(done)

graphql.graphql(schema, source, { hello: 'world' }).catch(done)
graphql.graphql({ schema, source, rootValue }).catch(done)
})

it('should instrument the execution field resolver without a rootValue resolver', done => {
Expand Down Expand Up @@ -484,14 +491,15 @@ describe('Plugin', () => {
.then(done)
.catch(done)

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

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

it('should run parsing, validation and execution in the current context', done => {
const source = `query MyQuery { hello(name: "world") }`
const variableValues = { who: 'world' }
const span = tracer.startSpan('test.request')

agent
Expand Down Expand Up @@ -520,7 +528,7 @@ describe('Plugin', () => {
.catch(done)

tracer.scope().activate(span, () => {
graphql.graphql(schema, source, null, null, { who: 'world' })
graphql.graphql({ schema, source, variableValues })
.then(() => span.finish())
.catch(done)
})
Expand Down Expand Up @@ -576,11 +584,11 @@ describe('Plugin', () => {
})

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

return graphql.graphql(schema, query)
.then(() => graphql.graphql(schema, subscription))
return graphql.graphql({ schema, source })
.then(() => graphql.graphql({ schema, source: subscription }))
.then(result => {
expect(result).to.not.have.property('errors')
})
Expand Down Expand Up @@ -631,7 +639,7 @@ describe('Plugin', () => {
.then(done)
.catch(done)

graphql.execute(schema, document)
graphql.execute({ schema, document })
})

it('should handle parsing exceptions', done => {
Expand Down Expand Up @@ -767,7 +775,7 @@ describe('Plugin', () => {
.then(done)
.catch(done)

Promise.resolve(graphql.execute(schema, document, rootValue))
Promise.resolve(graphql.execute({ schema, document, rootValue }))
.then(res => {
error = res.errors[0]
})
Expand Down Expand Up @@ -934,7 +942,7 @@ describe('Plugin', () => {
.then(done)
.catch(done)

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

it('should handle single fragment definitions', done => {
Expand All @@ -958,9 +966,42 @@ describe('Plugin', () => {
.then(done)
.catch(done)

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

// https://github.com/graphql/graphql-js/pull/2904
if (!semver.intersects(version, '>=16')) {
it('should instrument using positional arguments', done => {
const source = `query MyQuery { hello(name: "world") }`
const variableValues = { who: 'world' }

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

expect(spans[0]).to.have.property('service', 'test')
expect(spans[0]).to.have.property('name', 'graphql.execute')
expect(spans[0]).to.have.property('resource', 'query MyQuery{hello(name:"")}')
expect(spans[0]).to.have.property('type', 'graphql')
expect(spans[0].meta).to.not.have.property('graphql.source')
expect(spans[0].meta).to.have.property('graphql.operation.type', 'query')
expect(spans[0].meta).to.have.property('graphql.operation.name', 'MyQuery')
})
.then(done)
.catch(done)

graphql.graphql(schema, source, null, null, variableValues).catch(done)
})
} else {
it('should not support positional arguments', done => {
const source = `query MyQuery { hello(name: "world") }`
const variableValues = { who: 'world' }

graphql.graphql(schema, source, null, null, variableValues)
.then(() => done(new Error('Expected error.')), () => done())
})
}

// it('should not disable signature with invalid arguments', done => {
// agent
// .use(traces => {
Expand Down Expand Up @@ -1035,7 +1076,7 @@ describe('Plugin', () => {
.then(done)
.catch(done)

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

it('should apply the filter callback to the variables', done => {
Expand All @@ -1044,6 +1085,7 @@ describe('Plugin', () => {
hello(title: $title, name: $who)
}
`
const variableValues = { title: 'planet', who: 'world' }

agent
.use(traces => {
Expand All @@ -1057,7 +1099,7 @@ describe('Plugin', () => {
.then(done)
.catch(done)

graphql.graphql(schema, source, null, null, { title: 'planet', who: 'world' }).catch(done)
graphql.graphql({ schema, source, variableValues }).catch(done)
})
})

Expand Down Expand Up @@ -1085,6 +1127,7 @@ describe('Plugin', () => {
hello(title: $title, name: $who)
}
`
const variableValues = { title: 'planet', who: 'world' }

agent
.use(traces => {
Expand All @@ -1096,7 +1139,7 @@ describe('Plugin', () => {
.then(done)
.catch(done)

graphql.graphql(schema, source, null, null, { title: 'planet', who: 'world' }).catch(done)
graphql.graphql({ schema, source, variableValues }).catch(done)
})
})

Expand Down Expand Up @@ -1139,7 +1182,7 @@ describe('Plugin', () => {
.then(done)
.catch(done)

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

it('should run the resolvers in the execution scope', done => {
Expand Down Expand Up @@ -1217,7 +1260,7 @@ describe('Plugin', () => {
.then(done)
.catch(done)

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

Expand Down Expand Up @@ -1271,7 +1314,7 @@ describe('Plugin', () => {
.then(done)
.catch(done)

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

Expand Down Expand Up @@ -1304,7 +1347,7 @@ describe('Plugin', () => {
.then(done)
.catch(done)

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

Expand Down