forked from graphql/graphql-js
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathlists-test.ts
More file actions
603 lines (544 loc) · 16.8 KB
/
lists-test.ts
File metadata and controls
603 lines (544 loc) · 16.8 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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
import { assert, expect } from 'chai';
import { describe, it } from 'mocha';
import { expectJSON } from '../../__testUtils__/expectJSON.js';
import type { PromiseOrValue } from '../../jsutils/PromiseOrValue.js';
import { parse } from '../../language/parser.js';
import type { GraphQLFieldResolver } from '../../type/definition.js';
import {
GraphQLList,
GraphQLNonNull,
GraphQLObjectType,
} from '../../type/definition.js';
import { GraphQLString } from '../../type/scalars.js';
import { GraphQLSchema } from '../../type/schema.js';
import { buildSchema } from '../../utilities/buildASTSchema.js';
import { execute, executeSync } from '../entrypoints.js';
import type { ExecutionResult } from '../Executor.js';
describe('Execute: Accepts any iterable as list value', () => {
function complete(rootValue: unknown) {
return executeSync({
schema: buildSchema('type Query { listField: [String] }'),
document: parse('{ listField }'),
rootValue,
});
}
it('Accepts a Set as a List value', () => {
const listField = new Set(['apple', 'banana', 'apple', 'coconut']);
expect(complete({ listField })).to.deep.equal({
data: { listField: ['apple', 'banana', 'coconut'] },
});
});
it('Accepts a Generator function as a List value', () => {
function* listField() {
yield 'one';
yield 2;
yield true;
}
expect(complete({ listField })).to.deep.equal({
data: { listField: ['one', '2', 'true'] },
});
});
it('Accepts function arguments as a List value', () => {
function getArgs(..._args: ReadonlyArray<string>) {
return arguments;
}
const listField = getArgs('one', 'two');
expect(complete({ listField })).to.deep.equal({
data: { listField: ['one', 'two'] },
});
});
it('Does not accept (Iterable) String-literal as a List value', () => {
const listField = 'Singular';
expectJSON(complete({ listField })).toDeepEqual({
data: { listField: null },
errors: [
{
message:
'Expected Iterable, but did not find one for field "Query.listField".',
locations: [{ line: 1, column: 3 }],
path: ['listField'],
},
],
});
});
it('Ignores iterator return errors when iteration throws', () => {
let returnCalled = false;
const listField = {
[Symbol.iterator]() {
return {
next() {
throw new Error('bad');
},
return() {
returnCalled = true;
throw new Error('return bad');
},
};
},
};
expectJSON(complete({ listField })).toDeepEqual({
data: { listField: null },
errors: [
{
message: 'bad',
locations: [{ line: 1, column: 3 }],
path: ['listField'],
},
],
});
expect(returnCalled).to.equal(true);
});
});
describe('Execute: Handles abrupt completion in synchronous iterables', () => {
function complete(rootValue: unknown, as: string = '[String]') {
return execute({
schema: buildSchema(`type Query { listField: ${as} }`),
document: parse('{ listField }'),
rootValue,
});
}
it('closes the iterator when `next` throws', async () => {
let returned = false;
let nextCalls = 0;
const listField: IterableIterator<string> = {
[Symbol.iterator](): IterableIterator<string> {
return this;
},
next(): IteratorResult<string> {
nextCalls++;
if (nextCalls === 1) {
return { done: false, value: 'ok' };
}
throw new Error('bad');
},
return(): IteratorResult<string> {
returned = true;
return { done: true, value: undefined };
},
};
expectJSON(await complete({ listField })).toDeepEqual({
data: { listField: null },
errors: [
{
message: 'bad',
locations: [{ line: 1, column: 3 }],
path: ['listField'],
},
],
});
expect(nextCalls).to.equal(2);
expect(returned).to.equal(true);
});
it('closes the iterator when a null bubbles up from a non-null item', async () => {
const values = [1, null, 2];
let index = 0;
let returned = false;
const listField: IterableIterator<number | null> = {
[Symbol.iterator](): IterableIterator<number | null> {
return this;
},
next(): IteratorResult<number | null> {
const value = values[index++];
if (value === undefined) {
return { done: true, value: undefined };
}
return { done: false, value };
},
return(): IteratorResult<number | null> {
returned = true;
return { done: true, value: undefined };
},
};
expectJSON(await complete({ listField }, '[Int!]')).toDeepEqual({
data: { listField: null },
errors: [
{
message: 'Cannot return null for non-nullable field Query.listField.',
locations: [{ line: 1, column: 3 }],
path: ['listField', 1],
},
],
});
expect(index).to.equal(2);
expect(returned).to.equal(true);
});
it('ignores errors thrown by the iterator `return` method', async () => {
const values = [1, null, 2];
let index = 0;
let returned = false;
const listField: IterableIterator<number | null> = {
[Symbol.iterator](): IterableIterator<number | null> {
return this;
},
next(): IteratorResult<number | null> {
const value = values[index++];
if (value === undefined) {
return { done: true, value: undefined };
}
return { done: false, value };
},
return(): IteratorResult<number | null> {
returned = true;
throw new Error('ignored return error');
},
};
expectJSON(await complete({ listField }, '[Int!]')).toDeepEqual({
data: { listField: null },
errors: [
{
message: 'Cannot return null for non-nullable field Query.listField.',
locations: [{ line: 1, column: 3 }],
path: ['listField', 1],
},
],
});
expect(index).to.equal(2);
expect(returned).to.equal(true);
});
});
describe('Execute: Accepts async iterables as list value', () => {
function complete(rootValue: unknown, as: string = '[String]') {
return execute({
schema: buildSchema(`type Query { listField: ${as} }`),
document: parse('{ listField }'),
rootValue,
});
}
function completeObjectList(
resolve: GraphQLFieldResolver<{ index: number }, unknown>,
): PromiseOrValue<ExecutionResult> {
const schema = new GraphQLSchema({
query: new GraphQLObjectType({
name: 'Query',
fields: {
listField: {
resolve: async function* listField() {
yield await Promise.resolve({ index: 0 });
yield await Promise.resolve({ index: 1 });
yield await Promise.resolve({ index: 2 });
},
type: new GraphQLList(
new GraphQLObjectType({
name: 'ObjectWrapper',
fields: {
index: {
type: new GraphQLNonNull(GraphQLString),
resolve,
},
},
}),
),
},
},
}),
});
return execute({
schema,
document: parse('{ listField { index } }'),
});
}
it('Accepts an AsyncGenerator function as a List value', async () => {
async function* listField() {
yield await Promise.resolve('two');
yield await Promise.resolve(4);
yield await Promise.resolve(false);
}
expectJSON(await complete({ listField })).toDeepEqual({
data: { listField: ['two', '4', 'false'] },
});
});
it('Handles an AsyncGenerator function that throws', async () => {
async function* listField() {
yield await Promise.resolve('two');
yield await Promise.resolve(4);
throw new Error('bad');
}
expectJSON(await complete({ listField })).toDeepEqual({
data: { listField: null },
errors: [
{
message: 'bad',
locations: [{ line: 1, column: 3 }],
path: ['listField'],
},
],
});
});
it('Handles an AsyncGenerator function where an intermediate value triggers an error', async () => {
async function* listField() {
yield await Promise.resolve('two');
yield await Promise.resolve({});
yield await Promise.resolve(4);
}
expectJSON(await complete({ listField })).toDeepEqual({
data: { listField: ['two', null, '4'] },
errors: [
{
message: 'String cannot represent value: {}',
locations: [{ line: 1, column: 3 }],
path: ['listField', 1],
},
],
});
});
it('Handles errors from `completeValue` in AsyncIterables', async () => {
async function* listField() {
yield await Promise.resolve('two');
yield await Promise.resolve({});
}
expectJSON(await complete({ listField })).toDeepEqual({
data: { listField: ['two', null] },
errors: [
{
message: 'String cannot represent value: {}',
locations: [{ line: 1, column: 3 }],
path: ['listField', 1],
},
],
});
});
it('Handles promises from `completeValue` in AsyncIterables', async () => {
expectJSON(
await completeObjectList(({ index }) => Promise.resolve(index)),
).toDeepEqual({
data: { listField: [{ index: '0' }, { index: '1' }, { index: '2' }] },
});
});
it('Handles rejected promises from `completeValue` in AsyncIterables', async () => {
expectJSON(
await completeObjectList(({ index }) => {
if (index === 2) {
return Promise.reject(new Error('bad'));
}
return Promise.resolve(index);
}),
).toDeepEqual({
data: { listField: [{ index: '0' }, { index: '1' }, null] },
errors: [
{
message: 'bad',
locations: [{ line: 1, column: 15 }],
path: ['listField', 2, 'index'],
},
],
});
});
it('Handles nulls yielded by async generator', async () => {
async function* listField() {
yield await Promise.resolve(1);
yield await Promise.resolve(null);
yield await Promise.resolve(2);
}
const errors = [
{
message: 'Cannot return null for non-nullable field Query.listField.',
locations: [{ line: 1, column: 3 }],
path: ['listField', 1],
},
];
expect(await complete({ listField }, '[Int]')).to.deep.equal({
data: { listField: [1, null, 2] },
});
expect(await complete({ listField }, '[Int]!')).to.deep.equal({
data: { listField: [1, null, 2] },
});
expectJSON(await complete({ listField }, '[Int!]')).toDeepEqual({
data: { listField: null },
errors,
});
expectJSON(await complete({ listField }, '[Int!]!')).toDeepEqual({
data: null,
errors,
});
});
it('Returns async iterable when list nulls', async () => {
const values = [1, null, 2];
let i = 0;
let returned = false;
const listField = {
[Symbol.asyncIterator]: () => ({
next: () => Promise.resolve({ value: values[i++], done: false }),
return: () => {
returned = true;
return Promise.resolve({ value: undefined, done: true });
},
}),
};
const errors = [
{
message: 'Cannot return null for non-nullable field Query.listField.',
locations: [{ line: 1, column: 3 }],
path: ['listField', 1],
},
];
expectJSON(await complete({ listField }, '[Int!]')).toDeepEqual({
data: { listField: null },
errors,
});
assert(returned);
});
it('Ignores error on return method when async iterator nulls', async () => {
const values = [1, null, 2];
let i = 0;
const listField = {
[Symbol.asyncIterator]: () => ({
next: () => Promise.resolve({ value: values[i++], done: false }),
return: () => Promise.reject(new Error('ignored return error')),
}),
};
const errors = [
{
message: 'Cannot return null for non-nullable field Query.listField.',
locations: [{ line: 1, column: 3 }],
path: ['listField', 1],
},
];
expectJSON(await complete({ listField }, '[Int!]')).toDeepEqual({
data: { listField: null },
errors,
});
});
});
describe('Execute: Handles list nullability', () => {
async function complete(args: { listField: unknown; as: string }) {
const { listField, as } = args;
const schema = buildSchema(`type Query { listField: ${as} }`);
const document = parse('{ listField }');
const result = await executeQuery(listField);
// Promise<Array<T>> === Array<T>
expectJSON(await executeQuery(promisify(listField))).toDeepEqual(result);
if (Array.isArray(listField)) {
const listOfPromises = listField.map(promisify);
// Array<Promise<T>> === Array<T>
expectJSON(await executeQuery(listOfPromises)).toDeepEqual(result);
// Promise<Array<Promise<T>>> === Array<T>
expectJSON(await executeQuery(promisify(listOfPromises))).toDeepEqual(
result,
);
}
return result;
function executeQuery(listValue: unknown) {
return execute({ schema, document, rootValue: { listField: listValue } });
}
function promisify(value: unknown): Promise<unknown> {
return value instanceof Error
? Promise.reject(value)
: Promise.resolve(value);
}
}
it('Contains values', async () => {
const listField = [1, 2];
expect(await complete({ listField, as: '[Int]' })).to.deep.equal({
data: { listField: [1, 2] },
});
expect(await complete({ listField, as: '[Int]!' })).to.deep.equal({
data: { listField: [1, 2] },
});
expect(await complete({ listField, as: '[Int!]' })).to.deep.equal({
data: { listField: [1, 2] },
});
expect(await complete({ listField, as: '[Int!]!' })).to.deep.equal({
data: { listField: [1, 2] },
});
});
it('Contains null', async () => {
const listField = [1, null, 2];
const errors = [
{
message: 'Cannot return null for non-nullable field Query.listField.',
locations: [{ line: 1, column: 3 }],
path: ['listField', 1],
},
];
expect(await complete({ listField, as: '[Int]' })).to.deep.equal({
data: { listField: [1, null, 2] },
});
expect(await complete({ listField, as: '[Int]!' })).to.deep.equal({
data: { listField: [1, null, 2] },
});
expectJSON(await complete({ listField, as: '[Int!]' })).toDeepEqual({
data: { listField: null },
errors,
});
expectJSON(await complete({ listField, as: '[Int!]!' })).toDeepEqual({
data: null,
errors,
});
});
it('Returns null', async () => {
const listField = null;
const errors = [
{
message: 'Cannot return null for non-nullable field Query.listField.',
locations: [{ line: 1, column: 3 }],
path: ['listField'],
},
];
expect(await complete({ listField, as: '[Int]' })).to.deep.equal({
data: { listField: null },
});
expectJSON(await complete({ listField, as: '[Int]!' })).toDeepEqual({
data: null,
errors,
});
expect(await complete({ listField, as: '[Int!]' })).to.deep.equal({
data: { listField: null },
});
expectJSON(await complete({ listField, as: '[Int!]!' })).toDeepEqual({
data: null,
errors,
});
});
it('Contains error', async () => {
const listField = [1, new Error('bad'), 2];
const errors = [
{
message: 'bad',
locations: [{ line: 1, column: 3 }],
path: ['listField', 1],
},
];
expectJSON(await complete({ listField, as: '[Int]' })).toDeepEqual({
data: { listField: [1, null, 2] },
errors,
});
expectJSON(await complete({ listField, as: '[Int]!' })).toDeepEqual({
data: { listField: [1, null, 2] },
errors,
});
expectJSON(await complete({ listField, as: '[Int!]' })).toDeepEqual({
data: { listField: null },
errors,
});
expectJSON(await complete({ listField, as: '[Int!]!' })).toDeepEqual({
data: null,
errors,
});
});
it('Results in error', async () => {
const listField = new Error('bad');
const errors = [
{
message: 'bad',
locations: [{ line: 1, column: 3 }],
path: ['listField'],
},
];
expectJSON(await complete({ listField, as: '[Int]' })).toDeepEqual({
data: { listField: null },
errors,
});
expectJSON(await complete({ listField, as: '[Int]!' })).toDeepEqual({
data: null,
errors,
});
expectJSON(await complete({ listField, as: '[Int!]' })).toDeepEqual({
data: { listField: null },
errors,
});
expectJSON(await complete({ listField, as: '[Int!]!' })).toDeepEqual({
data: null,
errors,
});
});
});