@eddeee888/gcg-typescript-resolver-files 0.18.0 → 0.18.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (78) hide show
  1. package/package.json +6 -2
  2. package/CHANGELOG.md +0 -816
  3. package/DEVELOPMENT.md +0 -49
  4. package/dist/tsconfig.lib.tsbuildinfo +0 -1
  5. package/eslint.config.mjs +0 -22
  6. package/project.json +0 -8
  7. package/src/addVirtualTypesFileToTsMorphProject/addVirtualTypesFileToTsMorphProject.ts +0 -109
  8. package/src/addVirtualTypesFileToTsMorphProject/index.ts +0 -1
  9. package/src/defineConfig.spec.ts +0 -76
  10. package/src/defineConfig.ts +0 -37
  11. package/src/generateResolverFiles/addExternalResolverImport.ts +0 -185
  12. package/src/generateResolverFiles/addObjectTypeResolversPropertyAssignmentNodesIfNotImplemented.spec.ts +0 -1174
  13. package/src/generateResolverFiles/addObjectTypeResolversPropertyAssignmentNodesIfNotImplemented.ts +0 -144
  14. package/src/generateResolverFiles/addResolverMainFiles.ts +0 -343
  15. package/src/generateResolverFiles/ensureEnumTypeResolversAreGenerated.ts +0 -51
  16. package/src/generateResolverFiles/generateResolverFiles.ts +0 -148
  17. package/src/generateResolverFiles/getImportStatementWithExpectedNamedImport.spec.ts +0 -127
  18. package/src/generateResolverFiles/getImportStatementWithExpectedNamedImport.ts +0 -25
  19. package/src/generateResolverFiles/getVariableStatementWithExpectedIdentifier.spec.ts +0 -147
  20. package/src/generateResolverFiles/getVariableStatementWithExpectedIdentifier.ts +0 -34
  21. package/src/generateResolverFiles/handleGraphQLEnumType.ts +0 -100
  22. package/src/generateResolverFiles/handleGraphQLInterfaceType.ts +0 -70
  23. package/src/generateResolverFiles/handleGraphQLObjectType.ts +0 -171
  24. package/src/generateResolverFiles/handleGraphQLRootObjectTypeField.ts +0 -105
  25. package/src/generateResolverFiles/handleGraphQLScalarType.ts +0 -81
  26. package/src/generateResolverFiles/handleGraphQLUnionType.ts +0 -68
  27. package/src/generateResolverFiles/index.ts +0 -2
  28. package/src/generateResolverFiles/postProcessFiles.ts +0 -287
  29. package/src/generateResolverFiles/types.ts +0 -203
  30. package/src/generateResolverFiles/visitNamedType.ts +0 -66
  31. package/src/generateTypeDefsFiles/generateTypeDefsContent.ts +0 -15
  32. package/src/generateTypeDefsFiles/generateTypeDefsFiles.ts +0 -100
  33. package/src/generateTypeDefsFiles/index.ts +0 -1
  34. package/src/getGraphQLObjectTypeResolversToGenerate/getGraphQLObjectTypeResolversToGenerate.ts +0 -361
  35. package/src/getGraphQLObjectTypeResolversToGenerate/getNodePropertyMap.spec.ts +0 -115
  36. package/src/getGraphQLObjectTypeResolversToGenerate/getNodePropertyMap.ts +0 -88
  37. package/src/getGraphQLObjectTypeResolversToGenerate/index.ts +0 -1
  38. package/src/index.ts +0 -3
  39. package/src/normalizeAddConfigPath/index.ts +0 -1
  40. package/src/normalizeAddConfigPath/normalizeAddConfigPath.ts +0 -29
  41. package/src/parseGraphQLSchema/index.ts +0 -2
  42. package/src/parseGraphQLSchema/normalizeResolverName.ts +0 -28
  43. package/src/parseGraphQLSchema/parseGraphQLSchema.ts +0 -531
  44. package/src/parseGraphQLSchema/parseLocationForOutputDir.ts +0 -76
  45. package/src/parseSources/index.ts +0 -1
  46. package/src/parseSources/parseSources.ts +0 -83
  47. package/src/parseTypeMappers/collectTypeMappersFromSourceFile.spec.ts +0 -789
  48. package/src/parseTypeMappers/collectTypeMappersFromSourceFile.ts +0 -197
  49. package/src/parseTypeMappers/index.ts +0 -1
  50. package/src/parseTypeMappers/parseTypeMappers.ts +0 -75
  51. package/src/preset.ts +0 -317
  52. package/src/utils/cwd.ts +0 -8
  53. package/src/utils/fmt.ts +0 -16
  54. package/src/utils/index.ts +0 -12
  55. package/src/utils/isMatchResolverNamePattern.ts +0 -27
  56. package/src/utils/isNativeNamedType.ts +0 -21
  57. package/src/utils/isRootObjectType.ts +0 -8
  58. package/src/utils/isWhitelistedModule.spec.ts +0 -71
  59. package/src/utils/isWhitelistedModule.ts +0 -21
  60. package/src/utils/logger.ts +0 -14
  61. package/src/utils/normalizeImportExtension.spec.ts +0 -42
  62. package/src/utils/normalizeImportExtension.ts +0 -16
  63. package/src/utils/normalizeRelativePath.ts +0 -6
  64. package/src/utils/parseLocationForWhitelistedModule.spec.ts +0 -130
  65. package/src/utils/parseLocationForWhitelistedModule.ts +0 -38
  66. package/src/utils/printImportLine.spec.ts +0 -150
  67. package/src/utils/printImportLine.ts +0 -78
  68. package/src/utils/relativeModulePath.ts +0 -7
  69. package/src/validateAndMergeParsedConfigs/index.ts +0 -1
  70. package/src/validateAndMergeParsedConfigs/validateAndMergeParsedConfigs.spec.ts +0 -211
  71. package/src/validateAndMergeParsedConfigs/validateAndMergeParsedConfigs.ts +0 -92
  72. package/src/validatePresetConfig/index.ts +0 -1
  73. package/src/validatePresetConfig/validatePresetConfig.spec.ts +0 -706
  74. package/src/validatePresetConfig/validatePresetConfig.ts +0 -571
  75. package/tsconfig.json +0 -14
  76. package/tsconfig.lib.json +0 -18
  77. package/tsconfig.spec.json +0 -20
  78. package/vite.config.ts +0 -24
