@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,583 +0,0 @@
1
- // @flow
2
- /* eslint-disable no-console */
3
- import generate from '@babel/generator'; // eslint-disable-line flowtype-errors/uncovered
4
- import * as babelTypes from '@babel/types';
5
- import {type BabelNodeFlowType} from '@babel/types';
6
- import type {
7
- FieldNode,
8
- IntrospectionOutputTypeRef,
9
- OperationDefinitionNode,
10
- FragmentDefinitionNode,
11
- } from 'graphql';
12
- import type {Context, Schema, Selections} from './types';
13
- import {
14
- liftLeadingPropertyComments,
15
- maybeAddDescriptionComment,
16
- transferLeadingComments,
17
- } from './utils';
18
- import {enumTypeToFlow, scalarTypeToFlow} from './enums';
19
- import type {
20
- IntrospectionField,
21
- IntrospectionInterfaceType,
22
- IntrospectionObjectType,
23
- IntrospectionUnionType,
24
- } from 'graphql/utilities/introspectionQuery';
25
- import {
26
- BabelNodeObjectTypeProperty,
27
- BabelNodeObjectTypeSpreadProperty,
28
- } from '@babel/types';
29
-
30
- export const generateResponseType = (
31
- schema: Schema,
32
- query: OperationDefinitionNode,
33
- ctx: Context,
34
- ): string => {
35
- const ast = querySelectionToObjectType(
36
- ctx,
37
- query.selectionSet.selections,
38
- query.operation === 'mutation'
39
- ? schema.typesByName.Mutation
40
- : schema.typesByName.Query,
41
- query.operation === 'mutation' ? 'mutation' : 'query',
42
- );
43
- // eslint-disable-next-line flowtype-errors/uncovered
44
- return generate(ast).code;
45
- };
46
-
47
- const sortedObjectTypeAnnotation = (
48
- ctx: Context,
49
- properties: Array<
50
- BabelNodeObjectTypeProperty | BabelNodeObjectTypeSpreadProperty,
51
- >,
52
- ) => {
53
- const obj = babelTypes.objectTypeAnnotation(
54
- properties.sort((a, b) => {
55
- if (
56
- a.type === 'ObjectTypeProperty' &&
57
- b.type === 'ObjectTypeProperty'
58
- ) {
59
- const aName = a.key.type === 'Identifier' ? a.key.name : '';
60
- const bName = b.key.type === 'Identifier' ? b.key.name : '';
61
- return aName < bName ? -1 : 1;
62
- }
63
- return 0;
64
- }),
65
- undefined /* indexers */,
66
- undefined /* callProperties */,
67
- undefined /* internalSlots */,
68
- true /* exact */,
69
- );
70
- const name = ctx.path.join('_');
71
- const isTopLevelType = ctx.path.length <= 1;
72
- if (ctx.allObjectTypes != null && !isTopLevelType) {
73
- ctx.allObjectTypes[name] = obj;
74
- return babelTypes.genericTypeAnnotation(babelTypes.identifier(name));
75
- } else {
76
- return obj;
77
- }
78
- };
79
-
80
- export const generateFragmentType = (
81
- schema: Schema,
82
- fragment: FragmentDefinitionNode,
83
- ctx: Context,
84
- ): string => {
85
- const onType = fragment.typeCondition.name.value;
86
- let ast;
87
-
88
- if (schema.typesByName[onType]) {
89
- ast = sortedObjectTypeAnnotation(
90
- ctx,
91
- objectPropertiesToFlow(
92
- ctx,
93
- schema.typesByName[onType],
94
- onType,
95
- fragment.selectionSet.selections,
96
- ),
97
- );
98
- } else if (schema.interfacesByName[onType]) {
99
- ast = unionOrInterfaceToFlow(
100
- ctx,
101
- ctx.schema.interfacesByName[onType],
102
- fragment.selectionSet.selections,
103
- );
104
- } else if (schema.unionsByName[onType]) {
105
- ast = unionOrInterfaceToFlow(
106
- ctx,
107
- ctx.schema.unionsByName[onType],
108
- fragment.selectionSet.selections,
109
- );
110
- } else {
111
- throw new Error(`Unknown ${onType}`);
112
- }
113
-
114
- // eslint-disable-next-line flowtype-errors/uncovered
115
- return generate(ast).code;
116
- };
117
-
118
- const _typeToFlow = (
119
- ctx: Context,
120
- type,
121
- selection,
122
- ): babelTypes.BabelNodeFlowType => {
123
- if (type.kind === 'SCALAR') {
124
- return scalarTypeToFlow(ctx, type.name);
125
- }
126
- if (type.kind === 'LIST') {
127
- return babelTypes.genericTypeAnnotation(
128
- ctx.readOnlyArray
129
- ? babelTypes.identifier('$ReadOnlyArray')
130
- : babelTypes.identifier('Array'),
131
- babelTypes.typeParameterInstantiation([
132
- typeToFlow(ctx, type.ofType, selection),
133
- ]),
134
- );
135
- }
136
- if (type.kind === 'UNION') {
137
- const union = ctx.schema.unionsByName[type.name];
138
- if (!selection.selectionSet) {
139
- console.log('no selection set', selection);
140
- return babelTypes.anyTypeAnnotation();
141
- }
142
- return unionOrInterfaceToFlow(
143
- ctx,
144
- union,
145
- selection.selectionSet.selections,
146
- );
147
- }
148
-
149
- if (type.kind === 'INTERFACE') {
150
- if (!selection.selectionSet) {
151
- console.log('no selection set', selection);
152
- return babelTypes.anyTypeAnnotation();
153
- }
154
- return unionOrInterfaceToFlow(
155
- ctx,
156
- ctx.schema.interfacesByName[type.name],
157
- selection.selectionSet.selections,
158
- );
159
- }
160
- if (type.kind === 'ENUM') {
161
- return enumTypeToFlow(ctx, type.name);
162
- }
163
- if (type.kind !== 'OBJECT') {
164
- console.log('not object', type);
165
- return babelTypes.anyTypeAnnotation();
166
- }
167
-
168
- const tname = type.name;
169
- if (!ctx.schema.typesByName[tname]) {
170
- console.log('unknown referenced type', tname);
171
- return babelTypes.anyTypeAnnotation();
172
- }
173
- const childType = ctx.schema.typesByName[tname];
174
- if (!selection.selectionSet) {
175
- console.log('no selection set', selection);
176
- return babelTypes.anyTypeAnnotation();
177
- }
178
- return maybeAddDescriptionComment(
179
- childType.description,
180
- querySelectionToObjectType(
181
- ctx,
182
- selection.selectionSet.selections,
183
- childType,
184
- tname,
185
- ),
186
- );
187
- };
188
-
189
- export const typeToFlow = (
190
- ctx: Context,
191
- type: IntrospectionOutputTypeRef,
192
- selection: FieldNode,
193
- ): babelTypes.BabelNodeFlowType => {
194
- // throw new Error('npoe');
195
- if (type.kind === 'NON_NULL') {
196
- return _typeToFlow(ctx, type.ofType, selection);
197
- }
198
- // If we don'babelTypes care about strict nullability checking, then pretend everything is non-null
199
- if (!ctx.strictNullability) {
200
- return _typeToFlow(ctx, type, selection);
201
- }
202
- const inner = _typeToFlow(ctx, type, selection);
203
- const result = babelTypes.nullableTypeAnnotation(inner);
204
- return transferLeadingComments(inner, result);
205
- };
206
-
207
- const ensureOnlyOneTypenameProperty = (properties) => {
208
- let seenTypeName: false | string = false;
209
- return properties.filter((type) => {
210
- // The apollo-utilities "addTypeName" utility will add it
211
- // even if it's already specified :( so we have to filter out
212
- // the extra one here.
213
- if (
214
- type.type === 'ObjectTypeProperty' &&
215
- type.key.name === '__typename'
216
- ) {
217
- const name =
218
- type.value.type === 'StringLiteralTypeAnnotation'
219
- ? type.value.value
220
- : 'INVALID';
221
- if (seenTypeName) {
222
- if (name !== seenTypeName) {
223
- throw new Error(
224
- `Got two different type names ${name}, ${seenTypeName}`,
225
- );
226
- }
227
- return false;
228
- }
229
- seenTypeName = name;
230
- }
231
- return true;
232
- });
233
- };
234
-
235
- const querySelectionToObjectType = (
236
- ctx: Context,
237
- selections,
238
- type,
239
- typeName: string,
240
- ): BabelNodeFlowType => {
241
- return sortedObjectTypeAnnotation(
242
- ctx,
243
- ensureOnlyOneTypenameProperty(
244
- objectPropertiesToFlow(ctx, type, typeName, selections),
245
- ),
246
- );
247
- };
248
-
249
- export const objectPropertiesToFlow = (
250
- ctx: Context,
251
- type: IntrospectionObjectType & {
252
- fieldsByName: {[name: string]: IntrospectionField},
253
- },
254
- typeName: string,
255
- selections: Selections,
256
- ): Array<BabelNodeObjectTypeProperty | BabelNodeObjectTypeSpreadProperty> => {
257
- return [].concat(
258
- ...selections.map((selection) => {
259
- switch (selection.kind) {
260
- case 'InlineFragment': {
261
- const newTypeName =
262
- selection.typeCondition?.name.value ?? typeName;
263
- if (newTypeName !== typeName) {
264
- return [];
265
- }
266
- return objectPropertiesToFlow(
267
- ctx,
268
- ctx.schema.typesByName[newTypeName],
269
- newTypeName,
270
- selection.selectionSet.selections,
271
- );
272
- }
273
- case 'FragmentSpread':
274
- if (!ctx.fragments[selection.name.value]) {
275
- ctx.errors.push(
276
- `No fragment named '${selection.name.value}'. Did you forget to include it in the template literal?`,
277
- );
278
- return [
279
- babelTypes.objectTypeProperty(
280
- babelTypes.identifier(selection.name.value),
281
- babelTypes.genericTypeAnnotation(
282
- babelTypes.identifier(`UNKNOWN_FRAGMENT`),
283
- ),
284
- ),
285
- ];
286
- }
287
-
288
- return objectPropertiesToFlow(
289
- ctx,
290
- type,
291
- typeName,
292
- ctx.fragments[selection.name.value].selectionSet
293
- .selections,
294
- );
295
-
296
- case 'Field':
297
- const name = selection.name.value;
298
- const alias: string = selection.alias
299
- ? selection.alias.value
300
- : name;
301
- if (name === '__typename') {
302
- return [
303
- babelTypes.objectTypeProperty(
304
- babelTypes.identifier(alias),
305
- babelTypes.stringLiteralTypeAnnotation(
306
- typeName,
307
- ),
308
- ),
309
- ];
310
- }
311
- if (!type.fieldsByName[name]) {
312
- ctx.errors.push(
313
- `Unknown field '${name}' for type '${typeName}'`,
314
- );
315
- return babelTypes.objectTypeProperty(
316
- babelTypes.identifier(alias),
317
- babelTypes.genericTypeAnnotation(
318
- babelTypes.identifier(
319
- `UNKNOWN_FIELD["${name}"]`,
320
- ),
321
- ),
322
- );
323
- }
324
- const typeField = type.fieldsByName[name];
325
-
326
- return [
327
- maybeAddDescriptionComment(
328
- typeField.description,
329
- liftLeadingPropertyComments(
330
- babelTypes.objectTypeProperty(
331
- babelTypes.identifier(alias),
332
- typeToFlow(
333
- {
334
- ...ctx,
335
- path: ctx.path.concat([alias]),
336
- },
337
- typeField.type,
338
- selection,
339
- ),
340
- ),
341
- ),
342
- ),
343
- ];
344
-
345
- default:
346
- ctx.errors.push(
347
- // eslint-disable-next-line flowtype-errors/uncovered
348
- `Unsupported selection kind '${selection.kind}'`,
349
- );
350
- return [];
351
- }
352
- }),
353
- );
354
- };
355
-
356
- export const unionOrInterfaceToFlow = (
357
- ctx: Context,
358
- type:
359
- | IntrospectionUnionType
360
- | (IntrospectionInterfaceType & {
361
- fieldsByName: {[key: string]: IntrospectionField},
362
- }),
363
- selections: Selections,
364
- ): BabelNodeFlowType => {
365
- const allFields = selections.every(
366
- (selection) => selection.kind === 'Field',
367
- );
368
- const selectedAttributes: Array<{
369
- attributes: Array<
370
- BabelNodeObjectTypeProperty | BabelNodeObjectTypeSpreadProperty,
371
- >,
372
- typeName: string,
373
- }> = type.possibleTypes
374
- .slice()
375
- .sort((a, b) => {
376
- return a.name < b.name ? -1 : 1;
377
- })
378
- .map((possible) => {
379
- const configWithUpdatedPath = {
380
- ...ctx,
381
- path: allFields ? ctx.path : ctx.path.concat([possible.name]),
382
- };
383
- return {
384
- typeName: possible.name,
385
- attributes: ensureOnlyOneTypenameProperty(
386
- selections
387
- .map((selection) =>
388
- unionOrInterfaceSelection(
389
- configWithUpdatedPath,
390
- type,
391
- possible,
392
- selection,
393
- ),
394
- )
395
- .flat(),
396
- ),
397
- };
398
- });
399
- // If they're all fields, the only selection that could be different is __typename
400
- if (allFields) {
401
- const sharedAttributes = selectedAttributes[0].attributes.slice();
402
- const typeNameIndex = selectedAttributes[0].attributes.findIndex(
403
- (x) =>
404
- x.type === 'ObjectTypeProperty' &&
405
- x.key.type === 'Identifier' &&
406
- x.key.name === '__typename',
407
- );
408
- if (typeNameIndex !== -1) {
409
- sharedAttributes[typeNameIndex] = babelTypes.objectTypeProperty(
410
- babelTypes.identifier('__typename'),
411
- babelTypes.unionTypeAnnotation(
412
- selectedAttributes.map(
413
- (attrs) =>
414
- // eslint-disable-next-line flowtype-errors/uncovered
415
- ((attrs.attributes[
416
- typeNameIndex
417
- ]: any): BabelNodeObjectTypeProperty).value,
418
- ),
419
- ),
420
- );
421
- }
422
- return sortedObjectTypeAnnotation(ctx, sharedAttributes);
423
- }
424
- if (selectedAttributes.length === 1) {
425
- return sortedObjectTypeAnnotation(
426
- ctx,
427
- selectedAttributes[0].attributes,
428
- );
429
- }
430
- /**
431
- * When generating the objects for the sub-options of a union, the path needs
432
- * to include the name of the object type.
433
- * ```
434
- * query getFriend {
435
- * friend {
436
- * ... on Human { id }
437
- * ... on Droid { arms }
438
- * }
439
- * }
440
- * ```
441
- * produces
442
- * ```
443
- * type getFriend = {friend: getFriend_friend_Human | getFriend_friend_Droid }
444
- * type getFriend_friend_Human = {id: string}
445
- * type getFriend_friend_Droid = {arms: number}
446
- * ```
447
- * Note that this is different from when an attribute has a plain object type.
448
- * ```
449
- * query getHuman {
450
- * me: human(id: "me") { id }
451
- * }
452
- * ```
453
- * produces
454
- * ```
455
- * type getHuman = {me: getHuman_me}
456
- * type getHuman_me = {id: string}
457
- * ```
458
- * instead of e.g. `getHuman_me_Human`.
459
- */
460
- const result = babelTypes.unionTypeAnnotation(
461
- selectedAttributes.map(({typeName, attributes}) =>
462
- sortedObjectTypeAnnotation(
463
- {...ctx, path: ctx.path.concat([typeName])},
464
- attributes,
465
- ),
466
- ),
467
- );
468
- const name = ctx.path.join('_');
469
- if (ctx.allObjectTypes && ctx.path.length > 1) {
470
- ctx.allObjectTypes[name] = result;
471
- return babelTypes.genericTypeAnnotation(babelTypes.identifier(name));
472
- }
473
- return result;
474
- };
475
- const unionOrInterfaceSelection = (
476
- config,
477
- type,
478
- possible,
479
- selection,
480
- ): Array<BabelNodeObjectTypeProperty | BabelNodeObjectTypeSpreadProperty> => {
481
- if (selection.kind === 'Field' && selection.name.value === '__typename') {
482
- const alias = selection.alias
483
- ? selection.alias.value
484
- : selection.name.value;
485
- return [
486
- babelTypes.objectTypeProperty(
487
- babelTypes.identifier(alias),
488
- babelTypes.stringLiteralTypeAnnotation(possible.name),
489
- ),
490
- ];
491
- }
492
- if (selection.kind === 'Field' && type.kind !== 'UNION') {
493
- // this is an interface
494
- const name = selection.name.value;
495
- const alias = selection.alias ? selection.alias.value : name;
496
- if (!type.fieldsByName[name]) {
497
- config.errors.push(
498
- 'Unknown field: ' +
499
- name +
500
- ' on type ' +
501
- type.name +
502
- ' for possible ' +
503
- possible.name,
504
- );
505
- return [
506
- babelTypes.objectTypeProperty(
507
- babelTypes.identifier(alias),
508
- babelTypes.genericTypeAnnotation(
509
- babelTypes.identifier(`UNKNOWN_FIELD`),
510
- ),
511
- ),
512
- ];
513
- }
514
- const typeField = type.fieldsByName[name];
515
- return [
516
- liftLeadingPropertyComments(
517
- babelTypes.objectTypeProperty(
518
- babelTypes.identifier(alias),
519
- typeToFlow(
520
- {...config, path: config.path.concat([name])},
521
- typeField.type,
522
- selection,
523
- ),
524
- ),
525
- ),
526
- ];
527
- }
528
- if (selection.kind === 'FragmentSpread') {
529
- const fragment = config.fragments[selection.name.value];
530
- if (!fragment) {
531
- throw new Error(`Unknown fragment ${selection.name.value}`);
532
- }
533
- const typeName = fragment.typeCondition.name.value;
534
- if (
535
- (config.schema.interfacesByName[typeName] &&
536
- config.schema.interfacesByName[typeName].possibleTypesByName[
537
- possible.name
538
- ]) ||
539
- typeName === possible.name
540
- ) {
541
- return [].concat(
542
- ...fragment.selectionSet.selections.map((selection) =>
543
- unionOrInterfaceSelection(
544
- config,
545
- config.schema.typesByName[possible.name],
546
- possible,
547
- selection,
548
- ),
549
- ),
550
- );
551
- } else {
552
- return [];
553
- }
554
- }
555
- if (selection.kind !== 'InlineFragment') {
556
- config.errors.push(
557
- `union selectors must be inline fragment: found ${selection.kind}`,
558
- );
559
- if (type.kind === 'UNION') {
560
- config.errors
561
- .push(`You're trying to select a field from the union ${type.name},
562
- but the only field you're allowed to select is "__typename".
563
- Try using an inline fragment "... on SomeType {}".`);
564
- }
565
- return [];
566
- }
567
- if (selection.typeCondition) {
568
- const typeName = selection.typeCondition.name.value;
569
- const indirectMatch =
570
- config.schema.interfacesByName[typeName]?.possibleTypesByName[
571
- possible.name
572
- ];
573
- if (typeName !== possible.name && !indirectMatch) {
574
- return [];
575
- }
576
- }
577
- return objectPropertiesToFlow(
578
- config,
579
- config.schema.typesByName[possible.name],
580
- possible.name,
581
- selection.selectionSet.selections,
582
- );
583
- };