forked from graphql/graphql-js
-
Notifications
You must be signed in to change notification settings - Fork 2
/
definition-test.ts
737 lines (663 loc) · 21.5 KB
/
definition-test.ts
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
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
import { expect } from 'chai';
import { describe, it } from 'mocha';
import { identityFunc } from '../../jsutils/identityFunc.js';
import { inspect } from '../../jsutils/inspect.js';
import { Kind } from '../../language/kinds.js';
import { parseValue } from '../../language/parser.js';
import type { GraphQLNullableType, GraphQLType } from '../definition.js';
import {
GraphQLEnumType,
GraphQLInputObjectType,
GraphQLInterfaceType,
GraphQLList,
GraphQLNonNull,
GraphQLObjectType,
GraphQLScalarType,
GraphQLUnionType,
} from '../definition.js';
const ScalarType = new GraphQLScalarType({ name: 'Scalar' });
const ObjectType = new GraphQLObjectType({ name: 'Object', fields: {} });
const InterfaceType = new GraphQLInterfaceType({
name: 'Interface',
fields: {},
});
const UnionType = new GraphQLUnionType({ name: 'Union', types: [ObjectType] });
const EnumType = new GraphQLEnumType({ name: 'Enum', values: { foo: {} } });
const InputObjectType = new GraphQLInputObjectType({
name: 'InputObject',
fields: {},
});
const ListOfScalarsType = new GraphQLList(ScalarType);
const NonNullScalarType = new GraphQLNonNull(ScalarType);
const ListOfNonNullScalarsType = new GraphQLList(NonNullScalarType);
const NonNullListOfScalars = new GraphQLNonNull(ListOfScalarsType);
/* c8 ignore next */
const dummyFunc = () => expect.fail('Never called and used as a placeholder');
describe('Type System: Scalars', () => {
it('accepts a Scalar type defining serialize', () => {
expect(() => new GraphQLScalarType({ name: 'SomeScalar' })).to.not.throw();
});
it('accepts a Scalar type defining specifiedByURL', () => {
expect(
() =>
new GraphQLScalarType({
name: 'SomeScalar',
specifiedByURL: 'https://example.com/foo_spec',
}),
).not.to.throw();
});
it('accepts a Scalar type defining parseValue and parseLiteral', () => {
expect(
() =>
new GraphQLScalarType({
name: 'SomeScalar',
parseValue: dummyFunc,
parseLiteral: dummyFunc,
}),
).to.not.throw();
});
it('provides default methods if omitted', () => {
const scalar = new GraphQLScalarType({ name: 'Foo' });
expect(scalar.serialize).to.equal(identityFunc);
expect(scalar.parseValue).to.equal(identityFunc);
expect(scalar.parseLiteral).to.be.a('function');
});
it('use parseValue for parsing literals if parseLiteral omitted', () => {
const scalar = new GraphQLScalarType({
name: 'Foo',
parseValue(value) {
return 'parseValue: ' + inspect(value);
},
});
expect(scalar.parseLiteral(parseValue('null'))).to.equal(
'parseValue: null',
);
expect(scalar.parseLiteral(parseValue('{ foo: "bar" }'))).to.equal(
'parseValue: { foo: "bar" }',
);
expect(
scalar.parseLiteral(parseValue('{ foo: { bar: $var } }'), { var: 'baz' }),
).to.equal('parseValue: { foo: { bar: "baz" } }');
});
it('rejects a Scalar type defining parseLiteral but not parseValue', () => {
expect(
() =>
new GraphQLScalarType({
name: 'SomeScalar',
parseLiteral: dummyFunc,
}),
).to.throw(
'SomeScalar must provide both "parseValue" and "parseLiteral" functions.',
);
});
});
describe('Type System: Objects', () => {
it('does not mutate passed field definitions', () => {
const outputFields = {
field1: { type: ScalarType },
field2: {
type: ScalarType,
args: {
id: { type: ScalarType },
},
},
};
const testObject1 = new GraphQLObjectType({
name: 'Test',
fields: outputFields,
});
const testObject2 = new GraphQLObjectType({
name: 'Test',
fields: outputFields,
});
expect(testObject1.getFields()).to.deep.equal(testObject2.getFields());
expect(outputFields).to.deep.equal({
field1: {
type: ScalarType,
},
field2: {
type: ScalarType,
args: {
id: { type: ScalarType },
},
},
});
const inputFields = {
field1: { type: ScalarType },
field2: { type: ScalarType },
};
const testInputObject1 = new GraphQLInputObjectType({
name: 'Test',
fields: inputFields,
});
const testInputObject2 = new GraphQLInputObjectType({
name: 'Test',
fields: inputFields,
});
expect(testInputObject1.getFields()).to.deep.equal(
testInputObject2.getFields(),
);
expect(inputFields).to.deep.equal({
field1: { type: ScalarType },
field2: { type: ScalarType },
});
});
it('defines an object type with deprecated field', () => {
const TypeWithDeprecatedField = new GraphQLObjectType({
name: 'foo',
fields: {
bar: {
type: ScalarType,
deprecationReason: 'A terrible reason',
},
baz: {
type: ScalarType,
deprecationReason: '',
},
},
});
expect(TypeWithDeprecatedField.getFields().bar).to.include({
name: 'bar',
deprecationReason: 'A terrible reason',
});
expect(TypeWithDeprecatedField.getFields().baz).to.include({
name: 'baz',
deprecationReason: '',
});
});
it('accepts an Object type with a field function', () => {
const objType = new GraphQLObjectType({
name: 'SomeObject',
fields: () => ({
f: { type: ScalarType },
}),
});
expect(objType.getFields().f).to.deep.include({
coordinate: 'SomeObject.f',
name: 'f',
description: undefined,
type: ScalarType,
args: [],
resolve: undefined,
subscribe: undefined,
deprecationReason: undefined,
extensions: {},
astNode: undefined,
});
});
it('accepts an Object type with field args', () => {
const objType = new GraphQLObjectType({
name: 'SomeObject',
fields: {
f: {
type: ScalarType,
args: {
arg: { type: ScalarType },
},
},
},
});
const f = objType.getFields().f;
expect(f).to.deep.include({
coordinate: 'SomeObject.f',
name: 'f',
description: undefined,
type: ScalarType,
resolve: undefined,
subscribe: undefined,
deprecationReason: undefined,
extensions: {},
astNode: undefined,
});
expect(f.args).to.have.lengthOf(1);
expect(f.args[0]).to.deep.include({
coordinate: 'SomeObject.f(arg:)',
name: 'arg',
description: undefined,
type: ScalarType,
defaultValue: undefined,
deprecationReason: undefined,
extensions: {},
astNode: undefined,
});
});
it('accepts an Object type with array interfaces', () => {
const objType = new GraphQLObjectType({
name: 'SomeObject',
fields: {},
interfaces: [InterfaceType],
});
expect(objType.getInterfaces()).to.deep.equal([InterfaceType]);
});
it('accepts an Object type with interfaces as a function returning an array', () => {
const objType = new GraphQLObjectType({
name: 'SomeObject',
fields: {},
interfaces: () => [InterfaceType],
});
expect(objType.getInterfaces()).to.deep.equal([InterfaceType]);
});
it('accepts a lambda as an Object field resolver', () => {
const objType = new GraphQLObjectType({
name: 'SomeObject',
fields: {
f: {
type: ScalarType,
resolve: dummyFunc,
},
},
});
expect(() => objType.getFields()).to.not.throw();
});
it('rejects an Object type with invalid name', () => {
expect(
() => new GraphQLObjectType({ name: 'bad-name', fields: {} }),
).to.throw('Names must only contain [_a-zA-Z0-9] but "bad-name" does not.');
});
it('rejects an Object type with incorrectly named fields', () => {
const objType = new GraphQLObjectType({
name: 'SomeObject',
fields: {
'bad-name': { type: ScalarType },
},
});
expect(() => objType.getFields()).to.throw(
'Names must only contain [_a-zA-Z0-9] but "bad-name" does not.',
);
});
it('rejects an Object type with a field function that returns incorrect type', () => {
const objType = new GraphQLObjectType({
name: 'SomeObject',
// @ts-expect-error (Wrong type of return)
fields() {
return [{ field: ScalarType }];
},
});
expect(() => objType.getFields()).to.throw();
});
it('rejects an Object type with incorrectly named field args', () => {
const objType = new GraphQLObjectType({
name: 'SomeObject',
fields: {
badField: {
type: ScalarType,
args: {
'bad-name': { type: ScalarType },
},
},
},
});
expect(() => objType.getFields()).to.throw(
'Names must only contain [_a-zA-Z0-9] but "bad-name" does not.',
);
});
});
describe('Type System: Interfaces', () => {
it('accepts an Interface type defining resolveType', () => {
expect(
() =>
new GraphQLInterfaceType({
name: 'AnotherInterface',
fields: { f: { type: ScalarType } },
}),
).to.not.throw();
});
it('accepts an Interface type with an array of interfaces', () => {
const implementing = new GraphQLInterfaceType({
name: 'AnotherInterface',
fields: {},
interfaces: [InterfaceType],
});
expect(implementing.getInterfaces()).to.deep.equal([InterfaceType]);
});
it('accepts an Interface type with interfaces as a function returning an array', () => {
const implementing = new GraphQLInterfaceType({
name: 'AnotherInterface',
fields: {},
interfaces: () => [InterfaceType],
});
expect(implementing.getInterfaces()).to.deep.equal([InterfaceType]);
});
it('rejects an Interface type with invalid name', () => {
expect(
() => new GraphQLInterfaceType({ name: 'bad-name', fields: {} }),
).to.throw('Names must only contain [_a-zA-Z0-9] but "bad-name" does not.');
});
});
describe('Type System: Unions', () => {
it('accepts a Union type defining resolveType', () => {
expect(
() =>
new GraphQLUnionType({
name: 'SomeUnion',
types: [ObjectType],
}),
).to.not.throw();
});
it('accepts a Union type with array types', () => {
const unionType = new GraphQLUnionType({
name: 'SomeUnion',
types: [ObjectType],
});
expect(unionType.getTypes()).to.deep.equal([ObjectType]);
});
it('accepts a Union type with function returning an array of types', () => {
const unionType = new GraphQLUnionType({
name: 'SomeUnion',
types: () => [ObjectType],
});
expect(unionType.getTypes()).to.deep.equal([ObjectType]);
});
it('accepts a Union type without types', () => {
const unionType = new GraphQLUnionType({
name: 'SomeUnion',
types: [],
});
expect(unionType.getTypes()).to.deep.equal([]);
});
it('rejects an Union type with invalid name', () => {
expect(
() => new GraphQLUnionType({ name: 'bad-name', types: [] }),
).to.throw('Names must only contain [_a-zA-Z0-9] but "bad-name" does not.');
});
});
describe('Type System: Enums', () => {
it('defines an enum type with deprecated value', () => {
const EnumTypeWithDeprecatedValue = new GraphQLEnumType({
name: 'EnumWithDeprecatedValue',
values: {
foo: { deprecationReason: 'Just because' },
bar: { deprecationReason: '' },
},
});
expect(EnumTypeWithDeprecatedValue.getValues()[0]).to.include({
name: 'foo',
deprecationReason: 'Just because',
});
expect(EnumTypeWithDeprecatedValue.getValues()[1]).to.include({
name: 'bar',
deprecationReason: '',
});
});
it('defines an enum type with a value of `null` and `undefined`', () => {
const EnumTypeWithNullishValue = new GraphQLEnumType({
name: 'EnumWithNullishValue',
values: {
NULL: { value: null },
NAN: { value: NaN },
NO_CUSTOM_VALUE: { value: undefined },
},
});
const values = EnumTypeWithNullishValue.getValues();
expect(values).to.have.lengthOf(3);
expect(values[0]).to.deep.include({
coordinate: 'EnumWithNullishValue.NULL',
name: 'NULL',
description: undefined,
value: null,
deprecationReason: undefined,
extensions: {},
astNode: undefined,
});
expect(values[1]).to.deep.include({
coordinate: 'EnumWithNullishValue.NAN',
name: 'NAN',
description: undefined,
value: NaN,
deprecationReason: undefined,
extensions: {},
astNode: undefined,
});
expect(values[2]).to.deep.include({
coordinate: 'EnumWithNullishValue.NO_CUSTOM_VALUE',
name: 'NO_CUSTOM_VALUE',
description: undefined,
value: 'NO_CUSTOM_VALUE',
deprecationReason: undefined,
extensions: {},
astNode: undefined,
});
});
it('accepts a well defined Enum type with empty value definition', () => {
const enumType = new GraphQLEnumType({
name: 'SomeEnum',
values: {
FOO: {},
BAR: {},
},
});
expect(enumType.getValue('FOO')).has.property('value', 'FOO');
expect(enumType.getValue('BAR')).has.property('value', 'BAR');
});
it('accepts a well defined Enum type with internal value definition', () => {
const enumType = new GraphQLEnumType({
name: 'SomeEnum',
values: {
FOO: { value: 10 },
BAR: { value: 20 },
},
});
expect(enumType.getValue('FOO')).has.property('value', 10);
expect(enumType.getValue('BAR')).has.property('value', 20);
});
it('rejects an Enum type with invalid name', () => {
expect(
() => new GraphQLEnumType({ name: 'bad-name', values: {} }),
).to.throw('Names must only contain [_a-zA-Z0-9] but "bad-name" does not.');
});
it('rejects an Enum type with incorrectly named values', () => {
expect(
() =>
new GraphQLEnumType({
name: 'SomeEnum',
values: {
'bad-name': {},
},
}),
).to.throw('Names must only contain [_a-zA-Z0-9] but "bad-name" does not.');
});
});
describe('Type System: Input Objects', () => {
describe('Input Objects must have fields', () => {
it('accepts an Input Object type with fields', () => {
const inputObjType = new GraphQLInputObjectType({
name: 'SomeInputObject',
fields: {
f: { type: ScalarType },
},
});
expect(inputObjType.getFields().f).to.deep.include({
coordinate: 'SomeInputObject.f',
name: 'f',
description: undefined,
type: ScalarType,
defaultValue: undefined,
deprecationReason: undefined,
extensions: {},
astNode: undefined,
});
});
it('accepts an Input Object type with a field function', () => {
const inputObjType = new GraphQLInputObjectType({
name: 'SomeInputObject',
fields: () => ({
f: { type: ScalarType },
}),
});
expect(inputObjType.getFields().f).to.deep.include({
coordinate: 'SomeInputObject.f',
name: 'f',
description: undefined,
type: ScalarType,
defaultValue: undefined,
extensions: {},
deprecationReason: undefined,
astNode: undefined,
});
});
it('rejects an Input Object type with invalid name', () => {
expect(
() => new GraphQLInputObjectType({ name: 'bad-name', fields: {} }),
).to.throw(
'Names must only contain [_a-zA-Z0-9] but "bad-name" does not.',
);
});
it('rejects an Input Object type with incorrectly named fields', () => {
const inputObjType = new GraphQLInputObjectType({
name: 'SomeInputObject',
fields: {
'bad-name': { type: ScalarType },
},
});
expect(() => inputObjType.getFields()).to.throw(
'Names must only contain [_a-zA-Z0-9] but "bad-name" does not.',
);
});
});
it('Deprecation reason is preserved on fields', () => {
const inputObjType = new GraphQLInputObjectType({
name: 'SomeInputObject',
fields: {
deprecatedField: {
type: ScalarType,
deprecationReason: 'not used anymore',
},
},
});
expect(inputObjType.toConfig()).to.have.nested.property(
'fields.deprecatedField.deprecationReason',
'not used anymore',
);
});
describe('Input Object fields may have default values', () => {
it('accepts an Input Object type with a default value', () => {
const inputObjType = new GraphQLInputObjectType({
name: 'SomeInputObject',
fields: {
f: { type: ScalarType, defaultValue: 3 },
},
});
expect(inputObjType.getFields().f).to.deep.include({
coordinate: 'SomeInputObject.f',
name: 'f',
description: undefined,
type: ScalarType,
defaultValue: { value: 3 },
deprecationReason: undefined,
extensions: {},
astNode: undefined,
});
});
it('accepts an Input Object type with a default value literal', () => {
const inputObjType = new GraphQLInputObjectType({
name: 'SomeInputObject',
fields: {
f: {
type: ScalarType,
defaultValueLiteral: { kind: Kind.INT, value: '3' },
},
},
});
expect(inputObjType.getFields().f).to.deep.include({
coordinate: 'SomeInputObject.f',
name: 'f',
description: undefined,
type: ScalarType,
defaultValue: { literal: { kind: 'IntValue', value: '3' } },
deprecationReason: undefined,
extensions: {},
astNode: undefined,
});
});
it('rejects an Input Object type with potentially conflicting default values', () => {
const inputObjType = new GraphQLInputObjectType({
name: 'SomeInputObject',
fields: {
f: {
type: ScalarType,
defaultValue: 3,
defaultValueLiteral: { kind: Kind.INT, value: '3' },
},
},
});
expect(() => inputObjType.getFields()).to.throw(
'f has both a defaultValue and a defaultValueLiteral property, but only one must be provided.',
);
});
});
});
describe('Type System: List', () => {
function expectList(type: GraphQLType) {
return expect(() => new GraphQLList(type));
}
it('accepts a type as item type of list', () => {
expectList(ScalarType).to.not.throw();
expectList(ObjectType).to.not.throw();
expectList(UnionType).to.not.throw();
expectList(InterfaceType).to.not.throw();
expectList(EnumType).to.not.throw();
expectList(InputObjectType).to.not.throw();
expectList(ListOfScalarsType).to.not.throw();
expectList(NonNullScalarType).to.not.throw();
});
});
describe('Type System: Non-Null', () => {
function expectNonNull(type: GraphQLNullableType) {
return expect(() => new GraphQLNonNull(type));
}
it('accepts a type as nullable type of non-null', () => {
expectNonNull(ScalarType).to.not.throw();
expectNonNull(ObjectType).to.not.throw();
expectNonNull(UnionType).to.not.throw();
expectNonNull(InterfaceType).to.not.throw();
expectNonNull(EnumType).to.not.throw();
expectNonNull(InputObjectType).to.not.throw();
expectNonNull(ListOfScalarsType).to.not.throw();
expectNonNull(ListOfNonNullScalarsType).to.not.throw();
});
});
describe('Type System: test utility methods', () => {
it('stringifies types', () => {
expect(String(ScalarType)).to.equal('Scalar');
expect(String(ObjectType)).to.equal('Object');
expect(String(InterfaceType)).to.equal('Interface');
expect(String(UnionType)).to.equal('Union');
expect(String(EnumType)).to.equal('Enum');
expect(String(InputObjectType)).to.equal('InputObject');
expect(String(NonNullScalarType)).to.equal('Scalar!');
expect(String(ListOfScalarsType)).to.equal('[Scalar]');
expect(String(NonNullListOfScalars)).to.equal('[Scalar]!');
expect(String(ListOfNonNullScalarsType)).to.equal('[Scalar!]');
expect(String(new GraphQLList(ListOfScalarsType))).to.equal('[[Scalar]]');
});
it('JSON.stringifies types', () => {
expect(JSON.stringify(ScalarType)).to.equal('"Scalar"');
expect(JSON.stringify(ObjectType)).to.equal('"Object"');
expect(JSON.stringify(InterfaceType)).to.equal('"Interface"');
expect(JSON.stringify(UnionType)).to.equal('"Union"');
expect(JSON.stringify(EnumType)).to.equal('"Enum"');
expect(JSON.stringify(InputObjectType)).to.equal('"InputObject"');
expect(JSON.stringify(NonNullScalarType)).to.equal('"Scalar!"');
expect(JSON.stringify(ListOfScalarsType)).to.equal('"[Scalar]"');
expect(JSON.stringify(NonNullListOfScalars)).to.equal('"[Scalar]!"');
expect(JSON.stringify(ListOfNonNullScalarsType)).to.equal('"[Scalar!]"');
expect(JSON.stringify(new GraphQLList(ListOfScalarsType))).to.equal(
'"[[Scalar]]"',
);
});
it('Object.toStringifies types', () => {
function toString(obj: unknown): string {
return Object.prototype.toString.call(obj);
}
expect(toString(ScalarType)).to.equal('[object GraphQLScalarType]');
expect(toString(ObjectType)).to.equal('[object GraphQLObjectType]');
expect(toString(InterfaceType)).to.equal('[object GraphQLInterfaceType]');
expect(toString(UnionType)).to.equal('[object GraphQLUnionType]');
expect(toString(EnumType)).to.equal('[object GraphQLEnumType]');
expect(toString(InputObjectType)).to.equal(
'[object GraphQLInputObjectType]',
);
expect(toString(NonNullScalarType)).to.equal('[object GraphQLNonNull]');
expect(toString(ListOfScalarsType)).to.equal('[object GraphQLList]');
});
});