@khanacademy/graphql-flow 1.1.2 → 2.0.0

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 (76) hide show
  1. package/.babelrc +1 -1
  2. package/.eslintrc.js +0 -1
  3. package/.github/workflows/changeset-release.yml +1 -1
  4. package/CHANGELOG.md +16 -0
  5. package/dist/cli/config.js +2 -4
  6. package/dist/cli/run.js +1 -2
  7. package/dist/enums.js +8 -9
  8. package/dist/generateResponseType.js +33 -41
  9. package/dist/generateTypeFiles.js +13 -35
  10. package/dist/generateVariablesType.js +15 -31
  11. package/dist/index.js +11 -17
  12. package/dist/parser/parse.js +10 -8
  13. package/dist/parser/resolve.js +11 -8
  14. package/dist/parser/utils.js +36 -0
  15. package/dist/schemaFromIntrospectionData.js +1 -2
  16. package/dist/types.js +1 -2
  17. package/dist/utils.js +43 -3
  18. package/package.json +8 -7
  19. package/{src/cli/schema.json → schema.json} +3 -0
  20. package/{dist/__test__/generateTypeFileContents.test.js → src/__test__/generateTypeFileContents.test.ts} +38 -41
  21. package/{dist/__test__/graphql-flow.test.js → src/__test__/graphql-flow.test.ts} +232 -235
  22. package/src/__test__/{processPragmas.test.js → processPragmas.test.ts} +0 -1
  23. package/{dist/cli/__test__/config.test.js → src/cli/__test__/config.test.ts} +5 -6
  24. package/{dist/cli/config.js.flow → src/cli/config.ts} +6 -11
  25. package/src/cli/{run.js → run.ts} +5 -4
  26. package/src/{enums.js → enums.ts} +20 -22
  27. package/{dist/generateResponseType.js.flow → src/generateResponseType.ts} +167 -182
  28. package/src/{generateTypeFiles.js → generateTypeFiles.ts} +24 -40
  29. package/src/{generateVariablesType.js → generateVariablesType.ts} +34 -44
  30. package/{dist/index.js.flow → src/index.ts} +33 -24
  31. package/{dist/parser/__test__/parse.test.js → src/parser/__test__/parse.test.ts} +12 -11
  32. package/{dist/parser/parse.js.flow → src/parser/parse.ts} +69 -48
  33. package/{dist/parser/resolve.js.flow → src/parser/resolve.ts} +25 -19
  34. package/src/parser/utils.ts +24 -0
  35. package/{dist/schemaFromIntrospectionData.js.flow → src/schemaFromIntrospectionData.ts} +1 -4
  36. package/src/types.ts +97 -0
  37. package/src/utils.ts +73 -0
  38. package/tools/{find-files-with-gql.js → find-files-with-gql.ts} +2 -3
  39. package/tsconfig.json +110 -0
  40. package/types/flow-to-ts.d.ts +1 -0
  41. package/dist/__test__/example-schema.graphql +0 -67
  42. package/dist/__test__/processPragmas.test.js +0 -76
  43. package/dist/cli/config.js.map +0 -1
  44. package/dist/cli/run.js.flow +0 -236
  45. package/dist/cli/run.js.map +0 -1
  46. package/dist/cli/schema.json +0 -94
  47. package/dist/enums.js.flow +0 -98
  48. package/dist/enums.js.map +0 -1
  49. package/dist/generateResponseType.js.map +0 -1
  50. package/dist/generateTypeFiles.js.flow +0 -197
  51. package/dist/generateTypeFiles.js.map +0 -1
  52. package/dist/generateVariablesType.js.flow +0 -156
  53. package/dist/generateVariablesType.js.map +0 -1
  54. package/dist/index.js.map +0 -1
  55. package/dist/parser/parse.js.map +0 -1
  56. package/dist/parser/resolve.js.map +0 -1
  57. package/dist/schemaFromIntrospectionData.js.map +0 -1
  58. package/dist/types.js.flow +0 -87
  59. package/dist/types.js.map +0 -1
  60. package/dist/utils.js.flow +0 -50
  61. package/dist/utils.js.map +0 -1
  62. package/flow-typed/npm/@babel/types_vx.x.x.js +0 -5331
  63. package/flow-typed/npm/jest_v23.x.x.js +0 -1155
  64. package/flow-typed/overrides.js +0 -435
  65. package/src/__test__/generateTypeFileContents.test.js +0 -157
  66. package/src/__test__/graphql-flow.test.js +0 -639
  67. package/src/cli/__test__/config.test.js +0 -120
  68. package/src/cli/config.js +0 -84
  69. package/src/generateResponseType.js +0 -583
  70. package/src/index.js +0 -159
  71. package/src/parser/__test__/parse.test.js +0 -249
  72. package/src/parser/parse.js +0 -414
  73. package/src/parser/resolve.js +0 -117
  74. package/src/schemaFromIntrospectionData.js +0 -68
  75. package/src/types.js +0 -87
  76. package/src/utils.js +0 -50
