Skip to content

Commit 256b8be

Browse files
authored
test(node): Add mutation case to GraphQL OTEL integration tests. (#10352)
Adds a mutation scenario to GraphQL OTEL integration tests.
1 parent 35b6b26 commit 256b8be

File tree

3 files changed

+118
-31
lines changed

3 files changed

+118
-31
lines changed
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
const Sentry = require('@sentry/node-experimental');
2+
const { loggingTransport } = require('@sentry-internal/node-integration-tests');
3+
4+
Sentry.init({
5+
dsn: 'https://[email protected]/1337',
6+
release: '1.0',
7+
tracesSampleRate: 1.0,
8+
transport: loggingTransport,
9+
});
10+
11+
// Stop the process from exiting before the transaction is sent
12+
setInterval(() => {}, 1000);
13+
14+
async function run() {
15+
const { ApolloServer, gql } = require('apollo-server');
16+
17+
await Sentry.startSpan(
18+
{
19+
name: 'Test Transaction',
20+
op: 'transaction',
21+
},
22+
async span => {
23+
const server = new ApolloServer({
24+
typeDefs: gql`
25+
type Query {
26+
hello: String
27+
}
28+
type Mutation {
29+
login(email: String): String
30+
}
31+
`,
32+
resolvers: {
33+
Query: {
34+
hello: () => {
35+
return 'Hello world!';
36+
},
37+
},
38+
Mutation: {
39+
login: async (_, { email }) => {
40+
return `${email}--token`;
41+
},
42+
},
43+
},
44+
});
45+
46+
// Ref: https://www.apollographql.com/docs/apollo-server/testing/testing/#testing-using-executeoperation
47+
await server.executeOperation({
48+
query: gql`mutation Mutation($email: String){
49+
login(email: $email)
50+
}`,
51+
variables: { email: '[email protected]' },
52+
});
53+
54+
setTimeout(() => {
55+
span.end();
56+
server.stop();
57+
}, 500);
58+
},
59+
);
60+
}
61+
62+
// eslint-disable-next-line @typescript-eslint/no-floating-promises
63+
run();

dev-packages/node-integration-tests/suites/tracing-experimental/apollo-graphql/test.ts

Lines changed: 55 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -2,37 +2,61 @@ import { conditionalTest } from '../../../utils';
22
import { createRunner } from '../../../utils/runner';
33

44
conditionalTest({ min: 14 })('GraphQL/Apollo Tests', () => {
5-
const EXPECTED_TRANSACTION = {
6-
transaction: 'Test Transaction',
7-
spans: expect.arrayContaining([
8-
expect.objectContaining({
9-
data: {
10-
'graphql.operation.type': 'query',
11-
'graphql.source': '{hello}',
12-
'otel.kind': 'INTERNAL',
13-
'sentry.origin': 'auto.graphql.otel.graphql',
14-
},
15-
description: 'query',
16-
status: 'ok',
17-
origin: 'auto.graphql.otel.graphql',
18-
}),
19-
expect.objectContaining({
20-
data: {
21-
'graphql.field.name': 'hello',
22-
'graphql.field.path': 'hello',
23-
'graphql.field.type': 'String',
24-
'graphql.source': 'hello',
25-
'otel.kind': 'INTERNAL',
26-
'sentry.origin': 'manual',
27-
},
28-
description: 'graphql.resolve',
29-
status: 'ok',
30-
origin: 'manual',
31-
}),
32-
]),
33-
};
34-
355
test('CJS - should instrument GraphQL queries used from Apollo Server.', done => {
36-
createRunner(__dirname, 'scenario.js').expect({ transaction: EXPECTED_TRANSACTION }).start(done);
6+
const EXPECTED_TRANSACTION = {
7+
transaction: 'Test Transaction',
8+
spans: expect.arrayContaining([
9+
expect.objectContaining({
10+
data: {
11+
'graphql.operation.type': 'query',
12+
'graphql.source': '{hello}',
13+
'otel.kind': 'INTERNAL',
14+
'sentry.origin': 'auto.graphql.otel.graphql',
15+
},
16+
description: 'query',
17+
status: 'ok',
18+
origin: 'auto.graphql.otel.graphql',
19+
}),
20+
expect.objectContaining({
21+
data: {
22+
'graphql.field.name': 'hello',
23+
'graphql.field.path': 'hello',
24+
'graphql.field.type': 'String',
25+
'graphql.source': 'hello',
26+
'otel.kind': 'INTERNAL',
27+
'sentry.origin': 'manual',
28+
},
29+
description: 'graphql.resolve',
30+
status: 'ok',
31+
origin: 'manual',
32+
}),
33+
]),
34+
};
35+
36+
createRunner(__dirname, 'scenario-query.js').expect({ transaction: EXPECTED_TRANSACTION }).start(done);
37+
});
38+
39+
test('CJS - should instrument GraphQL mutations used from Apollo Server.', done => {
40+
const EXPECTED_TRANSACTION = {
41+
transaction: 'Test Transaction',
42+
spans: expect.arrayContaining([
43+
expect.objectContaining({
44+
data: {
45+
'graphql.operation.name': 'Mutation',
46+
'graphql.operation.type': 'mutation',
47+
'graphql.source': `mutation Mutation($email: String) {
48+
login(email: $email)
49+
}`,
50+
'otel.kind': 'INTERNAL',
51+
'sentry.origin': 'auto.graphql.otel.graphql',
52+
},
53+
description: 'mutation Mutation',
54+
status: 'ok',
55+
origin: 'auto.graphql.otel.graphql',
56+
}),
57+
]),
58+
};
59+
60+
createRunner(__dirname, 'scenario-mutation.js').expect({ transaction: EXPECTED_TRANSACTION }).start(done);
3761
});
3862
});

0 commit comments

Comments
 (0)