@@ -1,789 +0,0 @@
1
- import { Project, SyntaxKind } from 'ts-morph';
2
- import { collectTypeMappersFromSourceFile } from './collectTypeMappersFromSourceFile';
3
-
4
- describe('collectTypeMappersFromSourceFile', () => {
5
- it('mutates the result based on typeMappersSuffix for exports from other modules', () => {
6
- const project = new Project({
7
- compilerOptions: {
8
- paths: {
9
- '@external/module1': ['/path/to/external/modules1/index.ts'],
10
- '@external/module2': ['/path/to/external/modules2/index.ts'],
11
- },
12
- },
13
- });
14
- project.createSourceFile(
15
- `/path/to/external/modules1/index.ts`,
16
- `
17
- export interface Billing {
18
- id: number;
19
- address: string;
20
- }
21
-
22
- export type PaymentTypeMapper = {
23
- id: string;
24
- type: 'creditcard' | 'cash' | 'bitcoin';
25
- typeCode: 'payment';
26
- }
27
-
28
- export type SomethingTypeMapper = {
29
- id: number;
30
- }
31
- `
32
- );
33
- project.createSourceFile(
34
- `/path/to/external/modules2/index.ts`,
35
- `
36
- export type Address = {
37
- id: string;
38
- }
39
- export interface GeoTypeMapper {
40
- id: string;
41
- }
42
- export type NotAliasMapper2 = {
43
- id: number;
44
- }
45
- `
46
- );
47
- project.createSourceFile(
48
- '/path/to/schemas/module1/localModule1.ts',
49
- `
50
- export type Preference = {
51
- id: number;
52
- }
53
- export interface FlagTypeMapper {
54
- id: number;
55
- }
56
- export type NotAliasMapper3 = {
57
- id: number;
58
- }
59
- `
60
- );
61
- const mapperFile = project.createSourceFile(
62
- '/path/to/schemas/module1/schema.mappers.ts',
63
- `
64
- export type {
65
- Billing as BillingTypeMapper,
66
- PaymentTypeMapper,
67
- SomethingTypeMapper as NotAliasMapper1
68
- } from '@external/module1';
69
- export {
70
- Address as AddressTypeMapper,
71
- GeoTypeMapper,
72
- NotAliasMapper2
73
- } from '@external/module2';
74
- export {
75
- Preference as PreferenceTypeMapper,
76
- FlagTypeMapper,
77
- NotAliasMapper3
78
- } from './localModule1';`
79
- );
80
-
81
- const result = {};
82
-
83
- collectTypeMappersFromSourceFile(
84
- {
85
- typeMappersSourceFile: mapperFile,
86
- typeMappersSuffix: 'TypeMapper',
87
- resolverTypesPath: '/path/to/schemas/types.generated.ts',
88
- emitLegacyCommonJSImports: true,
89
- },
90
- result
91
- );
92
-
93
- expect(result).toEqual({
94
- Billing: {
95
- schemaType: 'Billing',
96
- mapper: {
97
- name: 'BillingTypeMapper',
98
- kind: SyntaxKind.ExportSpecifier,
99
- filename: '/path/to/schemas/module1/schema.mappers.ts',
100
- },
101
- configImportPath: './module1/schema.mappers#BillingTypeMapper',
102
- },
103
- Payment: {
104
- schemaType: 'Payment',
105
- mapper: {
106
- name: 'PaymentTypeMapper',
107
- kind: SyntaxKind.ExportSpecifier,
108
- filename: '/path/to/schemas/module1/schema.mappers.ts',
109
- },
110
- configImportPath: './module1/schema.mappers#PaymentTypeMapper',
111
- },
112
- Address: {
113
- schemaType: 'Address',
114
- mapper: {
115
- name: 'AddressTypeMapper',
116
- kind: SyntaxKind.ExportSpecifier,
117
- filename: '/path/to/schemas/module1/schema.mappers.ts',
118
- },
119
- configImportPath: './module1/schema.mappers#AddressTypeMapper',
120
- },
121
- Geo: {
122
- schemaType: 'Geo',
123
- mapper: {
124
- filename: '/path/to/schemas/module1/schema.mappers.ts',
125
- kind: SyntaxKind.ExportSpecifier,
126
- name: 'GeoTypeMapper',
127
- },
128
- configImportPath: './module1/schema.mappers#GeoTypeMapper',
129
- },
130
- Preference: {
131
- schemaType: 'Preference',
132
- mapper: {
133
- name: 'PreferenceTypeMapper',
134
- kind: SyntaxKind.ExportSpecifier,
135
- filename: '/path/to/schemas/module1/schema.mappers.ts',
136
- },
137
- configImportPath: './module1/schema.mappers#PreferenceTypeMapper',
138
- },
139
- Flag: {
140
- schemaType: 'Flag',
141
- mapper: {
142
- name: 'FlagTypeMapper',
143
- kind: SyntaxKind.ExportSpecifier,
144
- filename: '/path/to/schemas/module1/schema.mappers.ts',
145
- },
146
- configImportPath: './module1/schema.mappers#FlagTypeMapper',
147
- },
148
- });
149
- });
150
-
151
- it('mutates the result based on typeMappersSuffix for imports and rexports', () => {
152
- const project = new Project({
153
- compilerOptions: {
154
- paths: {
155
- '@external/module1': ['/path/to/external/modules1/index.ts'],
156
- '@external/module2': ['/path/to/external/modules2/index.ts'],
157
- },
158
- },
159
- });
160
- project.createSourceFile(
161
- `/path/to/external/modules1/index.ts`,
162
- `
163
- export interface Billing {
164
- __type: 1;
165
- }
166
-
167
- export type PaymentTypeMapper = {
168
- __type: 2;
169
- }
170
-
171
- export type SomethingTypeMapper = {
172
- id: number;
173
- }
174
- `
175
- );
176
- project.createSourceFile(
177
- `/path/to/external/modules2/index.ts`,
178
- `
179
- export type Address = {
180
- __type: 3;
181
- }
182
- export interface GeoTypeMapper {
183
- __type: boolean;
184
- }
185
- export type NotAliasMapper2 = {
186
- id: number;
187
- }
188
- `
189
- );
190
- project.createSourceFile(
191
- '/path/to/schemas/module1/localModule1.ts',
192
- `
193
- export type Preference = {
194
- __type: true;
195
- }
196
- export interface FlagTypeMapper {
197
- __type: false;
198
- }
199
- export type NotAliasMapper3 = {
200
- id: number;
201
- }
202
- `
203
- );
204
- const mapperFile = project.createSourceFile(
205
- '/path/to/schemas/module1/schema.mappers.ts',
206
- `
207
- import type {
208
- Billing as BillingTypeMapper,
209
- PaymentTypeMapper,
210
- SomethingTypeMapper as NotAliasMapper1
211
- } from '@external/module1';
212
- import {
213
- Address as AddressTypeMapper,
214
- GeoTypeMapper,
215
- NotAliasMapper2
216
- } from '@external/module2';
217
- import {
218
- Preference as PreferenceTypeMapper,
219
- FlagTypeMapper,
220
- NotAliasMapper3
221
- } from './localModule1';
222
-
223
- export {
224
- BillingTypeMapper,
225
- PaymentTypeMapper,
226
- NotAliasMapper1,
227
- AddressTypeMapper,
228
- GeoTypeMapper,
229
- NotAliasMapper2,
230
- PreferenceTypeMapper,
231
- FlagTypeMapper,
232
- NotAliasMapper3
233
- }
234
- `
235
- );
236
-
237
- const result = {};
238
-
239
- collectTypeMappersFromSourceFile(
240
- {
241
- typeMappersSourceFile: mapperFile,
242
- typeMappersSuffix: 'TypeMapper',
243
- resolverTypesPath: '/path/to/schemas/types.generated.ts',
244
- emitLegacyCommonJSImports: true,
245
- },
246
- result
247
- );
248
-
249
- expect(result).toEqual({
250
- Billing: {
251
- schemaType: 'Billing',
252
- mapper: {
253
- name: 'BillingTypeMapper',
254
- kind: SyntaxKind.ExportSpecifier,
255
- filename: '/path/to/schemas/module1/schema.mappers.ts',
256
- },
257
- configImportPath: './module1/schema.mappers#BillingTypeMapper',
258
- },
259
- Payment: {
260
- schemaType: 'Payment',
261
- mapper: {
262
- name: 'PaymentTypeMapper',
263
- kind: SyntaxKind.ExportSpecifier,
264
- filename: '/path/to/schemas/module1/schema.mappers.ts',
265
- },
266
- configImportPath: './module1/schema.mappers#PaymentTypeMapper',
267
- },
268
- Address: {
269
- schemaType: 'Address',
270
- mapper: {
271
- name: 'AddressTypeMapper',
272
- kind: SyntaxKind.ExportSpecifier,
273
- filename: '/path/to/schemas/module1/schema.mappers.ts',
274
- },
275
- configImportPath: './module1/schema.mappers#AddressTypeMapper',
276
- },
277
- Geo: {
278
- schemaType: 'Geo',
279
- mapper: {
280
- name: 'GeoTypeMapper',
281
- kind: SyntaxKind.ExportSpecifier,
282
- filename: '/path/to/schemas/module1/schema.mappers.ts',
283
- },
284
- configImportPath: './module1/schema.mappers#GeoTypeMapper',
285
- },
286
- Preference: {
287
- schemaType: 'Preference',
288
- mapper: {
289
- name: 'PreferenceTypeMapper',
290
- kind: SyntaxKind.ExportSpecifier,
291
- filename: '/path/to/schemas/module1/schema.mappers.ts',
292
- },
293
- configImportPath: './module1/schema.mappers#PreferenceTypeMapper',
294
- },
295
- Flag: {
296
- schemaType: 'Flag',
297
- mapper: {
298
- name: 'FlagTypeMapper',
299
- kind: SyntaxKind.ExportSpecifier,
300
- filename: '/path/to/schemas/module1/schema.mappers.ts',
301
- },
302
- configImportPath: './module1/schema.mappers#FlagTypeMapper',
303
- },
304
- });
305
- });
306
-
307
- it('mutates the result based on typeMappersSuffix for locally declared types/interfaces and exported', () => {
308
- const project = new Project();
309
- const mapperFile = project.createSourceFile(
310
- '/path/to/schemas/module1/schema.mappers.ts',
311
- `
312
- export interface UserTypeMapper {
313
- id: string;
314
- }
315
-
316
- export type ProfileTypeMapper {
317
- id: string;
318
- account: string;
319
- }
320
-
321
- export interface NotMapperInlineExport1 {
322
- id: string;
323
- }
324
-
325
- export type NotMapperInlineExport2 = {
326
- id: string;
327
- };
328
-
329
- interface Like {
330
- id: string;
331
- }
332
-
333
- type Password = {
334
- isValid: boolean;
335
- };
336
- type PasswordTypeMapper = Password;
337
-
338
- interface NotGoingToBe1TypeMapper {
339
- something: string;
340
- }
341
-
342
- type NotGoingToBe2TypeMapper = {
343
- something: string;
344
- }
345
-
346
- export {
347
- Like as LikeTypeMapper,
348
- PasswordTypeMapper,
349
- NotGoingToBe1TypeMapper as NotMapper1,
350
- NotGoingToBe2TypeMapper as NotMapper2,
351
- };`
352
- );
353
-
354
- const result = {};
355
-
356
- collectTypeMappersFromSourceFile(
357
- {
358
- typeMappersSourceFile: mapperFile,
359
- typeMappersSuffix: 'TypeMapper',
360
- resolverTypesPath: '/path/to/schemas/types.generated.ts',
361
- emitLegacyCommonJSImports: true,
362
- },
363
- result
364
- );
365
-
366
- expect(result).toEqual({
367
- Profile: {
368
- schemaType: 'Profile',
369
- mapper: {
370
- name: 'ProfileTypeMapper',
371
- kind: SyntaxKind.TypeAliasDeclaration,
372
- filename: '/path/to/schemas/module1/schema.mappers.ts',
373
- },
374
- configImportPath: './module1/schema.mappers#ProfileTypeMapper',
375
- },
376
- User: {
377
- schemaType: 'User',
378
- mapper: {
379
- name: 'UserTypeMapper',
380
- kind: SyntaxKind.InterfaceDeclaration,
381
- filename: '/path/to/schemas/module1/schema.mappers.ts',
382
- },
383
- configImportPath: './module1/schema.mappers#UserTypeMapper',
384
- },
385
- Like: {
386
- schemaType: 'Like',
387
- mapper: {
388
- name: 'LikeTypeMapper',
389
- kind: SyntaxKind.ExportSpecifier,
390
- filename: '/path/to/schemas/module1/schema.mappers.ts',
391
- },
392
- configImportPath: './module1/schema.mappers#LikeTypeMapper',
393
- },
394
- Password: {
395
- schemaType: 'Password',
396
- mapper: {
397
- name: 'PasswordTypeMapper',
398
- kind: SyntaxKind.ExportSpecifier,
399
- filename: '/path/to/schemas/module1/schema.mappers.ts',
400
- },
401
- configImportPath: './module1/schema.mappers#PasswordTypeMapper',
402
- },
403
- });
404
- });
405
-
406
- it('mutates the result based on typeMappersSuffix for locally declared class and exported', () => {
407
- const project = new Project();
408
- const mapperFile = project.createSourceFile(
409
- '/path/to/schemas/module1/schema.mappers.ts',
410
- `
411
- export class UserTypeMapper {
412
- id: string;
413
- firstName!: string;
414
- readonly lastName: string;
415
- public createdAt: string;
416
- public readonly updatedAt: string;
417
- // bellow are not going to be collected
418
- protected protectedField: string;
419
- static notInstanceField: string;
420
- private tsPrivateField: string;
421
- private readonly tsPrivateReadonlyField: string;
422
- #ecmaPrivateField: string;
423
- method(): string
424
- get getter(): string
425
- }
426
-
427
- export class NotMapperInlineExport1 {
428
- id: string;
429
- }
430
-
431
- class PostTypeMapper {
432
- id: string
433
- }
434
-
435
- class Base {
436
- id: string;
437
- private title: string;
438
- }
439
-
440
- class Like extends Base {
441
- createdAt: string
442
- }
443
-
444
- export {
445
- Like as LikeTypeMapper,
446
- PostTypeMapper,
447
- };`
448
- );
449
-
450
- const result = {};
451
-
452
- collectTypeMappersFromSourceFile(
453
- {
454
- typeMappersSourceFile: mapperFile,
455
- typeMappersSuffix: 'TypeMapper',
456
- resolverTypesPath: '/path/to/schemas/types.generated.ts',
457
- emitLegacyCommonJSImports: true,
458
- },
459
- result
460
- );
461
-
462
- expect(result).toEqual({
463
- Like: {
464
- schemaType: 'Like',
465
- mapper: {
466
- name: 'LikeTypeMapper',
467
- kind: SyntaxKind.ExportSpecifier,
468
- filename: '/path/to/schemas/module1/schema.mappers.ts',
469
- },
470
- configImportPath: './module1/schema.mappers#LikeTypeMapper',
471
- },
472
- Post: {
473
- schemaType: 'Post',
474
- mapper: {
475
- name: 'PostTypeMapper',
476
- kind: SyntaxKind.ExportSpecifier,
477
- filename: '/path/to/schemas/module1/schema.mappers.ts',
478
- },
479
- configImportPath: './module1/schema.mappers#PostTypeMapper',
480
- },
481
- User: {
482
- schemaType: 'User',
483
- mapper: {
484
- name: 'UserTypeMapper',
485
- kind: SyntaxKind.ClassDeclaration,
486
- filename: '/path/to/schemas/module1/schema.mappers.ts',
487
- },
488
- configImportPath: './module1/schema.mappers#UserTypeMapper',
489
- },
490
- });
491
- });
492
-
493
- it('mutates the result on multiple runs', () => {
494
- const project = new Project({
495
- compilerOptions: {
496
- paths: {
497
- '@external/module1': ['/path/to/external/modules1/index.ts'],
498
- '@external/module2': ['/path/to/external/modules2/index.ts'],
499
- },
500
- },
501
- });
502
- project.createSourceFile(
503
- `/path/to/external/modules1/index.ts`,
504
- `
505
- export interface Billing {
506
- id: number;
507
- address: string;
508
- }
509
-
510
- export type PaymentTypeMapper = {
511
- id: string;
512
- type: 'creditcard' | 'cash' | 'bitcoin';
513
- typeCode: 'payment';
514
- }
515
- `
516
- );
517
- project.createSourceFile(
518
- `/path/to/external/modules2/index.ts`,
519
- `
520
- export type Address = {
521
- id: string;
522
- }
523
- export interface GeoTypeMapper {
524
- id: string;
525
- }
526
- `
527
- );
528
- project.createSourceFile(
529
- '/path/to/schemas/preference/localModule1.ts',
530
- `
531
- export type Preference = {
532
- id: number;
533
- }
534
- export interface FlagTypeMapper {
535
- id: number;
536
- }
537
- `
538
- );
539
- const billingMapperFile = project.createSourceFile(
540
- '/path/to/schemas/billing/schema.mappers.ts',
541
- "export type { Billing as BillingTypeMapper, PaymentTypeMapper } from '@external/module1';"
542
- );
543
- const addressMapperFile = project.createSourceFile(
544
- '/path/to/schemas/address/schema.mappers.ts',
545
- "export { Address as AddressTypeMapper, GeoTypeMapper } from '@external/module2';"
546
- );
547
- const preferenceMapperFile = project.createSourceFile(
548
- '/path/to/schemas/preference/schema.mappers.ts',
549
- "export { Preference as PreferenceTypeMapper, FlagTypeMapper } from './localModule1';"
550
- );
551
-
552
- const expectedBilling = {
553
- schemaType: 'Billing',
554
- mapper: {
555
- name: 'BillingTypeMapper',
556
- kind: SyntaxKind.ExportSpecifier,
557
- filename: '/path/to/schemas/billing/schema.mappers.ts',
558
- },
559
- configImportPath: './billing/schema.mappers#BillingTypeMapper',
560
- };
561
- const expectedPayment = {
562
- schemaType: 'Payment',
563
- mapper: {
564
- name: 'PaymentTypeMapper',
565
- kind: SyntaxKind.ExportSpecifier,
566
- filename: '/path/to/schemas/billing/schema.mappers.ts',
567
- },
568
- configImportPath: './billing/schema.mappers#PaymentTypeMapper',
569
- };
570
- const expectedAddress = {
571
- schemaType: 'Address',
572
- mapper: {
573
- name: 'AddressTypeMapper',
574
- kind: SyntaxKind.ExportSpecifier,
575
- filename: '/path/to/schemas/address/schema.mappers.ts',
576
- },
577
- configImportPath: './address/schema.mappers#AddressTypeMapper',
578
- };
579
- const expectedGeo = {
580
- schemaType: 'Geo',
581
- mapper: {
582
- name: 'GeoTypeMapper',
583
- kind: SyntaxKind.ExportSpecifier,
584
- filename: '/path/to/schemas/address/schema.mappers.ts',
585
- },
586
- configImportPath: './address/schema.mappers#GeoTypeMapper',
587
- };
588
- const expectedPreference = {
589
- schemaType: 'Preference',
590
- mapper: {
591
- name: 'PreferenceTypeMapper',
592
- kind: SyntaxKind.ExportSpecifier,
593
- filename: '/path/to/schemas/preference/schema.mappers.ts',
594
- },
595
- configImportPath: './preference/schema.mappers#PreferenceTypeMapper',
596
- };
597
- const expectedFlag = {
598
- schemaType: 'Flag',
599
- mapper: {
600
- name: 'FlagTypeMapper',
601
- kind: SyntaxKind.ExportSpecifier,
602
- filename: '/path/to/schemas/preference/schema.mappers.ts',
603
- },
604
- configImportPath: './preference/schema.mappers#FlagTypeMapper',
605
- };
606
-
607
- const result = {};
608
-
609
- collectTypeMappersFromSourceFile(
610
- {
611
- typeMappersSourceFile: billingMapperFile,
612
- typeMappersSuffix: 'TypeMapper',
613
- resolverTypesPath: '/path/to/schemas/types.generated.ts',
614
- emitLegacyCommonJSImports: true,
615
- },
616
- result
617
- );
618
- expect(result).toEqual({
619
- Billing: expectedBilling,
620
- Payment: expectedPayment,
621
- });
622
-
623
- collectTypeMappersFromSourceFile(
624
- {
625
- typeMappersSourceFile: addressMapperFile,
626
- typeMappersSuffix: 'TypeMapper',
627
- resolverTypesPath: '/path/to/schemas/types.generated.ts',
628
- emitLegacyCommonJSImports: true,
629
- },
630
- result
631
- );
632
- expect(result).toEqual({
633
- Billing: expectedBilling,
634
- Payment: expectedPayment,
635
- Address: expectedAddress,
636
- Geo: expectedGeo,
637
- });
638
-
639
- collectTypeMappersFromSourceFile(
640
- {
641
- typeMappersSourceFile: preferenceMapperFile,
642
- typeMappersSuffix: 'TypeMapper',
643
- resolverTypesPath: '/path/to/schemas/types.generated.ts',
644
- emitLegacyCommonJSImports: true,
645
- },
646
- result
647
- );
648
- expect(result).toEqual({
649
- Billing: expectedBilling,
650
- Payment: expectedPayment,
651
- Address: expectedAddress,
652
- Geo: expectedGeo,
653
- Preference: expectedPreference,
654
- Flag: expectedFlag,
655
- });
656
- });
657
-
658
- it('mutates the result with .js extensions in configImportPath for mapper files when emitLegacyCommonJSImports=false', () => {
659
- const project = new Project({
660
- compilerOptions: {
661
- paths: {
662
- '@external/module1': ['/path/to/external/modules1/index.ts'],
663
- '@external/module2': ['/path/to/external/modules2/index.ts'],
664
- },
665
- },
666
- });
667
- project.createSourceFile(
668
- `/path/to/external/modules1/index.ts`,
669
- `
670
- export interface Billing {
671
- id: number;
672
- address: string;
673
- }
674
- `
675
- );
676
- project.createSourceFile(
677
- `/path/to/external/modules2/index.ts`,
678
- `
679
- export type Address = {
680
- id: string;
681
- }
682
- `
683
- );
684
- project.createSourceFile(
685
- '/path/to/schemas/module1/localModule1.ts',
686
- `
687
- export type Preference = {
688
- id: number;
689
- }
690
- `
691
- );
692
- const mapperFile = project.createSourceFile(
693
- '/path/to/schemas/module1/schema.mappers.ts',
694
- `
695
- export type { Billing as BillingMapper } from '@external/module1';
696
- export { Address as AddressMapper } from '@external/module2';
697
- export { Preference as PreferenceMapper } from './localModule1';`
698
- );
699
-
700
- const result = {};
701
-
702
- collectTypeMappersFromSourceFile(
703
- {
704
- typeMappersSourceFile: mapperFile,
705
- typeMappersSuffix: 'Mapper',
706
- resolverTypesPath: '/path/to/schemas/types.generated.ts',
707
- emitLegacyCommonJSImports: false,
708
- },
709
- result
710
- );
711
-
712
- expect(result).toEqual({
713
- Billing: {
714
- schemaType: 'Billing',
715
- mapper: {
716
- name: 'BillingMapper',
717
- kind: SyntaxKind.ExportSpecifier,
718
- filename: '/path/to/schemas/module1/schema.mappers.ts',
719
- },
720
- configImportPath: './module1/schema.mappers.js#BillingMapper',
721
- },
722
- Address: {
723
- schemaType: 'Address',
724
- mapper: {
725
- name: 'AddressMapper',
726
- kind: SyntaxKind.ExportSpecifier,
727
- filename: '/path/to/schemas/module1/schema.mappers.ts',
728
- },
729
- configImportPath: './module1/schema.mappers.js#AddressMapper',
730
- },
731
- Preference: {
732
- schemaType: 'Preference',
733
- mapper: {
734
- name: 'PreferenceMapper',
735
- kind: SyntaxKind.ExportSpecifier,
736
- filename: '/path/to/schemas/module1/schema.mappers.ts',
737
- },
738
- configImportPath: './module1/schema.mappers.js#PreferenceMapper',
739
- },
740
- });
741
- });
742
-
743
- it('throws error if there are duplicated mappers', () => {
744
- const project = new Project();
745
- project.createSourceFile(
746
- '/path/to/schemas/module1/schema.mappers.ts',
747
- `
748
- export interface UserTypeMapper {
749
- id: string;
750
- }`
751
- );
752
- project.createSourceFile(
753
- '/path/to/schemas/module2/schema.mappers.ts',
754
- `
755
- export type UserTypeMapper {
756
- id: string;
757
- password: string;
758
- }`
759
- );
760
-
761
- const result = {};
762
-
763
- collectTypeMappersFromSourceFile(
764
- {
765
- typeMappersSourceFile: project.getSourceFiles()[0],
766
- typeMappersSuffix: 'TypeMapper',
767
- resolverTypesPath: '/path/to/schemas/types.generated.ts',
768
- emitLegacyCommonJSImports: true,
769
- },
770
- result
771
- );
772
-
773
- expect(() =>
774
- collectTypeMappersFromSourceFile(
775
- {
776
- typeMappersSourceFile: project.getSourceFiles()[1],
777
- typeMappersSuffix: 'TypeMapper',
778
- resolverTypesPath: '/path/to/schemas/types.generated.ts',
779
- emitLegacyCommonJSImports: true,
780
- },
781
- result
782
- )
783
- ).toThrowErrorMatchingInlineSnapshot(`
784
- [Error: GraphQL type "User" has duplicated "UserTypeMapper" mappers:
785
- - ./module2/schema.mappers#UserTypeMapper
786
- - ./module1/schema.mappers#UserTypeMapper]
787
- `);
788
- });
789
- });