-
-
Notifications
You must be signed in to change notification settings - Fork 76
/
TypeMapper-test.js
1230 lines (1072 loc) · 44 KB
/
TypeMapper-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
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* @flow strict */
import { GraphQLSchema } from 'graphql';
import {
GraphQLString,
GraphQLFloat,
GraphQLInt,
GraphQLBoolean,
GraphQLID,
GraphQLObjectType,
GraphQLInputObjectType,
GraphQLList,
GraphQLNonNull,
GraphQLEnumType,
GraphQLScalarType,
GraphQLInterfaceType,
} from '../graphql';
import { schemaComposer as sc } from '..';
import { SchemaComposer } from '../SchemaComposer';
import { graphqlVersion } from '../utils/graphqlVersion';
import { GraphQLDate, GraphQLBuffer, GraphQLJSON, GraphQLJSONObject } from '../type';
import { ObjectTypeComposer } from '../ObjectTypeComposer';
import { InputTypeComposer } from '../InputTypeComposer';
import { ScalarTypeComposer } from '../ScalarTypeComposer';
import { EnumTypeComposer } from '../EnumTypeComposer';
import { InterfaceTypeComposer } from '../InterfaceTypeComposer';
import { UnionTypeComposer } from '../UnionTypeComposer';
import { Resolver } from '../Resolver';
import { TypeMapper } from '../TypeMapper';
import { ListComposer } from '../ListComposer';
import { NonNullComposer } from '../NonNullComposer';
import { ThunkComposer } from '../ThunkComposer';
let typeMapper;
beforeEach(() => {
sc.clear();
typeMapper = new TypeMapper(sc);
});
describe('TypeMapper', () => {
it('should provide default scalar GraphQL types', () => {
expect((typeMapper.getBuiltInType('String'): any).getType()).toBe(GraphQLString);
expect((typeMapper.getBuiltInType('Float'): any).getType()).toBe(GraphQLFloat);
expect((typeMapper.getBuiltInType('Int'): any).getType()).toBe(GraphQLInt);
expect((typeMapper.getBuiltInType('Boolean'): any).getType()).toBe(GraphQLBoolean);
expect((typeMapper.getBuiltInType('ID'): any).getType()).toBe(GraphQLID);
expect((typeMapper.getBuiltInType('JSON'): any).getType()).toBe(GraphQLJSON);
expect((typeMapper.getBuiltInType('JSONObject'): any).getType()).toBe(GraphQLJSONObject);
expect((typeMapper.getBuiltInType('Date'): any).getType()).toBe(GraphQLDate);
expect((typeMapper.getBuiltInType('Buffer'): any).getType()).toBe(GraphQLBuffer);
});
it('should not add basic scalars if they already provided', () => {
const CustomJSON = new GraphQLScalarType({
name: 'JSON',
serialize: () => {},
});
const CustomDate = new GraphQLScalarType({
name: 'Date',
serialize: () => {},
});
const Query = new GraphQLObjectType({
name: 'Query',
fields: {
json: { type: CustomJSON },
date: { type: CustomDate },
},
});
const schema = new GraphQLSchema({
query: Query,
});
const sc2 = new SchemaComposer(schema);
expect(sc2.get('JSON').getType()).toBe(CustomJSON);
expect(sc2.get('Date').getType()).toBe(CustomDate);
});
it('should allow to override basic graphql-compose types', () => {
const CustomJSON = sc.createScalarTC({
name: 'CustomJSON',
serialize: () => {},
});
const CustomDate = sc.createScalarTC({
name: 'CustomDate',
serialize: () => {},
});
const CustomBuffer = sc.createScalarTC({
name: 'CustomBuffer',
serialize: () => {},
});
sc.set('JSON', CustomJSON);
sc.set('Date', CustomDate);
sc.set('Buffer', CustomBuffer);
expect(typeMapper.schemaComposer.get('JSON')).toBe(CustomJSON);
expect(typeMapper.schemaComposer.get('Date')).toBe(CustomDate);
expect(typeMapper.schemaComposer.get('Buffer')).toBe(CustomBuffer);
});
it('should create object type from template string', () => {
const tc: any = typeMapper.convertSDLTypeDefinition(
graphqlVersion < 12
? `
type IntRange {
# Max value
max: Int,
min: Int!
arr: [String]
}
`
: `
type IntRange {
"""Max value"""
max: Int,
min: Int!
arr: [String]
}
`
);
expect(tc).toBeInstanceOf(ObjectTypeComposer);
const type = tc.getType();
expect(type).toBeInstanceOf(GraphQLObjectType);
expect(typeMapper.schemaComposer.get('IntRange')).toBe(tc);
const IntRangeTC = new ObjectTypeComposer(type, sc);
expect(IntRangeTC.getTypeName()).toBe('IntRange');
expect(IntRangeTC.getFieldNames()).toEqual(expect.arrayContaining(['max', 'min', 'arr']));
expect(IntRangeTC.getFieldType('max')).toBe(GraphQLInt);
expect(IntRangeTC.getFieldConfig('max').description).toBe('Max value');
expect(IntRangeTC.getFieldType('min')).toBeInstanceOf(GraphQLNonNull);
expect(IntRangeTC.getFieldType('arr')).toBeInstanceOf(GraphQLList);
});
it('should create input object type from template string', () => {
const tc: any = typeMapper.convertSDLTypeDefinition(
`
input InputIntRange {
min: Int
max: Int!
}
`
);
expect(tc).toBeInstanceOf(InputTypeComposer);
const type = tc.getType();
expect(type).toBeInstanceOf(GraphQLInputObjectType);
expect(typeMapper.schemaComposer.get('InputIntRange')).toBe(tc);
const IntRangeTC: any = new InputTypeComposer(type, sc);
expect(IntRangeTC.getTypeName()).toBe('InputIntRange');
expect(IntRangeTC.getFieldType('min')).toBe(GraphQLInt);
expect(IntRangeTC.getFieldType('max')).toBeInstanceOf(GraphQLNonNull);
expect(IntRangeTC.getFieldType('max').ofType).toBe(GraphQLInt);
});
it('should create interface type from template string', () => {
const tc: any = typeMapper.convertSDLTypeDefinition(
graphqlVersion < 12
? `
interface IntRangeInterface {
# Max value
max: Int,
min: Int!
arr: [String]
}
`
: `
interface IntRangeInterface {
"""Max value"""
max: Int,
min: Int!
arr: [String]
}
`
);
expect(tc).toBeInstanceOf(InterfaceTypeComposer);
const type = tc.getType();
expect(type).toBeInstanceOf(GraphQLInterfaceType);
expect(typeMapper.schemaComposer.get('IntRangeInterface').getType()).toBe(type);
const IntRangeTC = new InterfaceTypeComposer(type, sc);
expect(IntRangeTC.getTypeName()).toBe('IntRangeInterface');
expect(IntRangeTC.getFieldNames()).toEqual(expect.arrayContaining(['max', 'min', 'arr']));
expect(IntRangeTC.getFieldType('max')).toBe(GraphQLInt);
expect(IntRangeTC.getFieldConfig('max').description).toBe('Max value');
expect(IntRangeTC.getFieldType('min')).toBeInstanceOf(GraphQLNonNull);
expect(IntRangeTC.getFieldType('arr')).toBeInstanceOf(GraphQLList);
});
it('should return wrapped type', () => {
expect(typeMapper.convertSDLWrappedTypeName('String!')).toBeInstanceOf(NonNullComposer);
expect(typeMapper.convertSDLWrappedTypeName('[String]')).toBeInstanceOf(ListComposer);
expect(typeMapper.convertSDLWrappedTypeName('[String]!')).toBeInstanceOf(NonNullComposer);
expect((typeMapper.convertSDLWrappedTypeName('[String]!'): any).ofType).toBeInstanceOf(
ListComposer
);
expect(typeMapper.convertSDLWrappedTypeName('String')).toBeInstanceOf(ScalarTypeComposer);
expect((typeMapper.convertSDLWrappedTypeName('String'): any).getType()).toBe(GraphQLString);
});
describe('convertOutputFieldConfig()', () => {
describe('converting field type', () => {
it('should accept type with GraphQLOutputType', () => {
const fc = typeMapper.convertOutputFieldConfig({
type: GraphQLString,
});
expect(fc.type).toBeInstanceOf(ScalarTypeComposer);
expect(fc.type.getType()).toBe(GraphQLString);
const objectType = new GraphQLObjectType({
name: 'SomeType',
fields: { f: { type: GraphQLString } },
});
const fc2 = typeMapper.convertOutputFieldConfig({
type: objectType,
});
expect(fc2.type).toBeInstanceOf(ObjectTypeComposer);
expect(fc2.type.getType()).toBe(objectType);
});
it('should accept GraphQLScalarType', () => {
const fc = typeMapper.convertOutputFieldConfig(GraphQLString);
expect(fc.type).toBeInstanceOf(ScalarTypeComposer);
expect(fc.type.getType()).toBe(GraphQLString);
});
it('should accept GraphQLObjectType', () => {
const type = new GraphQLObjectType({
name: 'Test',
fields: () => ({
a: { type: GraphQLInt },
}),
});
const fc = typeMapper.convertOutputFieldConfig(type);
expect(fc.type).toBeInstanceOf(ObjectTypeComposer);
expect(fc.type.getType()).toBe(type);
});
it('should accept GraphQLNonNull', () => {
const fc: any = typeMapper.convertOutputFieldConfig(new GraphQLNonNull(GraphQLString));
expect(fc.type).toBeInstanceOf(NonNullComposer);
expect(fc.type.getType().ofType).toBe(GraphQLString);
});
it('should accept GraphQLList', () => {
const fc: any = typeMapper.convertOutputFieldConfig(new GraphQLList(GraphQLString));
expect(fc.type).toBeInstanceOf(ListComposer);
expect(fc.type.getType().ofType).toBe(GraphQLString);
});
it('should accept type as string to scalar', () => {
const fc = typeMapper.convertOutputFieldConfig({
type: 'String',
});
expect(fc.type).toBeInstanceOf(ScalarTypeComposer);
expect(fc.type.getType()).toBe(GraphQLString);
});
it('should accept type as string to object type', () => {
ObjectTypeComposer.create('type Foo { id: ID! }', sc);
const fc = typeMapper.convertOutputFieldConfig({
type: 'Foo',
});
expect(fc.type).toBe(sc.getAnyTC('Foo'));
});
it('should accept type as string to enum type', () => {
EnumTypeComposer.create('enum Foo { A B }', sc);
const fc = typeMapper.convertOutputFieldConfig({
type: 'Foo',
});
expect(fc.type).toBe(sc.getAnyTC('Foo'));
});
it('should accept type as string to interface type', () => {
InterfaceTypeComposer.create('interface Foo { id: ID! }', sc);
const fc = typeMapper.convertOutputFieldConfig({
type: 'Foo',
});
expect(fc.type).toBe(sc.getAnyTC('Foo'));
});
it('should accept type as string to union type', () => {
ObjectTypeComposer.create('type A { id: ID! }', sc);
ObjectTypeComposer.create('type B { id: ID! }', sc);
UnionTypeComposer.create('union Foo = A | B', sc);
const fc = typeMapper.convertOutputFieldConfig({
type: 'Foo',
});
expect(fc.type).toBe(sc.getAnyTC('Foo'));
});
it('should create field config from type as string', () => {
const fc = typeMapper.convertOutputFieldConfig('String');
expect(fc.type).toBeInstanceOf(ScalarTypeComposer);
expect(fc.type.getType()).toBe(GraphQLString);
});
it('should lookup type name as string in schemaComposer', () => {
const tc = ObjectTypeComposer.create(`type MyType { a: Int }`, sc);
const fc = typeMapper.convertOutputFieldConfig('MyType');
expect(fc.type).toBe(tc);
const fc2: any = typeMapper.convertOutputFieldConfig({ type: '[MyType]' });
expect(fc2.type).toBeInstanceOf(ListComposer);
expect(fc2.type.getType().ofType).toBe(tc.getType());
});
it('should create field config from GraphQL Schema Language', () => {
const fc = typeMapper.convertOutputFieldConfig(
`type MyOutputType {
a: String,
b: Int,
}`
);
const tc: any = fc.type;
expect(tc.getTypeName()).toBe('MyOutputType');
expect(tc.getFieldType('a')).toBe(GraphQLString);
expect(tc.getFieldType('b')).toBe(GraphQLInt);
});
it('should create field with Scalar type from GraphQL Schema Language', () => {
const fc = typeMapper.convertOutputFieldConfig('scalar MyScalar');
expect(fc.type).toBeInstanceOf(ScalarTypeComposer);
expect(fc.type.getTypeName()).toBe('MyScalar');
});
it('should create field with Enum type from GraphQL Schema Language', () => {
const fc: any = typeMapper.convertOutputFieldConfig('enum MyEnum { AND OR }');
expect(fc.type).toBeInstanceOf(EnumTypeComposer);
const enumValues = fc.type.getFields();
expect(enumValues.AND).toMatchObject({ value: 'AND' });
expect(enumValues.OR).toMatchObject({ value: 'OR' });
});
it('should throw error if provided input type definition', () => {
expect(() => {
typeMapper.convertOutputFieldConfig(
`input MyInputType {
a: String,
}`
);
}).toThrowError(/but got input type definition/);
});
it('should accept ObjectTypeComposer', () => {
const tc = ObjectTypeComposer.create('type PriceRange { lon: Float, lat: Float }', sc);
tc.setDescription('Description');
const fc = typeMapper.convertOutputFieldConfig({
type: tc,
});
expect(fc.type).toBe(tc);
expect(fc.description).toBe(undefined);
const fc2 = typeMapper.convertOutputFieldConfig(tc);
expect(fc2.type).toBe(tc);
expect(fc2.description).toBe(undefined);
});
it('should accept ScalarTypeComposer', () => {
const stc = ScalarTypeComposer.create('scalar UInt', sc);
stc.setDescription('Description');
const fc = typeMapper.convertOutputFieldConfig({
type: stc,
});
expect(fc.type).toBe(stc);
expect(fc.description).toBe(undefined);
const fc2 = typeMapper.convertOutputFieldConfig(stc);
expect(fc2.type).toBe(stc);
expect(fc2.description).toBe(undefined);
});
it('should accept EnumTypeComposer', () => {
const etc = EnumTypeComposer.create('enum MyEnum { V1 V2 V3 }', sc);
etc.setDescription('Description');
const fc = typeMapper.convertOutputFieldConfig({
type: etc,
});
expect(fc.type).toBe(etc);
expect(fc.description).toBe(undefined);
const fc2 = typeMapper.convertOutputFieldConfig(etc);
expect(fc2.type).toBe(etc);
expect(fc2.description).toBe(undefined);
});
it('should accept InterfaceTypeComposer', () => {
const iftc = InterfaceTypeComposer.create('interface MyIFace { id: Int }', sc);
iftc.setDescription('Description');
const fc = typeMapper.convertOutputFieldConfig({
type: iftc,
});
expect(fc.type).toBe(iftc);
expect(fc.description).toBe(undefined);
const fc2 = typeMapper.convertOutputFieldConfig(iftc);
expect(fc2.type).toBe(iftc);
expect(fc2.description).toBe(undefined);
});
it('should accept Resolver', () => {
const resolver = new Resolver(
{
name: 'find',
type: 'Float',
args: {
a1: 'String',
},
resolve: () => 123,
},
sc
);
const fc: any = typeMapper.convertOutputFieldConfig(resolver);
expect(fc.type).toBeInstanceOf(ScalarTypeComposer);
expect(fc.type.getType()).toBe(GraphQLFloat);
expect(fc.args.a1.type.getType()).toBe(GraphQLString);
expect(fc.resolve()).toBe(123);
});
it('should accept Resolver as type', () => {
const resolver: any = new Resolver(
{
name: 'find',
type: 'Float',
args: {
a1: 'String',
},
resolve: () => 123,
},
sc
);
const fc = typeMapper.convertOutputFieldConfig({ type: resolver });
expect(fc.type).toBeInstanceOf(ScalarTypeComposer);
expect(fc.type.getType()).toBe(GraphQLFloat);
expect(fc.args).toEqual({});
expect(fc.resolve).toBe(undefined);
});
it('should pass unchanged thunk', () => {
const myTypeThunk = (): string => 'Int';
const fc: any = typeMapper.convertOutputFieldConfig(myTypeThunk);
expect(fc.type).toBeInstanceOf(ThunkComposer);
expect(fc.type.getType()).toBe(GraphQLInt);
});
it('should accept array with one element as type and wrap them with GraphQLList', () => {
const fc: any = typeMapper.convertOutputFieldConfig(['String']);
expect(fc.type).toBeInstanceOf(ListComposer);
expect(fc.type.ofType).toBeInstanceOf(ScalarTypeComposer);
expect(fc.type.ofType.getType()).toBe(GraphQLString);
const fc2: any = typeMapper.convertOutputFieldConfig({ type: ['String'] });
expect(fc2.type).toBeInstanceOf(ListComposer);
expect(fc2.type.getType().ofType).toBe(GraphQLString);
const fc3: any = typeMapper.convertOutputFieldConfig({
type: [GraphQLString],
});
expect(fc3.type).toBeInstanceOf(ListComposer);
expect(fc3.type.getType().ofType).toBe(GraphQLString);
const tc = ObjectTypeComposer.create('type PriceRange { lon: Float, lat: Float }', sc);
const fc4: any = typeMapper.convertOutputFieldConfig([tc]);
expect(fc4.type).toBeInstanceOf(ListComposer);
expect(fc4.type.getType().ofType).toBe(tc.getType());
const fc5: any = typeMapper.convertOutputFieldConfig({ type: [tc] });
expect(fc5.type).toBeInstanceOf(ListComposer);
expect(fc5.type.ofType).toBe(tc);
expect(() => {
typeMapper.convertOutputFieldConfig([]);
}).toThrowError(/Array must have exact one output type definition/);
const fc6: any = typeMapper.convertOutputFieldConfig([['String']]);
expect(fc6.type).toBeInstanceOf(ListComposer);
expect(fc6.type.getType().ofType).toBeInstanceOf(GraphQLList);
expect(fc6.type.getType().ofType.ofType).toBe(GraphQLString);
});
it('should throw error if provided InputTypeComposer', () => {
const itc: any = InputTypeComposer.create(
'input LonLatInput { lon: Float, lat: Float }',
sc
);
expect(() => {
typeMapper.convertOutputFieldConfig({ type: itc });
}).toThrowError(/InputTypeComposer/);
expect(() => {
typeMapper.convertOutputFieldConfig(itc);
}).toThrowError(/InputTypeComposer/);
});
});
it('should convert args types', () => {
const fc: any = typeMapper.convertOutputFieldConfig({
type: 'String',
args: {
a1: { type: 'String' },
a2: 'Int',
},
});
expect(fc.args.a1.type).toBeInstanceOf(ScalarTypeComposer);
expect(fc.args.a1.type.getType()).toBe(GraphQLString);
expect(fc.args.a2.type.getType()).toBe(GraphQLInt);
});
it('should process outputFieldConfigMap()', () => {
const fcm = typeMapper.convertOutputFieldConfigMap({
f1: 'String',
f2: 'Int',
});
expect(fcm.f1.type).toBeInstanceOf(ScalarTypeComposer);
expect(fcm.f1.type.getType()).toBe(GraphQLString);
expect(fcm.f2.type.getType()).toBe(GraphQLInt);
});
});
describe('convertInputFieldConfig()', () => {
it('should accept type with GraphQLInputObjectType', () => {
const ic = typeMapper.convertInputFieldConfig({
type: GraphQLString,
});
expect(ic.type).toBeInstanceOf(ScalarTypeComposer);
expect(ic.type.getType()).toBe(GraphQLString);
const objectType = new GraphQLInputObjectType({
name: 'SomeTypeInput',
fields: { f: { type: GraphQLString } },
});
const ic2 = typeMapper.convertInputFieldConfig({
type: objectType,
});
expect(ic2.type).toBeInstanceOf(InputTypeComposer);
expect(ic2.type.getType()).toBe(objectType);
});
it('should accept GraphQLScalarType', () => {
const ic = typeMapper.convertInputFieldConfig(GraphQLString);
expect(ic.type.getType()).toBe(GraphQLString);
});
it('should accept GraphQLInputObjectType', () => {
const type = new GraphQLInputObjectType({
name: 'InputType',
fields: () => ({ f1: { type: GraphQLInt } }),
});
const ic = typeMapper.convertInputFieldConfig(type);
expect(ic.type).toBeInstanceOf(InputTypeComposer);
expect(ic.type.getType()).toBe(type);
});
it('should accept GraphQLNonNull', () => {
const ic: any = typeMapper.convertInputFieldConfig(new GraphQLNonNull(GraphQLString));
expect(ic.type).toBeInstanceOf(NonNullComposer);
expect(ic.type.getType().ofType).toBe(GraphQLString);
});
it('should accept GraphQLList', () => {
const ic: any = typeMapper.convertInputFieldConfig(new GraphQLList(GraphQLString));
expect(ic.type).toBeInstanceOf(ListComposer);
expect(ic.type.getType().ofType).toBe(GraphQLString);
});
it('should accept type as string to Scalar', () => {
const ic = typeMapper.convertInputFieldConfig({
type: 'String',
});
expect(ic.type.getType()).toBe(GraphQLString);
});
it('should accept type as string to Enum', () => {
EnumTypeComposer.create('enum Foo { A B }', sc);
const ic = typeMapper.convertInputFieldConfig({
type: 'Foo',
});
expect(ic.type).toBe(sc.get('Foo'));
});
it('should accept type as string to Input Object', () => {
InputTypeComposer.create('input Foo { id: Sting }', sc);
const ic = typeMapper.convertInputFieldConfig({
type: 'Foo',
});
expect(ic.type).toBe(sc.get('Foo'));
});
it('should create field config from type name as string', () => {
const ic = typeMapper.convertInputFieldConfig('String');
expect(ic.type).toBeInstanceOf(ScalarTypeComposer);
expect(ic.type.getType()).toBe(GraphQLString);
});
it('should lookup type name as string in schemaComposer', () => {
const itc = InputTypeComposer.create(`input MyInput { a: Int }`, sc);
const ic = typeMapper.convertInputFieldConfig('MyInput');
expect(ic.type).toBe(itc);
});
it('should create field config from input type GraphQL Schema Language', () => {
const fc = typeMapper.convertInputFieldConfig(
`input MyInputType {
a: String,
b: Int,
}`
);
const itc: any = fc.type;
expect(itc.getTypeName()).toBe('MyInputType');
expect(itc.getFieldType('a')).toBe(GraphQLString);
expect(itc.getFieldType('b')).toBe(GraphQLInt);
});
it('should create field with Scalar type from GraphQL Schema Language', () => {
const fc = typeMapper.convertInputFieldConfig('scalar MyInput');
expect(fc.type).toBeInstanceOf(ScalarTypeComposer);
expect(fc.type.getTypeName()).toBe('MyInput');
});
it('should create field with Enum type from GraphQL Schema Language', () => {
const fc: any = typeMapper.convertInputFieldConfig('enum MyInputEnum { AND OR }');
expect(fc.type).toBeInstanceOf(EnumTypeComposer);
const enumValues = fc.type.getFields();
expect(enumValues.AND).toMatchObject({ value: 'AND' });
expect(enumValues.OR).toMatchObject({ value: 'OR' });
});
it('should throw error if provided output type definition', () => {
expect(() => {
typeMapper.convertInputFieldConfig(
`type MyOutputType {
a: String,
}`
);
}).toThrowError(/but got output type definitio/);
});
it('should accept InputTypeComposer', () => {
const itc = InputTypeComposer.create('input PriceRangeInput { lon: Float, lat: Float }', sc);
itc.setDescription('Description');
const ic = typeMapper.convertInputFieldConfig({
type: itc,
});
expect(ic.type).toBe(itc);
expect(ic.description).toBe(undefined);
const ic2 = typeMapper.convertInputFieldConfig(itc);
expect(ic2.type).toBe(itc);
expect(ic2.description).toBe(undefined);
});
it('should accept ScalarTypeComposer', () => {
const stc = sc.createScalarTC('scalar MySca');
stc.setDescription('Description');
const ic = typeMapper.convertInputFieldConfig({
type: stc,
});
expect(ic.type).toBe(stc);
expect(ic.description).toBe(undefined);
const ic2 = typeMapper.convertInputFieldConfig(stc);
expect(ic2.type).toBe(stc);
expect(ic2.description).toBe(undefined);
});
it('should accept EnumTypeComposer', () => {
const etc = sc.createEnumTC('enum MyEnum { V1 V2 }');
etc.setDescription('Description');
const ic = typeMapper.convertInputFieldConfig({
type: etc,
});
expect(ic.type).toBe(etc);
expect(ic.description).toBe(undefined);
const ic2 = typeMapper.convertInputFieldConfig(etc);
expect(ic2.type).toBe(etc);
expect(ic2.description).toBe(undefined);
});
it('should throw error if provided ObjectTypeComposer', () => {
const tc: any = sc.createObjectTC('type LonLat { lon: Float, lat: Float }');
expect(() => {
typeMapper.convertInputFieldConfig({
type: tc,
});
}).toThrowError(/\sObjectTypeComposer/);
expect(() => {
typeMapper.convertInputFieldConfig(tc);
}).toThrowError(/\sObjectTypeComposer/);
});
it('should pass unchanged thunk', () => {
const myTypeThunk = { type: () => 'Int' };
const tc: any = typeMapper.convertInputFieldConfig(myTypeThunk);
expect(tc.type).toBeInstanceOf(ThunkComposer);
expect(tc.type._thunk()).toBeInstanceOf(ScalarTypeComposer);
expect(tc.type._thunk().getType()).toBe(GraphQLInt);
});
it('should accept array with one element as type and wrap them with GraphQLList', () => {
const fc: any = typeMapper.convertInputFieldConfig(['String']);
expect(fc.type).toBeInstanceOf(ListComposer);
expect(fc.type.getType().ofType).toBe(GraphQLString);
const fc2: any = typeMapper.convertInputFieldConfig({ type: ['String'] });
expect(fc2.type).toBeInstanceOf(ListComposer);
expect(fc2.type.getType().ofType).toBe(GraphQLString);
const fc3: any = typeMapper.convertInputFieldConfig({
type: [GraphQLString],
});
expect(fc3.type).toBeInstanceOf(ListComposer);
expect(fc3.type.getType().ofType).toBe(GraphQLString);
const itc = InputTypeComposer.create('input PriceRangeInput { lon: Float, lat: Float }', sc);
const fc4: any = typeMapper.convertInputFieldConfig([itc]);
expect(fc4.type).toBeInstanceOf(ListComposer);
expect(fc4.type.getType().ofType).toBe(itc.getType());
const fc5: any = typeMapper.convertInputFieldConfig({ type: [itc] });
expect(fc5.type).toBeInstanceOf(ListComposer);
expect(fc5.type.getType().ofType).toBe(itc.getType());
const fc6: any = typeMapper.convertInputFieldConfig([['String']]);
expect(fc6.type).toBeInstanceOf(ListComposer);
expect(fc6.type.ofType).toBeInstanceOf(ListComposer);
expect(fc6.type.ofType.ofType).toBeInstanceOf(ScalarTypeComposer);
expect(fc6.type.getType().ofType.ofType).toBe(GraphQLString);
expect(() => {
typeMapper.convertInputFieldConfig([]);
}).toThrowError(/Array must have exact one input type definition/);
});
it('should process inputFieldConfigMap()', () => {
const icm = typeMapper.convertInputFieldConfigMap({
i1: { type: 'String' },
i2: 'Int',
});
expect(icm.i1.type.getType()).toBe(GraphQLString);
expect(icm.i2.type.getType()).toBe(GraphQLInt);
});
});
describe('convertArgConfig()', () => {
it('should accept type with GraphQLInputObjectType', () => {
const ac = typeMapper.convertArgConfig({
type: GraphQLString,
});
expect(ac.type).toBeInstanceOf(ScalarTypeComposer);
expect(ac.type.getType()).toBe(GraphQLString);
const objectType = new GraphQLInputObjectType({
name: 'SomeTypeInput',
fields: { f: { type: GraphQLString } },
});
const ac2 = typeMapper.convertArgConfig({
type: objectType,
});
expect(ac2.type.getType()).toBe(objectType);
});
it('should accept GraphQLScalarType', () => {
const ac = typeMapper.convertArgConfig(GraphQLString);
expect(ac.type).toBeInstanceOf(ScalarTypeComposer);
expect(ac.type.getType()).toBe(GraphQLString);
});
it('should accept GraphQLInputObjectType', () => {
const type = new GraphQLInputObjectType({
name: 'InputType',
fields: () => ({ f: { type: GraphQLInt } }),
});
const ac = typeMapper.convertArgConfig(type);
expect(ac.type).toBeInstanceOf(InputTypeComposer);
expect(ac.type.getType()).toBe(type);
});
it('should accept GraphQLNonNull', () => {
const ac: any = typeMapper.convertArgConfig(new GraphQLNonNull(GraphQLString));
expect(ac.type).toBeInstanceOf(NonNullComposer);
expect(ac.type.getType().ofType).toBe(GraphQLString);
});
it('should accept GraphQLList', () => {
const ac: any = typeMapper.convertArgConfig(new GraphQLList(GraphQLString));
expect(ac.type).toBeInstanceOf(ListComposer);
expect(ac.type.getType().ofType).toBe(GraphQLString);
});
it('should accept type as string to Scalar', () => {
const ac = typeMapper.convertArgConfig({
type: 'String',
});
expect(ac.type.getType()).toBe(GraphQLString);
});
it('should accept type as string to Enum', () => {
EnumTypeComposer.create('enum Foo { A B }', sc);
const ac = typeMapper.convertArgConfig({
type: 'Foo',
});
expect(ac.type).toBe(sc.get('Foo'));
});
it('should accept type as string to Input Object', () => {
InputTypeComposer.create('input Foo { id: String }', sc);
const ac = typeMapper.convertArgConfig({
type: 'Foo',
});
expect(ac.type).toBe(sc.get('Foo'));
});
it('should create arg config from GraphQL Schema Language', () => {
const ac = typeMapper.convertArgConfig('String');
expect(ac.type.getType()).toBe(GraphQLString);
});
it('should lookup type name as string in schemaComposer', () => {
const itc = InputTypeComposer.create(`input MyArg { a: Int }`, sc);
const ac = typeMapper.convertArgConfig('MyArg');
expect(ac.type).toBe(itc);
});
it('should create arg config from input type GraphQL Schema Language', () => {
const fc = typeMapper.convertArgConfig(
`input MyInputArg {
a: String,
b: Int,
}`
);
const itc: any = fc.type;
expect(itc.getTypeName()).toBe('MyInputArg');
expect(itc.getFieldType('a')).toBe(GraphQLString);
expect(itc.getFieldType('b')).toBe(GraphQLInt);
});
it('should create arg config with Scalar type from GraphQL Schema Language', () => {
const fc = typeMapper.convertArgConfig('scalar Abc');
expect(fc.type).toBeInstanceOf(ScalarTypeComposer);
expect(fc.type.getType()).toBeInstanceOf(GraphQLScalarType);
expect(fc.type.getTypeName()).toBe('Abc');
});
it('should create arg config with Enum type from GraphQL Schema Language', () => {
const fc: any = typeMapper.convertArgConfig('enum MyArgEnum { AND OR }');
expect(fc.type).toBeInstanceOf(EnumTypeComposer);
expect(fc.type.getType()).toBeInstanceOf(GraphQLEnumType);
const enumValues = fc.type.getFields();
expect(enumValues.AND).toMatchObject({ value: 'AND' });
expect(enumValues.OR).toMatchObject({ value: 'OR' });
});
it('should throw error if provided output type definition', () => {
expect(() => {
typeMapper.convertArgConfig(
`type MyOutputType {
a: String,
}`
);
}).toThrowError(/but got output type definition/);
});
it('should accept InputTypeComposer', () => {
const itc = InputTypeComposer.create('input PriceRangeInput { lon: Float, lat: Float }', sc);
itc.setDescription('Description');
const ac = typeMapper.convertArgConfig({
type: itc,
});
expect(ac.type).toBeInstanceOf(InputTypeComposer);
expect(ac.type).toBe(itc);
expect(ac.type.getType()).toBe(itc.getType());
expect(ac.description).toBe(undefined);
const ac2 = typeMapper.convertArgConfig(itc);
expect(ac2.type.getType()).toBe(itc.getType());
expect(ac2.description).toBe(undefined);
});
it('should accept ScalarTypeComposer', () => {
const stc = ScalarTypeComposer.create('scalar Aaa', sc);
stc.setDescription('Description');
const ac = typeMapper.convertArgConfig({
type: stc,
});
expect(ac.type).toBeInstanceOf(ScalarTypeComposer);
expect(ac.type).toBe(stc);
expect(ac.type.getType()).toBe(stc.getType());
expect(ac.description).toBe(undefined);
const ac2 = typeMapper.convertArgConfig(stc);
expect(ac2.type.getType()).toBe(stc.getType());
expect(ac2.description).toBe(undefined);
});
it('should accept EnumTypeComposer', () => {
const etc = EnumTypeComposer.create('enum MyEnum { V1 V2 }', sc);
etc.setDescription('Description');
const ac = typeMapper.convertArgConfig({
type: etc,
});
expect(ac.type).toBeInstanceOf(EnumTypeComposer);
expect(ac.type).toBe(etc);
expect(ac.type.getType()).toBe(etc.getType());
expect(ac.description).toBe(undefined);
const ac2 = typeMapper.convertArgConfig(etc);
expect(ac2.type).toBeInstanceOf(EnumTypeComposer);
expect(ac2.type).toBe(etc);
expect(ac2.type.getType()).toBe(etc.getType());
expect(ac2.description).toBe(undefined);
});
it('should pass unchanged thunk', () => {
const myTypeThunk = (): string => 'Int';
const ac: any = typeMapper.convertArgConfig(myTypeThunk);
expect(ac.type).toBeInstanceOf(ThunkComposer);
expect(ac.type.getType()).toBe(GraphQLInt);
});
it('should accept array with one element as type and wrap them with GraphQLList', () => {
const fc: any = typeMapper.convertArgConfig(['String']);
expect(fc.type).toBeInstanceOf(ListComposer);
expect(fc.type.getType()).toBeInstanceOf(GraphQLList);
expect(fc.type.getType().ofType).toBe(GraphQLString);
const fc2: any = typeMapper.convertArgConfig({ type: ['String'] });
expect(fc2.type).toBeInstanceOf(ListComposer);
expect(fc2.type.getType()).toBeInstanceOf(GraphQLList);
expect(fc2.type.getType().ofType).toBe(GraphQLString);
const fc3: any = typeMapper.convertArgConfig({ type: [GraphQLString] });
expect(fc3.type).toBeInstanceOf(ListComposer);
expect(fc3.type.getType()).toBeInstanceOf(GraphQLList);
expect(fc3.type.getType().ofType).toBe(GraphQLString);
const itc = InputTypeComposer.create('input PriceRangeInput { lon: Float, lat: Float }', sc);
const fc4: any = typeMapper.convertArgConfig([itc]);
expect(fc4.type).toBeInstanceOf(ListComposer);
expect(fc4.type.getType()).toBeInstanceOf(GraphQLList);
expect(fc4.type.getType().ofType).toBe(itc.getType());
const fc5: any = typeMapper.convertArgConfig({ type: [itc] });
expect(fc5.type).toBeInstanceOf(ListComposer);
expect(fc5.type.getType()).toBeInstanceOf(GraphQLList);
expect(fc5.type.getType().ofType).toBe(itc.getType());
const fc6: any = typeMapper.convertArgConfig([['String']]);
expect(fc6.type).toBeInstanceOf(ListComposer);
expect(fc6.type.getType()).toBeInstanceOf(GraphQLList);
expect(fc6.type.getType().ofType).toBeInstanceOf(GraphQLList);
expect(fc6.type.getType().ofType.ofType).toBe(GraphQLString);
expect(() => {
typeMapper.convertArgConfig([]);
}).toThrowError(/Array must have exact one input type definition/);
});
it('should throw error if provided ObjectTypeComposer', () => {
const tc: any = ObjectTypeComposer.create('type LonLat { lon: Float, lat: Float }', sc);
expect(() => {
typeMapper.convertArgConfig({ type: tc });
}).toThrowError(/\sObjectTypeComposer/);
expect(() => {
typeMapper.convertArgConfig(tc);
}).toThrowError(/\sObjectTypeComposer/);
});
it('should process ArgConfigMap', () => {
const acm = typeMapper.convertArgConfigMap({
a1: { type: 'String' },
a2: 'Int',
});
expect(acm.a1.type).toBeInstanceOf(ScalarTypeComposer);
expect(acm.a1.type.getType()).toBe(GraphQLString);
expect(acm.a2.type).toBeInstanceOf(ScalarTypeComposer);
expect(acm.a2.type.getType()).toBe(GraphQLInt);
});
});
describe('parseTypesFrom... methods', () => {
it('parseTypesFromString()', () => {
const gql = `
type User {
name: String
}
type Article {