forked from graphql/graphql-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
definition-test.js
982 lines (891 loc) · 28.5 KB
/
definition-test.js
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
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
// @flow strict
import { expect } from 'chai';
import { describe, it } from 'mocha';
import inspect from '../../jsutils/inspect';
import identityFunc from '../../jsutils/identityFunc';
import { parseValue } from '../../language/parser';
import type { GraphQLType, GraphQLNullableType } from '../definition';
import {
GraphQLScalarType,
GraphQLObjectType,
GraphQLInterfaceType,
GraphQLUnionType,
GraphQLEnumType,
GraphQLInputObjectType,
GraphQLList,
GraphQLNonNull,
} from '../definition';
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 = GraphQLList(ScalarType);
const NonNullScalarType = GraphQLNonNull(ScalarType);
const ListOfNonNullScalarsType = GraphQLList(NonNullScalarType);
const NonNullListOfScalars = GraphQLNonNull(ListOfScalarsType);
// istanbul ignore next (Never called and used as a placeholder)
const dummyFunc = () => {
/* empty */
};
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" }',
);
});
it('rejects a Scalar type without name', () => {
// $FlowExpectedError
expect(() => new GraphQLScalarType({})).to.throw('Must provide name.');
});
it('rejects a Scalar type defining serialize with an incorrect type', () => {
expect(
() =>
new GraphQLScalarType({
name: 'SomeScalar',
// $FlowExpectedError
serialize: {},
}),
).to.throw(
'SomeScalar must provide "serialize" function. If this custom Scalar is also used as an input type, ensure "parseValue" and "parseLiteral" functions are also provided.',
);
});
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.',
);
});
it('rejects a Scalar type defining parseValue and parseLiteral with an incorrect type', () => {
expect(
() =>
new GraphQLScalarType({
name: 'SomeScalar',
// $FlowExpectedError
parseValue: {},
// $FlowExpectedError
parseLiteral: {},
}),
).to.throw(
'SomeScalar must provide both "parseValue" and "parseLiteral" functions.',
);
});
it('rejects a Scalar type defining specifiedByUrl with an incorrect type', () => {
expect(
() =>
new GraphQLScalarType({
name: 'SomeScalar',
// $FlowExpectedError
specifiedByUrl: {},
}),
).to.throw(
'SomeScalar must provide "specifiedByUrl" as a string, but got: {}.',
);
});
});
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: 'Test1',
fields: outputFields,
});
const testObject2 = new GraphQLObjectType({
name: 'Test2',
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: 'Test1',
fields: inputFields,
});
const testInputObject2 = new GraphQLInputObjectType({
name: 'Test2',
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',
isDeprecated: true,
deprecationReason: 'A terrible reason',
});
expect(TypeWithDeprecatedField.getFields().baz).to.include({
name: 'baz',
isDeprecated: true,
deprecationReason: '',
});
});
it('accepts an Object type with a field function', () => {
const objType = new GraphQLObjectType({
name: 'SomeObject',
fields: () => ({
f: { type: ScalarType },
}),
});
expect(objType.getFields()).to.deep.equal({
f: {
name: 'f',
description: undefined,
type: ScalarType,
args: [],
resolve: undefined,
subscribe: undefined,
isDeprecated: false,
deprecationReason: undefined,
extensions: undefined,
astNode: undefined,
},
});
});
it('accepts an Object type with field args', () => {
const objType = new GraphQLObjectType({
name: 'SomeObject',
fields: {
f: {
type: ScalarType,
args: {
arg: { type: ScalarType },
},
},
},
});
expect(objType.getFields()).to.deep.equal({
f: {
name: 'f',
description: undefined,
type: ScalarType,
args: [
{
name: 'arg',
description: undefined,
type: ScalarType,
defaultValue: undefined,
isDeprecated: false,
deprecationReason: undefined,
extensions: undefined,
astNode: undefined,
},
],
resolve: undefined,
subscribe: undefined,
isDeprecated: false,
deprecationReason: undefined,
extensions: undefined,
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 without name', () => {
// $FlowExpectedError
expect(() => new GraphQLObjectType({})).to.throw('Must provide name.');
});
it('rejects an Object type field with undefined config', () => {
const objType = new GraphQLObjectType({
name: 'SomeObject',
fields: {
// $FlowExpectedError
f: undefined,
},
});
expect(() => objType.getFields()).to.throw(
'SomeObject.f field config must be an object.',
);
});
it('rejects an Object type with incorrectly typed fields', () => {
const objType = new GraphQLObjectType({
name: 'SomeObject',
// $FlowExpectedError
fields: [{ field: ScalarType }],
});
expect(() => objType.getFields()).to.throw(
'SomeObject fields must be an object with field names as keys or a function which returns such an object.',
);
});
it('rejects an Object type with a field function that returns incorrect type', () => {
const objType = new GraphQLObjectType({
name: 'SomeObject',
// $FlowExpectedError
fields() {
return [{ field: ScalarType }];
},
});
expect(() => objType.getFields()).to.throw(
'SomeObject fields must be an object with field names as keys or a function which returns such an object.',
);
});
it('rejects an Object type with incorrectly typed field args', () => {
const objType = new GraphQLObjectType({
name: 'SomeObject',
fields: {
badField: {
type: ScalarType,
// $FlowExpectedError
args: [{ badArg: ScalarType }],
},
},
});
expect(() => objType.getFields()).to.throw(
'SomeObject.badField args must be an object with argument names as keys.',
);
});
it('rejects an Object type with an isDeprecated instead of deprecationReason on field', () => {
const OldObject = new GraphQLObjectType({
name: 'OldObject',
// $FlowExpectedError
fields: {
field: { type: ScalarType, isDeprecated: true },
},
});
expect(() => OldObject.getFields()).to.throw(
'OldObject.field should provide "deprecationReason" instead of "isDeprecated".',
);
});
it('rejects an Object type with incorrectly typed interfaces', () => {
const objType = new GraphQLObjectType({
name: 'SomeObject',
fields: {},
// $FlowExpectedError
interfaces: {},
});
expect(() => objType.getInterfaces()).to.throw(
'SomeObject interfaces must be an Array or a function which returns an Array.',
);
});
it('rejects an Object type with interfaces as a function returning an incorrect type', () => {
const objType = new GraphQLObjectType({
name: 'SomeObject',
fields: {},
// $FlowExpectedError
interfaces() {
return {};
},
});
expect(() => objType.getInterfaces()).to.throw(
'SomeObject interfaces must be an Array or a function which returns an Array.',
);
});
it('rejects an empty Object field resolver', () => {
const objType = new GraphQLObjectType({
name: 'SomeObject',
// $FlowExpectedError
fields: {
field: { type: ScalarType, resolve: {} },
},
});
expect(() => objType.getFields()).to.throw(
'SomeObject.field field resolver must be a function if provided, but got: {}.',
);
});
it('rejects a constant scalar value resolver', () => {
const objType = new GraphQLObjectType({
name: 'SomeObject',
// $FlowExpectedError
fields: {
field: { type: ScalarType, resolve: 0 },
},
});
expect(() => objType.getFields()).to.throw(
'SomeObject.field field resolver must be a function if provided, but got: 0.',
);
});
it('rejects an Object type with an incorrect type for isTypeOf', () => {
expect(
() =>
new GraphQLObjectType({
name: 'AnotherObject',
fields: {},
// $FlowExpectedError
isTypeOf: {},
}),
).to.throw(
'AnotherObject must provide "isTypeOf" as a function, but got: {}.',
);
});
});
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 without name', () => {
// $FlowExpectedError
expect(() => new GraphQLInterfaceType({})).to.throw('Must provide name.');
});
it('rejects an Interface type with incorrectly typed interfaces', () => {
const objType = new GraphQLInterfaceType({
name: 'AnotherInterface',
fields: {},
// $FlowExpectedError
interfaces: {},
});
expect(() => objType.getInterfaces()).to.throw(
'AnotherInterface interfaces must be an Array or a function which returns an Array.',
);
});
it('rejects an Interface type with interfaces as a function returning an incorrect type', () => {
const objType = new GraphQLInterfaceType({
name: 'AnotherInterface',
fields: {},
// $FlowExpectedError
interfaces() {
return {};
},
});
expect(() => objType.getInterfaces()).to.throw(
'AnotherInterface interfaces must be an Array or a function which returns an Array.',
);
});
it('rejects an Interface type with an incorrect type for resolveType', () => {
expect(
() =>
new GraphQLInterfaceType({
name: 'AnotherInterface',
fields: {},
// $FlowExpectedError
resolveType: {},
}),
).to.throw(
'AnotherInterface must provide "resolveType" as a function, but got: {}.',
);
});
});
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 without name', () => {
// $FlowExpectedError
expect(() => new GraphQLUnionType({})).to.throw('Must provide name.');
});
it('rejects an Union type with an incorrect type for resolveType', () => {
expect(
() =>
new GraphQLUnionType({
name: 'SomeUnion',
types: [],
// $FlowExpectedError
resolveType: {},
}),
).to.throw(
'SomeUnion must provide "resolveType" as a function, but got: {}.',
);
});
it('rejects a Union type with incorrectly typed types', () => {
const unionType = new GraphQLUnionType({
name: 'SomeUnion',
// $FlowExpectedError
types: { ObjectType },
});
expect(() => unionType.getTypes()).to.throw(
'Must provide Array of types or a function which returns such an array for Union SomeUnion.',
);
});
});
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',
isDeprecated: true,
deprecationReason: 'Just because',
});
expect(EnumTypeWithDeprecatedValue.getValues()[1]).to.include({
name: 'bar',
isDeprecated: true,
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 },
},
});
expect(EnumTypeWithNullishValue.getValues()).to.deep.equal([
{
name: 'NULL',
description: undefined,
value: null,
isDeprecated: false,
deprecationReason: undefined,
extensions: undefined,
astNode: undefined,
},
{
name: 'NAN',
description: undefined,
value: NaN,
isDeprecated: false,
deprecationReason: undefined,
extensions: undefined,
astNode: undefined,
},
{
name: 'NO_CUSTOM_VALUE',
description: undefined,
value: 'NO_CUSTOM_VALUE',
isDeprecated: false,
deprecationReason: undefined,
extensions: undefined,
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 without name', () => {
// $FlowExpectedError
expect(() => new GraphQLEnumType({ values: {} })).to.throw(
'Must provide name.',
);
});
it('rejects an Enum type with incorrectly typed values', () => {
expect(
() =>
new GraphQLEnumType({
name: 'SomeEnum',
// $FlowExpectedError
values: [{ FOO: 10 }],
}),
).to.throw('SomeEnum values must be an object with value names as keys.');
});
it('rejects an Enum type with missing value definition', () => {
expect(
() =>
new GraphQLEnumType({
name: 'SomeEnum',
// $FlowExpectedError
values: { FOO: null },
}),
).to.throw(
'SomeEnum.FOO must refer to an object with a "value" key representing an internal value but got: null.',
);
});
it('rejects an Enum type with incorrectly typed value definition', () => {
expect(
() =>
new GraphQLEnumType({
name: 'SomeEnum',
// $FlowExpectedError
values: { FOO: 10 },
}),
).to.throw(
'SomeEnum.FOO must refer to an object with a "value" key representing an internal value but got: 10.',
);
});
it('does not allow isDeprecated instead of deprecationReason on enum', () => {
expect(
() =>
new GraphQLEnumType({
name: 'SomeEnum',
// $FlowExpectedError
values: {
FOO: { isDeprecated: true },
},
}),
).to.throw(
'SomeEnum.FOO should provide "deprecationReason" instead of "isDeprecated".',
);
});
});
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()).to.deep.equal({
f: {
name: 'f',
description: undefined,
type: ScalarType,
defaultValue: undefined,
isDeprecated: false,
deprecationReason: undefined,
extensions: undefined,
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()).to.deep.equal({
f: {
name: 'f',
description: undefined,
type: ScalarType,
defaultValue: undefined,
extensions: undefined,
isDeprecated: false,
deprecationReason: undefined,
astNode: undefined,
},
});
});
it('rejects an Input Object type without name', () => {
// $FlowExpectedError
expect(() => new GraphQLInputObjectType({})).to.throw(
'Must provide name.',
);
});
it('rejects an Input Object type with incorrect fields', () => {
const inputObjType = new GraphQLInputObjectType({
name: 'SomeInputObject',
// $FlowExpectedError
fields: [],
});
expect(() => inputObjType.getFields()).to.throw(
'SomeInputObject fields must be an object with field names as keys or a function which returns such an object.',
);
});
it('rejects an Input Object type with fields function that returns incorrect type', () => {
const inputObjType = new GraphQLInputObjectType({
name: 'SomeInputObject',
// $FlowExpectedError
fields: () => [],
});
expect(() => inputObjType.getFields()).to.throw(
'SomeInputObject fields must be an object with field names as keys or a function which returns such an object.',
);
});
});
describe('Input Object fields must not have resolvers', () => {
it('rejects an Input Object type with resolvers', () => {
const inputObjType = new GraphQLInputObjectType({
name: 'SomeInputObject',
// $FlowExpectedError
fields: {
f: { type: ScalarType, resolve: dummyFunc },
},
});
expect(() => inputObjType.getFields()).to.throw(
'SomeInputObject.f field has a resolve property, but Input Types cannot define resolvers.',
);
});
it('rejects an Input Object type with resolver constant', () => {
const inputObjType = new GraphQLInputObjectType({
name: 'SomeInputObject',
// $FlowExpectedError
fields: {
f: { type: ScalarType, resolve: {} },
},
});
expect(() => inputObjType.getFields()).to.throw(
'SomeInputObject.f field has a resolve property, but Input Types cannot define resolvers.',
);
});
});
});
describe('Type System: List', () => {
function expectList(type: GraphQLType) {
return expect(() => GraphQLList(type));
}
it('accepts an 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();
});
it('rejects a non-type as item type of list', () => {
// $FlowExpectedError
expectList({}).to.throw('Expected {} to be a GraphQL type.');
// $FlowExpectedError
expectList(String).to.throw(
'Expected [function String] to be a GraphQL type.',
);
// $FlowExpectedError
expectList(null).to.throw('Expected null to be a GraphQL type.');
// $FlowExpectedError
expectList(undefined).to.throw('Expected undefined to be a GraphQL type.');
});
});
describe('Type System: Non-Null', () => {
function expectNonNull(type: GraphQLNullableType) {
return expect(() => GraphQLNonNull(type));
}
it('accepts an 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();
});
it('rejects a non-type as nullable type of non-null', () => {
// $FlowExpectedError
expectNonNull(NonNullScalarType).to.throw(
'Expected Scalar! to be a GraphQL nullable type.',
);
// $FlowExpectedError
expectNonNull({}).to.throw('Expected {} to be a GraphQL nullable type.');
// $FlowExpectedError
expectNonNull(String).to.throw(
'Expected [function String] to be a GraphQL nullable type.',
);
// $FlowExpectedError
expectNonNull(null).to.throw(
'Expected null to be a GraphQL nullable type.',
);
// $FlowExpectedError
expectNonNull(undefined).to.throw(
'Expected undefined to be a GraphQL nullable type.',
);
});
});
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(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(GraphQLList(ListOfScalarsType))).to.equal(
'"[[Scalar]]"',
);
});
it('Object.toStringifies types', () => {
function toString(obj) {
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]');
});
});