@@ -1,5 +1,3 @@
1
- // @flow
2
-
3
1
  /**
4
2
  * Tests for our graphql flow generation!
5
3
  */
@@ -13,8 +11,8 @@ import type {GenerateConfig} from '../types';
13
11
  // This allows us to "snapshot" a string cleanly.
14
12
  /* eslint-disable flowtype-errors/uncovered */
15
13
  expect.addSnapshotSerializer({
16
- test: (value) => value && typeof value === 'string',
17
- print: (value, _, __) => value,
14
+ test: (value: any) => value && typeof value === 'string',
15
+ print: (value: any, _: any, __: any) => value,
18
16
  });
19
17
  /* eslint-enable flowtype-errors/uncovered */
20
18
 
@@ -22,7 +20,7 @@ const [_, exampleSchema] = getSchemas(__dirname + '/example-schema.graphql');
22
20
 
23
21
  const rawQueryToFlowTypes = (
24
22
  query: string,
25
- options?: $Partial<GenerateConfig>,
23
+ options?: Partial<GenerateConfig>,
26
24
  ): string => {
27
25
  const node = gql(query);
28
26
  return documentToFlowTypes(node, exampleSchema, {
@@ -31,11 +29,11 @@ const rawQueryToFlowTypes = (
31
29
  ...options,
32
30
  })
33
31
  .map(
34
- ({typeName, code, extraTypes}) =>
32
+ ({typeName, code, extraTypes}: any) =>
35
33
  `// ${typeName}.js\n${code}` +
36
34
  Object.keys(extraTypes)
37
35
  .sort()
38
- .map((k) => `\nexport type ${k} = ${extraTypes[k]};`)
36
+ .map((k: any) => `\nexport type ${k} = ${extraTypes[k]};`)
39
37
  .join(''),
40
38
  )
41
39
  .join('\n\n');
@@ -51,14 +49,14 @@ describe('graphql-flow generation', () => {
51
49
 
52
50
  expect(result).toMatchInlineSnapshot(`
53
51
  // SomeQueryType.js
54
- export type SomeQueryType = {|
55
- variables: {|
56
- candies: number
57
- |},
58
- response: {|
59
- candies: ?string
60
- |}
61
- |};
52
+ export type SomeQueryType = {
53
+ variables: {
54
+ candies: number;
55
+ },
56
+ response: {
57
+ candies: string | null | undefined;
58
+ }
59
+ };
62
60
  `);
63
61
  });
64
62
 
@@ -74,18 +72,18 @@ describe('graphql-flow generation', () => {
74
72
 
75
73
  expect(result).toMatchInlineSnapshot(`
76
74
  // SomeQueryType.js
77
- export type SomeQueryType = {|
78
- variables: {|
79
- id: string
80
- |},
81
- response: {|
82
-
75
+ export type SomeQueryType = {
76
+ variables: {
77
+ id: string;
78
+ },
79
+ response: {
80
+ human:
83
81
  /** A human character*/
84
- human: ?{|
85
- id: string
86
- |}
87
- |}
88
- |};
82
+ {
83
+ id: string;
84
+ } | null | undefined;
85
+ }
86
+ };
89
87
  `);
90
88
  });
91
89
 
@@ -105,23 +103,23 @@ describe('graphql-flow generation', () => {
105
103
 
106
104
  expect(result).toMatchInlineSnapshot(`
107
105
  // SomeQueryType.js
108
- export type SomeQueryType = {|
109
- variables: {||},
110
- response: {|
111
-
106
+ export type SomeQueryType = {
107
+ variables: {},
108
+ response: {
109
+ human:
112
110
  /** A human character*/
113
- human: ?{|
114
- friends: ?$ReadOnlyArray<?{|
115
- name: ?string
116
- |}>,
117
- homePlanet: ?string,
118
- id: string,
111
+ {
112
+ friends: ReadonlyArray<{
113
+ name: string | null | undefined;
114
+ } | null | undefined> | null | undefined;
115
+ homePlanet: string | null | undefined;
116
+ id: string;
119
117
 
120
118
  /** The person's name*/
121
- name: ?string,
122
- |}
123
- |}
124
- |};
119
+ name: string | null | undefined;
120
+ } | null | undefined;
121
+ }
122
+ };
125
123
  `);
126
124
  });
127
125
 
@@ -136,16 +134,16 @@ describe('graphql-flow generation', () => {
136
134
 
137
135
  expect(result).toMatchInlineSnapshot(`
138
136
  // SomeQueryType.js
139
- export type SomeQueryType = {|
140
- variables: {||},
141
- response: {|
142
-
137
+ export type SomeQueryType = {
138
+ variables: {},
139
+ response: {
140
+ human:
143
141
  /** A human character*/
144
- human: ?{|
145
- notDead: ?boolean
146
- |}
147
- |}
148
- |};
142
+ {
143
+ notDead: boolean | null | undefined;
144
+ } | null | undefined;
145
+ }
146
+ };
149
147
  `);
150
148
  });
151
149
 
@@ -166,23 +164,23 @@ describe('graphql-flow generation', () => {
166
164
  `);
167
165
  expect(result).toMatchInlineSnapshot(`
168
166
  // SomeQueryType.js
169
- export type SomeQueryType = {|
170
- variables: {||},
171
- response: {|
172
- friend: ?({|
173
- __typename: "Animal"
174
- |} | {|
175
- __typename: "Droid",
167
+ export type SomeQueryType = {
168
+ variables: {},
169
+ response: {
170
+ friend: ({
171
+ __typename: "Animal";
172
+ } | {
173
+ __typename: "Droid";
176
174
 
177
175
  /** The robot's primary function*/
178
- primaryFunction: string,
179
- |} | {|
180
- __typename: "Human",
181
- hands: ?number,
182
- id: string,
183
- |})
184
- |}
185
- |};
176
+ primaryFunction: string;
177
+ } | {
178
+ __typename: "Human";
179
+ hands: number | null | undefined;
180
+ id: string;
181
+ }) | null | undefined;
182
+ }
183
+ };
186
184
  `);
187
185
  });
188
186
 
@@ -217,63 +215,63 @@ describe('graphql-flow generation', () => {
217
215
 
218
216
  expect(result).toMatchInlineSnapshot(`
219
217
  // SomeQueryType.js
220
- export type SomeQueryType = {|
221
- variables: {||},
222
- response: {|
223
-
218
+ export type SomeQueryType = {
219
+ variables: {},
220
+ response: {
221
+ human:
224
222
  /** A human character*/
225
- human: ?{|
226
- alive: ?boolean,
227
- friends: ?$ReadOnlyArray<?({|
228
- __typename: "Droid",
229
- appearsIn: ?$ReadOnlyArray<
223
+ {
224
+ alive: boolean | null | undefined;
225
+ friends: ReadonlyArray<({
226
+ __typename: "Droid";
227
+ appearsIn: ReadonlyArray<
230
228
  /** - NEW_HOPE
231
229
  - EMPIRE
232
230
  - JEDI*/
233
- ?("NEW_HOPE" | "EMPIRE" | "JEDI")>,
234
- friends: ?$ReadOnlyArray<?{|
235
- id: string
236
- |}>,
237
- id: string,
238
- name: ?string,
239
- |} | {|
240
- __typename: "Human",
241
- appearsIn: ?$ReadOnlyArray<
231
+ ("NEW_HOPE" | "EMPIRE" | "JEDI") | null | undefined> | null | undefined;
232
+ friends: ReadonlyArray<{
233
+ id: string;
234
+ } | null | undefined> | null | undefined;
235
+ id: string;
236
+ name: string | null | undefined;
237
+ } | {
238
+ __typename: "Human";
239
+ appearsIn: ReadonlyArray<
242
240
  /** - NEW_HOPE
243
241
  - EMPIRE
244
242
  - JEDI*/
245
- ?("NEW_HOPE" | "EMPIRE" | "JEDI")>,
246
- friends: ?$ReadOnlyArray<?{|
247
- id: string
248
- |}>,
249
- hands: ?number,
250
- id: string,
251
- name: ?string,
252
- |})>,
253
- hands: ?number,
254
- homePlanet: ?string,
255
- id: string,
243
+ ("NEW_HOPE" | "EMPIRE" | "JEDI") | null | undefined> | null | undefined;
244
+ friends: ReadonlyArray<{
245
+ id: string;
246
+ } | null | undefined> | null | undefined;
247
+ hands: number | null | undefined;
248
+ id: string;
249
+ name: string | null | undefined;
250
+ }) | null | undefined> | null | undefined;
251
+ hands: number | null | undefined;
252
+ homePlanet: string | null | undefined;
253
+ id: string;
256
254
 
257
255
  /** The person's name*/
258
- name: ?string,
259
- |}
260
- |}
261
- |};
256
+ name: string | null | undefined;
257
+ } | null | undefined;
258
+ }
259
+ };
262
260
 
263
261
  // Profile.js
264
- export type Profile = {|
265
- __typename: "Droid" | "Human",
266
- appearsIn: ?$ReadOnlyArray<
262
+ export type Profile = {
263
+ __typename: "Droid" | "Human";
264
+ appearsIn: ReadonlyArray<
267
265
  /** - NEW_HOPE
268
266
  - EMPIRE
269
267
  - JEDI*/
270
- ?("NEW_HOPE" | "EMPIRE" | "JEDI")>,
271
- friends: ?$ReadOnlyArray<?{|
272
- id: string
273
- |}>,
274
- id: string,
275
- name: ?string,
276
- |};
268
+ ("NEW_HOPE" | "EMPIRE" | "JEDI") | null | undefined> | null | undefined;
269
+ friends: ReadonlyArray<{
270
+ id: string;
271
+ } | null | undefined> | null | undefined;
272
+ id: string;
273
+ name: string | null | undefined;
274
+ };
277
275
  `);
278
276
  });
279
277
 
@@ -293,18 +291,18 @@ describe('graphql-flow generation', () => {
293
291
 
294
292
  expect(result).toMatchInlineSnapshot(`
295
293
  // SomeQueryType.js
296
- export type SomeQueryType = {|
297
- variables: {||},
298
- response: {|
299
-
294
+ export type SomeQueryType = {
295
+ variables: {},
296
+ response: {
297
+ human:
300
298
  /** A human character*/
301
- human: ?{|
302
- friends: ?Array<?{|
303
- name: ?string
304
- |}>
305
- |}
306
- |}
307
- |};
299
+ {
300
+ friends: Array<{
301
+ name: string | null | undefined;
302
+ } | null | undefined> | null | undefined;
303
+ } | null | undefined;
304
+ }
305
+ };
308
306
  `);
309
307
  });
310
308
 
@@ -356,29 +354,29 @@ describe('graphql-flow generation', () => {
356
354
  );
357
355
  expect(result).toMatchInlineSnapshot(`
358
356
  // HelloType.js
359
- export type HelloType = {|
360
- variables: {||},
361
- response: {|
362
- hero: ?({|
363
- __typename: "Droid",
357
+ export type HelloType = {
358
+ variables: {},
359
+ response: {
360
+ hero: ({
361
+ __typename: "Droid";
364
362
 
365
363
  /** The robot's primary function*/
366
- primaryFunction: string,
367
- |} | {|
368
- __typename: "Human"
369
- |})
370
- |}
371
- |};
364
+ primaryFunction: string;
365
+ } | {
366
+ __typename: "Human";
367
+ }) | null | undefined;
368
+ }
369
+ };
372
370
 
373
371
  // onChar.js
374
- export type onChar = {|
375
- __typename: "Droid",
372
+ export type onChar = {
373
+ __typename: "Droid";
376
374
 
377
375
  /** The robot's primary function*/
378
- primaryFunction: string,
379
- |} | {|
380
- __typename: "Human"
381
- |};
376
+ primaryFunction: string;
377
+ } | {
378
+ __typename: "Human";
379
+ };
382
380
  `);
383
381
  });
384
382
 
@@ -406,33 +404,33 @@ describe('graphql-flow generation', () => {
406
404
  // `DepsType.response.droid`
407
405
  expect(result).toMatchInlineSnapshot(`
408
406
  // DepsType.js
409
- export type DepsType = {|
410
- variables: {||},
411
- response: {|
412
-
407
+ export type DepsType = {
408
+ variables: {},
409
+ response: {
410
+ droid:
413
411
  /** A robot character*/
414
- droid: ?{|
415
- __typename: "Droid",
416
- name: ?string,
412
+ {
413
+ __typename: "Droid";
414
+ name: string | null | undefined;
417
415
 
418
416
  /** The robot's primary function*/
419
- primaryFunction: string,
420
- |}
421
- |}
422
- |};
417
+ primaryFunction: string;
418
+ } | null | undefined;
419
+ }
420
+ };
423
421
 
424
422
  // Hello.js
425
- export type Hello = {|
426
- __typename: "Droid",
427
- name: ?string,
423
+ export type Hello = {
424
+ __typename: "Droid";
425
+ name: string | null | undefined;
428
426
 
429
427
  /** The robot's primary function*/
430
- primaryFunction: string,
431
- |} | {|
432
- __typename: "Human",
433
- homePlanet: ?string,
434
- name: ?string,
435
- |};
428
+ primaryFunction: string;
429
+ } | {
430
+ __typename: "Human";
431
+ homePlanet: string | null | undefined;
432
+ name: string | null | undefined;
433
+ };
436
434
  `);
437
435
  });
438
436
  });
@@ -461,32 +459,32 @@ describe('graphql-flow generation', () => {
461
459
 
462
460
  expect(result).toMatchInlineSnapshot(`
463
461
  // SomeQueryType.js
464
- export type SomeQueryType = {|
465
- variables: {||},
466
- response: {|
467
-
462
+ export type SomeQueryType = {
463
+ variables: {},
464
+ response: {
465
+ human:
468
466
  /** A human character*/
469
- human: ?SomeQuery_human
470
- |}
471
- |};
472
- export type SomeQuery_human = {|
473
- alive: ?boolean,
474
- friends: ?$ReadOnlyArray<?SomeQuery_human_friends>,
475
- hands: ?number,
476
- homePlanet: ?string,
477
- id: string,
467
+ SomeQuery_human | null | undefined;
468
+ }
469
+ };
470
+ export type SomeQuery_human = {
471
+ alive: boolean | null | undefined;
472
+ friends: ReadonlyArray<SomeQuery_human_friends | null | undefined> | null | undefined;
473
+ hands: number | null | undefined;
474
+ homePlanet: string | null | undefined;
475
+ id: string;
478
476
 
479
477
  /** The person's name*/
480
- name: ?string,
481
- |};
478
+ name: string | null | undefined;
479
+ };
482
480
  export type SomeQuery_human_friends = SomeQuery_human_friends_Droid | SomeQuery_human_friends_Human;
483
- export type SomeQuery_human_friends_Droid = {|
484
- __typename: "Droid"
485
- |};
486
- export type SomeQuery_human_friends_Human = {|
487
- __typename: "Human",
488
- hands: ?number,
489
- |};
481
+ export type SomeQuery_human_friends_Droid = {
482
+ __typename: "Droid";
483
+ };
484
+ export type SomeQuery_human_friends_Human = {
485
+ __typename: "Human";
486
+ hands: number | null | undefined;
487
+ };
490
488
  `);
491
489
  });
492
490
 
@@ -508,28 +506,28 @@ describe('graphql-flow generation', () => {
508
506
 
509
507
  expect(result).toMatchInlineSnapshot(`
510
508
  // SomeQueryType.js
511
- export type SomeQueryType = {|
512
- variables: {|
513
- id: string,
514
-
509
+ export type SomeQueryType = {
510
+ variables: {
511
+ id: string;
512
+ episode?:
515
513
  /** - NEW_HOPE
516
514
  - EMPIRE
517
515
  - JEDI*/
518
- episode?: ?("NEW_HOPE" | "EMPIRE" | "JEDI"),
519
- |},
520
- response: {|
521
- hero: ?{|
522
- name: ?string
523
- |},
524
-
516
+ ("NEW_HOPE" | "EMPIRE" | "JEDI") | null | undefined;
517
+ },
518
+ response: {
519
+ hero: {
520
+ name: string | null | undefined;
521
+ } | null | undefined;
522
+ human:
525
523
  /** A human character*/
526
- human: ?{|
527
- friends: ?Array<?{|
528
- name: ?string
529
- |}>
530
- |},
531
- |}
532
- |};
524
+ {
525
+ friends: Array<{
526
+ name: string | null | undefined;
527
+ } | null | undefined> | null | undefined;
528
+ } | null | undefined;
529
+ }
530
+ };
533
531
  `);
534
532
  });
535
533
 
@@ -548,20 +546,20 @@ describe('graphql-flow generation', () => {
548
546
  );
549
547
  expect(result).toMatchInlineSnapshot(`
550
548
  // SomeQueryType.js
551
- export type SomeQueryType = {|
552
- variables: {||},
553
- response: {|
554
- hero: ?({|
555
- id: string,
556
- name: ?string,
557
- |} | {|
558
- id: string,
549
+ export type SomeQueryType = {
550
+ variables: {},
551
+ response: {
552
+ hero: ({
553
+ id: string;
554
+ name: string | null | undefined;
555
+ } | {
556
+ id: string;
559
557
 
560
558
  /** The person's name*/
561
- name: ?string,
562
- |})
563
- |}
564
- |};
559
+ name: string | null | undefined;
560
+ }) | null | undefined;
561
+ }
562
+ };
565
563
  `);
566
564
  });
567
565
 
@@ -580,19 +578,19 @@ describe('graphql-flow generation', () => {
580
578
  );
581
579
  expect(result).toMatchInlineSnapshot(`
582
580
  // SomeQueryType.js
583
- export type SomeQueryType = {|
584
- variables: {||},
585
- response: {|
586
-
581
+ export type SomeQueryType = {
582
+ variables: {},
583
+ response: {
584
+ human:
587
585
  /** A human character*/
588
- human: ?{|
589
- id: string,
586
+ {
587
+ id: string;
590
588
 
591
589
  /** The person's name*/
592
- name: ?string,
593
- |}
594
- |}
595
- |};
590
+ name: string | null | undefined;
591
+ } | null | undefined;
592
+ }
593
+ };
596
594
  `);
597
595
  });
598
596
 
@@ -608,31 +606,30 @@ describe('graphql-flow generation', () => {
608
606
 
609
607
  expect(result).toMatchInlineSnapshot(`
610
608
  // addCharacterType.js
611
- export type addCharacterType = {|
612
- variables: {|
613
-
609
+ export type addCharacterType = {
610
+ variables: {
611
+ character:
614
612
  /** A character to add*/
615
- character: {|
616
-
613
+ {
617
614
  /** The new character's name*/
618
- name: string,
615
+ name: string;
619
616
 
620
617
  /** The character's friends*/
621
- friends?: ?$ReadOnlyArray<string>,
622
- appearsIn?: ?$ReadOnlyArray<
618
+ friends?: ReadonlyArray<string> | null | undefined;
619
+ appearsIn?: ReadonlyArray<
623
620
  /** - NEW_HOPE
624
621
  - EMPIRE
625
622
  - JEDI*/
626
- "NEW_HOPE" | "EMPIRE" | "JEDI">,
627
- candies: number,
628
- |}
629
- |},
630
- response: {|
631
- addCharacter: ?{|
632
- id: string
633
- |}
634
- |}
635
- |};
623
+ "NEW_HOPE" | "EMPIRE" | "JEDI"> | null | undefined;
624
+ candies: number;
625
+ };
626
+ },
627
+ response: {
628
+ addCharacter: {
629
+ id: string;
630
+ } | null | undefined;
631
+ }
632
+ };
636
633
  `);
637
634
  });
638
635
  });
@@ -1,4 +1,3 @@
1
- // @flow
2
1
  import type {CrawlConfig, GenerateConfig} from '../types';
3
2
 
4
3
  import {processPragmas} from '../generateTypeFiles';