forked from elastic/apm-agent-nodejs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrace-graphql.js
More file actions
47 lines (41 loc) · 1.46 KB
/
trace-graphql.js
File metadata and controls
47 lines (41 loc) · 1.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
/*
* Copyright Elasticsearch B.V. and other contributors where applicable.
* Licensed under the BSD 2-Clause License; you may not use this file except in
* compliance with the BSD 2-Clause License.
*/
// A small example showing Elastic APM tracing of a script using `graphql`.
// Adapted from https://graphql.org/graphql-js/#writing-code
const apm = require('../').start({ // elastic-apm-node
serviceName: 'example-trace-graphql'
})
const { graphql, buildSchema } = require('graphql')
// Construct a schema, using GraphQL schema language
const schema = buildSchema(`
type Query {
hello: String
bye: String
}
`)
// The root provides a resolver function for each API endpoint
const root = {
hello: () => {
return 'Hello world!'
},
bye: () => {
return 'Farewell!'
}
}
// For tracing spans to be created, there must be an active transaction.
// Typically, a transaction is automatically started for incoming HTTP
// requests to a Node.js server. However, because this script is not running
// an HTTP server, we manually start a transaction. More details at:
// https://www.elastic.co/guide/en/apm/agent/nodejs/current/custom-transactions.html
const t1 = apm.startTransaction('t1')
// Run the GraphQL query '{ hello }' and print out the response
graphql(schema, '{ hello }', root).then((response) => {
console.log('hello response:', response)
})
graphql(schema, '{ bye }', root).then((response) => {
console.log('bye response:', response)
t1.end()
})