-
Notifications
You must be signed in to change notification settings - Fork 2k
Expand file tree
/
Copy pathtest.js
More file actions
297 lines (260 loc) · 8.69 KB
/
test.js
File metadata and controls
297 lines (260 loc) · 8.69 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
// TracingChannel is marked experimental in Node's docs but is shipped on
// every runtime graphql-js supports. This test exercises it directly.
/* eslint-disable n/no-unsupported-features/node-builtins */
import assert from 'node:assert/strict';
import { AsyncLocalStorage } from 'node:async_hooks';
import dc from 'node:diagnostics_channel';
import {
buildSchema,
enableDiagnosticsChannel,
execute,
parse,
subscribe,
validate,
} from 'graphql';
enableDiagnosticsChannel(dc);
function runParseCases() {
// graphql:parse - synchronous.
{
const events = [];
const handler = {
start: (msg) => events.push({ kind: 'start', source: msg.source }),
end: (msg) => events.push({ kind: 'end', source: msg.source }),
asyncStart: (msg) =>
events.push({ kind: 'asyncStart', source: msg.source }),
asyncEnd: (msg) => events.push({ kind: 'asyncEnd', source: msg.source }),
error: (msg) =>
events.push({ kind: 'error', source: msg.source, error: msg.error }),
};
const channel = dc.tracingChannel('graphql:parse');
channel.subscribe(handler);
try {
const doc = parse('{ field }');
assert.equal(doc.kind, 'Document');
assert.deepEqual(
events.map((e) => e.kind),
['start', 'end'],
);
assert.equal(events[0].source, '{ field }');
assert.equal(events[1].source, '{ field }');
} finally {
channel.unsubscribe(handler);
}
}
// graphql:parse - error path fires start, error, end.
{
const events = [];
const handler = {
start: (msg) => events.push({ kind: 'start', source: msg.source }),
end: (msg) => events.push({ kind: 'end', source: msg.source }),
error: (msg) =>
events.push({ kind: 'error', source: msg.source, error: msg.error }),
};
const channel = dc.tracingChannel('graphql:parse');
channel.subscribe(handler);
try {
assert.throws(() => parse('{ '));
assert.deepEqual(
events.map((e) => e.kind),
['start', 'error', 'end'],
);
assert.ok(events[1].error instanceof Error);
} finally {
channel.unsubscribe(handler);
}
}
}
function runValidateCase() {
const schema = buildSchema(`type Query { field: String }`);
const doc = parse('{ field }');
const events = [];
const handler = {
start: (msg) =>
events.push({
kind: 'start',
schema: msg.schema,
document: msg.document,
}),
end: () => events.push({ kind: 'end' }),
error: (msg) => events.push({ kind: 'error', error: msg.error }),
};
const channel = dc.tracingChannel('graphql:validate');
channel.subscribe(handler);
try {
const errors = validate(schema, doc);
assert.deepEqual(errors, []);
assert.deepEqual(
events.map((e) => e.kind),
['start', 'end'],
);
assert.equal(events[0].schema, schema);
assert.equal(events[0].document, doc);
} finally {
channel.unsubscribe(handler);
}
}
function runExecuteCase() {
const schema = buildSchema(`type Query { hello: String }`);
const document = parse('query Greeting { hello }');
const events = [];
const handler = {
start: (msg) =>
events.push({
kind: 'start',
operationType: msg.operationType,
operationName: msg.operationName,
document: msg.document,
schema: msg.schema,
}),
end: () => events.push({ kind: 'end' }),
asyncStart: () => events.push({ kind: 'asyncStart' }),
asyncEnd: () => events.push({ kind: 'asyncEnd' }),
error: (msg) => events.push({ kind: 'error', error: msg.error }),
};
const channel = dc.tracingChannel('graphql:execute');
channel.subscribe(handler);
try {
const result = execute({
schema,
document,
rootValue: { hello: 'world' },
});
assert.equal(result.data.hello, 'world');
assert.deepEqual(
events.map((e) => e.kind),
['start', 'end'],
);
assert.equal(events[0].operationType, 'query');
assert.equal(events[0].operationName, 'Greeting');
assert.equal(events[0].document, document);
assert.equal(events[0].schema, schema);
} finally {
channel.unsubscribe(handler);
}
}
async function runSubscribeCase() {
async function* ticks() {
yield { tick: 'one' };
}
const schema = buildSchema(`
type Query { dummy: String }
type Subscription { tick: String }
`);
// buildSchema doesn't attach a subscribe resolver to fields; inject one.
schema.getSubscriptionType().getFields().tick.subscribe = () => ticks();
const document = parse('subscription Tick { tick }');
const events = [];
const handler = {
start: (msg) =>
events.push({
kind: 'start',
operationType: msg.operationType,
operationName: msg.operationName,
}),
end: () => events.push({ kind: 'end' }),
asyncStart: () => events.push({ kind: 'asyncStart' }),
asyncEnd: () => events.push({ kind: 'asyncEnd' }),
error: (msg) => events.push({ kind: 'error', error: msg.error }),
};
const channel = dc.tracingChannel('graphql:subscribe');
channel.subscribe(handler);
try {
const result = subscribe({ schema, document });
const stream = typeof result.then === 'function' ? await result : result;
if (stream[Symbol.asyncIterator]) {
await stream.return?.();
}
// Subscription setup is synchronous here; start/end fire, no async tail.
assert.deepEqual(
events.map((e) => e.kind),
['start', 'end'],
);
assert.equal(events[0].operationType, 'subscription');
assert.equal(events[0].operationName, 'Tick');
} finally {
channel.unsubscribe(handler);
}
}
function runResolveCase() {
const schema = buildSchema(
`type Query { hello: String nested: Nested } type Nested { leaf: String }`,
);
const document = parse('{ hello nested { leaf } }');
const events = [];
const handler = {
start: (msg) =>
events.push({
kind: 'start',
fieldName: msg.fieldName,
parentType: msg.parentType,
fieldType: msg.fieldType,
fieldPath: msg.fieldPath,
isTrivialResolver: msg.isTrivialResolver,
}),
end: () => events.push({ kind: 'end' }),
asyncStart: () => events.push({ kind: 'asyncStart' }),
asyncEnd: () => events.push({ kind: 'asyncEnd' }),
error: (msg) => events.push({ kind: 'error', error: msg.error }),
};
const channel = dc.tracingChannel('graphql:resolve');
channel.subscribe(handler);
try {
const rootValue = { hello: () => 'world', nested: { leaf: 'leaf-value' } };
execute({ schema, document, rootValue });
const starts = events.filter((e) => e.kind === 'start');
const paths = starts.map((e) => e.fieldPath);
assert.deepEqual(paths, ['hello', 'nested', 'nested.leaf']);
const hello = starts.find((e) => e.fieldName === 'hello');
assert.equal(hello.parentType, 'Query');
assert.equal(hello.fieldType, 'String');
// buildSchema never attaches field.resolve; all fields report as trivial.
assert.equal(hello.isTrivialResolver, true);
} finally {
channel.unsubscribe(handler);
}
}
function runNoSubscriberCase() {
const doc = parse('{ field }');
assert.equal(doc.kind, 'Document');
}
async function runAlsPropagationCase() {
// A subscriber that binds a store on the `start` sub-channel should be able
// to read it in every lifecycle handler (start, end, asyncStart, asyncEnd).
// This is what APMs use to parent child spans to the current operation
// without threading state through the ctx object.
const als = new AsyncLocalStorage();
const channel = dc.tracingChannel('graphql:execute');
channel.start.bindStore(als, (ctx) => ({ operationName: ctx.operationName }));
const seen = {};
const handler = {
start: () => (seen.start = als.getStore()),
end: () => (seen.end = als.getStore()),
asyncStart: () => (seen.asyncStart = als.getStore()),
asyncEnd: () => (seen.asyncEnd = als.getStore()),
};
channel.subscribe(handler);
try {
const schema = buildSchema(`type Query { slow: String }`);
const document = parse('query Slow { slow }');
const rootValue = { slow: () => Promise.resolve('done') };
await execute({ schema, document, rootValue });
assert.deepEqual(seen.start, { operationName: 'Slow' });
assert.deepEqual(seen.end, { operationName: 'Slow' });
assert.deepEqual(seen.asyncStart, { operationName: 'Slow' });
assert.deepEqual(seen.asyncEnd, { operationName: 'Slow' });
} finally {
channel.unsubscribe(handler);
channel.start.unbindStore(als);
}
}
async function main() {
runParseCases();
runValidateCase();
runExecuteCase();
await runSubscribeCase();
runResolveCase();
await runAlsPropagationCase();
runNoSubscriberCase();
console.log('diagnostics integration test passed');
}
main();