@khanacademy/graphql-flow 1.2.0 → 3.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 (78) 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 +8 -7
  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 +9 -23
  10. package/dist/generateVariablesType.js +15 -31
  11. package/dist/index.js +8 -15
  12. package/dist/parser/parse.js +6 -7
  13. package/dist/parser/resolve.js +1 -2
  14. package/dist/parser/utils.js +1 -2
  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 +9 -8
  19. package/{dist/__test__/generateTypeFileContents.test.js → src/__test__/generateTypeFileContents.test.ts} +38 -41
  20. package/{dist/__test__/graphql-flow.test.js → src/__test__/graphql-flow.test.ts} +232 -235
  21. package/src/__test__/{processPragmas.test.js → processPragmas.test.ts} +0 -1
  22. package/{dist/cli/__test__/config.test.js → src/cli/__test__/config.test.ts} +5 -6
  23. package/src/cli/{config.js → config.ts} +10 -15
  24. package/src/cli/{run.js → run.ts} +5 -4
  25. package/src/{enums.js → enums.ts} +20 -22
  26. package/src/{generateResponseType.js → generateResponseType.ts} +167 -182
  27. package/src/{generateTypeFiles.js → generateTypeFiles.ts} +20 -30
  28. package/src/{generateVariablesType.js → generateVariablesType.ts} +34 -44
  29. package/{dist/index.js.flow → src/index.ts} +32 -24
  30. package/{dist/parser/__test__/parse.test.js → src/parser/__test__/parse.test.ts} +12 -11
  31. package/src/parser/{parse.js → parse.ts} +65 -47
  32. package/{dist/parser/resolve.js.flow → src/parser/resolve.ts} +15 -11
  33. package/{dist/parser/utils.js.flow → src/parser/utils.ts} +0 -1
  34. package/{dist/schemaFromIntrospectionData.js.flow → src/schemaFromIntrospectionData.ts} +1 -4
  35. package/src/types.ts +97 -0
  36. package/src/utils.ts +73 -0
  37. package/tools/{find-files-with-gql.js → find-files-with-gql.ts} +2 -3
  38. package/tsconfig.json +110 -0
  39. package/types/flow-to-ts.d.ts +1 -0
  40. package/dist/__test__/example-schema.graphql +0 -67
  41. package/dist/__test__/processPragmas.test.js +0 -76
  42. package/dist/cli/config.js.flow +0 -84
  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/enums.js.flow +0 -98
  47. package/dist/enums.js.map +0 -1
  48. package/dist/generateResponseType.js.flow +0 -583
  49. package/dist/generateResponseType.js.map +0 -1
  50. package/dist/generateTypeFiles.js.flow +0 -191
  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.flow +0 -417
  56. package/dist/parser/parse.js.map +0 -1
  57. package/dist/parser/resolve.js.map +0 -1
  58. package/dist/parser/utils.js.map +0 -1
  59. package/dist/schemaFromIntrospectionData.js.map +0 -1
  60. package/dist/types.js.flow +0 -88
  61. package/dist/types.js.map +0 -1
  62. package/dist/utils.js.flow +0 -50
  63. package/dist/utils.js.map +0 -1
  64. package/flow-typed/npm/@babel/types_vx.x.x.js +0 -5331
  65. package/flow-typed/npm/jest_v23.x.x.js +0 -1155
  66. package/flow-typed/overrides.js +0 -435
  67. package/src/__test__/generateTypeFileContents.test.js +0 -157
  68. package/src/__test__/graphql-flow.test.js +0 -639
  69. package/src/cli/__test__/config.test.js +0 -120
  70. package/src/cli/schema.json +0 -97
  71. package/src/index.js +0 -160
  72. package/src/parser/__test__/parse.test.js +0 -249
  73. package/src/parser/resolve.js +0 -119
  74. package/src/parser/utils.js +0 -25
  75. package/src/schemaFromIntrospectionData.js +0 -68
  76. package/src/types.js +0 -88
  77. package/src/utils.js +0 -50
  78. /package/{dist/cli/schema.json → schema.json} +0 -0
package/.babelrc CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "presets": [
3
- "@babel/preset-flow",
3
+ "@babel/preset-typescript",
4
4
  ["@babel/preset-env", { "targets": { "node": "12" } }]
5
5
  ]
6
6
  }
package/.eslintrc.js CHANGED
@@ -5,6 +5,5 @@ module.exports = {
5
5
  plugins: ['flowtype-errors'],
6
6
  rules: {
7
7
  'prettier/prettier': ['error', {singleQuote: true}],
8
- 'flowtype-errors/uncovered': 2,
9
8
  },
10
9
  };
@@ -36,7 +36,7 @@ jobs:
36
36
  fetch-depth: 0
37
37
  - uses: Khan/actions@shared-node-cache-v0.0.2
38
38
  with:
39
- node-version: 12.x
39
+ node-version: 16.x
40
40
 
41
41
  - name: Create Release Pull Request or Publish to npm
42
42
  id: changesets
package/CHANGELOG.md CHANGED
@@ -1,5 +1,21 @@
1
1
  # @khanacademy/graphql-flow
2
2
 
3
+ ## 3.0.0
4
+
5
+ ### Major Changes
6
+
7
+ - ad68019: Update 'graphql' to 16.3.x (requires consumers to change their version of 'graphql')
8
+
9
+ ## 2.0.0
10
+
11
+ ### Major Changes
12
+
13
+ - 38eb544: Generate TypeScript directly and only TypeScript
14
+
15
+ ### Minor Changes
16
+
17
+ - 2933c91: Migrate to TypeScript
18
+
3
19
  ## 1.2.0
4
20
 
5
21
  ### Minor Changes
@@ -7,7 +7,7 @@ exports.validateOrThrow = exports.loadConfigFile = exports.getSchemas = exports.
7
7
 
8
8
  var _schemaFromIntrospectionData = require("../schemaFromIntrospectionData");
9
9
 
10
- var _schema = _interopRequireDefault(require("./schema.json"));
10
+ var _schema = _interopRequireDefault(require("../../schema.json"));
11
11
 
12
12
  var _fs = _interopRequireDefault(require("fs"));
13
13
 
@@ -33,7 +33,6 @@ const validateOrThrow = (value, jsonSchema) => {
33
33
  exports.validateOrThrow = validateOrThrow;
34
34
 
35
35
  const loadConfigFile = configFile => {
36
- // $FlowIgnore
37
36
  const data = require(configFile); // eslint-disable-line flowtype-errors/uncovered
38
37
 
39
38
 
@@ -53,9 +52,12 @@ const getSchemas = schemaFilePath => {
53
52
 
54
53
  if (schemaFilePath.endsWith('.graphql')) {
55
54
  const schemaForValidation = (0, _graphql.buildSchema)(raw);
56
- const queryResponse = (0, _graphql.graphqlSync)(schemaForValidation, (0, _graphql.getIntrospectionQuery)({
57
- descriptions: true
58
- }));
55
+ const queryResponse = (0, _graphql.graphqlSync)({
56
+ schema: schemaForValidation,
57
+ source: (0, _graphql.getIntrospectionQuery)({
58
+ descriptions: true
59
+ })
60
+ });
59
61
  const schemaForTypeGeneration = (0, _schemaFromIntrospectionData.schemaFromIntrospectionData)( // eslint-disable-next-line flowtype-errors/uncovered
60
62
  queryResponse.data);
61
63
  return [schemaForValidation, schemaForTypeGeneration];
@@ -96,5 +98,4 @@ const findApplicableConfig = (path, configs) => {
96
98
  });
97
99
  };
98
100
 
99
- exports.findApplicableConfig = findApplicableConfig;
100
- //# sourceMappingURL=config.js.map
101
+ exports.findApplicableConfig = findApplicableConfig;
package/dist/cli/run.js CHANGED
@@ -220,5 +220,4 @@ if (config.crawl.dumpOperations) {
220
220
  recursive: true
221
221
  });
222
222
  (0, _fs.writeFileSync)(dumpOperations, JSON.stringify(printedOperations.sort(), null, 2));
223
- }
224
- //# sourceMappingURL=run.js.map
223
+ }
package/dist/enums.js CHANGED
@@ -17,14 +17,14 @@ function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj &&
17
17
  * Both input & output types can have enums & scalars.
18
18
  */
19
19
  const experimentalEnumTypeToFlow = (ctx, enumConfig, description) => {
20
- const enumDeclaration = babelTypes.enumDeclaration( // pass id into generic type annotation
21
- babelTypes.identifier(enumConfig.name), babelTypes.enumStringBody(enumConfig.enumValues.map(v => babelTypes.enumDefaultedMember(babelTypes.identifier(v.name)))));
20
+ const enumDeclaration = babelTypes.tsEnumDeclaration( // pass id into generic type annotation
21
+ babelTypes.identifier(enumConfig.name), enumConfig.enumValues.map(v => babelTypes.tsEnumMember(babelTypes.identifier(v.name), babelTypes.stringLiteral(v.name))));
22
22
 
23
23
  if (ctx.experimentalEnumsMap) {
24
24
  ctx.experimentalEnumsMap[enumConfig.name] = enumDeclaration;
25
25
  }
26
26
 
27
- return (0, _utils.maybeAddDescriptionComment)(description, babelTypes.genericTypeAnnotation(enumDeclaration.id));
27
+ return (0, _utils.maybeAddDescriptionComment)(description, babelTypes.tsTypeReference(enumDeclaration.id));
28
28
  };
29
29
 
30
30
  exports.experimentalEnumTypeToFlow = experimentalEnumTypeToFlow;
@@ -37,7 +37,7 @@ const enumTypeToFlow = (ctx, name) => {
37
37
  combinedDescription = enumConfig.description + '\n\n' + combinedDescription;
38
38
  }
39
39
 
40
- return ctx.experimentalEnumsMap ? experimentalEnumTypeToFlow(ctx, enumConfig, combinedDescription) : (0, _utils.maybeAddDescriptionComment)(combinedDescription, babelTypes.unionTypeAnnotation(enumConfig.enumValues.map(n => babelTypes.stringLiteralTypeAnnotation(n.name))));
40
+ return ctx.experimentalEnumsMap ? experimentalEnumTypeToFlow(ctx, enumConfig, combinedDescription) : (0, _utils.maybeAddDescriptionComment)(combinedDescription, babelTypes.tsUnionType(enumConfig.enumValues.map(n => babelTypes.tsLiteralType(babelTypes.stringLiteral(n.name)))));
41
41
  };
42
42
 
43
43
  exports.enumTypeToFlow = enumTypeToFlow;
@@ -54,18 +54,17 @@ exports.builtinScalars = builtinScalars;
54
54
 
55
55
  const scalarTypeToFlow = (ctx, name) => {
56
56
  if (builtinScalars[name]) {
57
- return babelTypes.genericTypeAnnotation(babelTypes.identifier(builtinScalars[name]));
57
+ return babelTypes.tsTypeReference(babelTypes.identifier(builtinScalars[name]));
58
58
  }
59
59
 
60
60
  const underlyingType = ctx.scalars[name];
61
61
 
62
62
  if (underlyingType != null) {
63
- return babelTypes.genericTypeAnnotation(babelTypes.identifier(underlyingType));
63
+ return babelTypes.tsTypeReference(babelTypes.identifier(underlyingType));
64
64
  }
65
65
 
66
66
  ctx.errors.push(`Unexpected scalar '${name}'! Please add it to the "scalars" argument at the callsite of 'generateFlowTypes()'.`);
67
- return babelTypes.genericTypeAnnotation(babelTypes.identifier(`UNKNOWN_SCALAR["${name}"]`));
67
+ return babelTypes.tsTypeReference(babelTypes.identifier(`UNKNOWN_SCALAR["${name}"]`));
68
68
  };
69
69
 
70
- exports.scalarTypeToFlow = scalarTypeToFlow;
71
- //# sourceMappingURL=enums.js.map
70
+ exports.scalarTypeToFlow = scalarTypeToFlow;
@@ -30,29 +30,21 @@ const generateResponseType = (schema, query, ctx) => {
30
30
  exports.generateResponseType = generateResponseType;
31
31
 
32
32
  const sortedObjectTypeAnnotation = (ctx, properties) => {
33
- const obj = babelTypes.objectTypeAnnotation(properties.sort((a, b) => {
34
- if (a.type === 'ObjectTypeProperty' && b.type === 'ObjectTypeProperty') {
33
+ const obj = (0, _utils.objectTypeFromProperties)(properties.sort((a, b) => {
34
+ if (a.type === 'TSPropertySignature' && b.type === 'TSPropertySignature') {
35
35
  const aName = a.key.type === 'Identifier' ? a.key.name : '';
36
36
  const bName = b.key.type === 'Identifier' ? b.key.name : '';
37
37
  return aName < bName ? -1 : 1;
38
38
  }
39
39
 
40
40
  return 0;
41
- }), undefined
42
- /* indexers */
43
- , undefined
44
- /* callProperties */
45
- , undefined
46
- /* internalSlots */
47
- , true
48
- /* exact */
49
- );
41
+ }));
50
42
  const name = ctx.path.join('_');
51
43
  const isTopLevelType = ctx.path.length <= 1;
52
44
 
53
45
  if (ctx.allObjectTypes != null && !isTopLevelType) {
54
46
  ctx.allObjectTypes[name] = obj;
55
- return babelTypes.genericTypeAnnotation(babelTypes.identifier(name));
47
+ return babelTypes.tsTypeReference(babelTypes.identifier(name));
56
48
  } else {
57
49
  return obj;
58
50
  }
@@ -84,7 +76,7 @@ const _typeToFlow = (ctx, type, selection) => {
84
76
  }
85
77
 
86
78
  if (type.kind === 'LIST') {
87
- return babelTypes.genericTypeAnnotation(ctx.readOnlyArray ? babelTypes.identifier('$ReadOnlyArray') : babelTypes.identifier('Array'), babelTypes.typeParameterInstantiation([typeToFlow(ctx, type.ofType, selection)]));
79
+ return babelTypes.tsTypeReference(ctx.readOnlyArray ? babelTypes.identifier('ReadonlyArray') : babelTypes.identifier('Array'), babelTypes.tsTypeParameterInstantiation([typeToFlow(ctx, type.ofType, selection)]));
88
80
  }
89
81
 
90
82
  if (type.kind === 'UNION') {
@@ -92,7 +84,7 @@ const _typeToFlow = (ctx, type, selection) => {
92
84
 
93
85
  if (!selection.selectionSet) {
94
86
  console.log('no selection set', selection);
95
- return babelTypes.anyTypeAnnotation();
87
+ return babelTypes.tsAnyKeyword();
96
88
  }
97
89
 
98
90
  return unionOrInterfaceToFlow(ctx, union, selection.selectionSet.selections);
@@ -101,7 +93,7 @@ const _typeToFlow = (ctx, type, selection) => {
101
93
  if (type.kind === 'INTERFACE') {
102
94
  if (!selection.selectionSet) {
103
95
  console.log('no selection set', selection);
104
- return babelTypes.anyTypeAnnotation();
96
+ return babelTypes.tsAnyKeyword();
105
97
  }
106
98
 
107
99
  return unionOrInterfaceToFlow(ctx, ctx.schema.interfacesByName[type.name], selection.selectionSet.selections);
@@ -113,21 +105,21 @@ const _typeToFlow = (ctx, type, selection) => {
113
105
 
114
106
  if (type.kind !== 'OBJECT') {
115
107
  console.log('not object', type);
116
- return babelTypes.anyTypeAnnotation();
108
+ return babelTypes.tsAnyKeyword();
117
109
  }
118
110
 
119
111
  const tname = type.name;
120
112
 
121
113
  if (!ctx.schema.typesByName[tname]) {
122
114
  console.log('unknown referenced type', tname);
123
- return babelTypes.anyTypeAnnotation();
115
+ return babelTypes.tsAnyKeyword();
124
116
  }
125
117
 
126
118
  const childType = ctx.schema.typesByName[tname];
127
119
 
128
120
  if (!selection.selectionSet) {
129
121
  console.log('no selection set', selection);
130
- return babelTypes.anyTypeAnnotation();
122
+ return babelTypes.tsAnyKeyword();
131
123
  }
132
124
 
133
125
  return (0, _utils.maybeAddDescriptionComment)(childType.description, querySelectionToObjectType(ctx, selection.selectionSet.selections, childType, tname));
@@ -146,7 +138,7 @@ const typeToFlow = (ctx, type, selection) => {
146
138
 
147
139
  const inner = _typeToFlow(ctx, type, selection);
148
140
 
149
- const result = babelTypes.nullableTypeAnnotation(inner);
141
+ const result = (0, _utils.nullableType)(inner);
150
142
  return (0, _utils.transferLeadingComments)(inner, result);
151
143
  };
152
144
 
@@ -158,8 +150,10 @@ const ensureOnlyOneTypenameProperty = properties => {
158
150
  // The apollo-utilities "addTypeName" utility will add it
159
151
  // even if it's already specified :( so we have to filter out
160
152
  // the extra one here.
161
- if (type.type === 'ObjectTypeProperty' && type.key.name === '__typename') {
162
- const name = type.value.type === 'StringLiteralTypeAnnotation' ? type.value.value : 'INVALID';
153
+ if (type.type === 'TSPropertySignature' && type.key.type === "Identifier" && type.key.name === '__typename') {
154
+ var _type$typeAnnotation;
155
+
156
+ const name = ((_type$typeAnnotation = type.typeAnnotation) === null || _type$typeAnnotation === void 0 ? void 0 : _type$typeAnnotation.typeAnnotation.type) === 'TSLiteralType' && type.typeAnnotation.typeAnnotation.literal.type === 'StringLiteral' ? type.typeAnnotation.typeAnnotation.literal.value : 'INVALID';
163
157
 
164
158
  if (seenTypeName) {
165
159
  if (name !== seenTypeName) {
@@ -181,7 +175,7 @@ const querySelectionToObjectType = (ctx, selections, type, typeName) => {
181
175
  };
182
176
 
183
177
  const objectPropertiesToFlow = (ctx, type, typeName, selections) => {
184
- return [].concat(...selections.map(selection => {
178
+ return selections.flatMap(selection => {
185
179
  switch (selection.kind) {
186
180
  case 'InlineFragment':
187
181
  {
@@ -199,7 +193,7 @@ const objectPropertiesToFlow = (ctx, type, typeName, selections) => {
199
193
  case 'FragmentSpread':
200
194
  if (!ctx.fragments[selection.name.value]) {
201
195
  ctx.errors.push(`No fragment named '${selection.name.value}'. Did you forget to include it in the template literal?`);
202
- return [babelTypes.objectTypeProperty(babelTypes.identifier(selection.name.value), babelTypes.genericTypeAnnotation(babelTypes.identifier(`UNKNOWN_FRAGMENT`)))];
196
+ return [babelTypes.tsPropertySignature(babelTypes.identifier(selection.name.value), babelTypes.tsTypeAnnotation(babelTypes.tsTypeReference(babelTypes.identifier(`UNKNOWN_FRAGMENT`))))];
203
197
  }
204
198
 
205
199
  return objectPropertiesToFlow(ctx, type, typeName, ctx.fragments[selection.name.value].selectionSet.selections);
@@ -209,25 +203,25 @@ const objectPropertiesToFlow = (ctx, type, typeName, selections) => {
209
203
  const alias = selection.alias ? selection.alias.value : name;
210
204
 
211
205
  if (name === '__typename') {
212
- return [babelTypes.objectTypeProperty(babelTypes.identifier(alias), babelTypes.stringLiteralTypeAnnotation(typeName))];
206
+ return [babelTypes.tsPropertySignature(babelTypes.identifier(alias), babelTypes.tsTypeAnnotation(babelTypes.tsLiteralType(babelTypes.stringLiteral(typeName))))];
213
207
  }
214
208
 
215
209
  if (!type.fieldsByName[name]) {
216
210
  ctx.errors.push(`Unknown field '${name}' for type '${typeName}'`);
217
- return babelTypes.objectTypeProperty(babelTypes.identifier(alias), babelTypes.genericTypeAnnotation(babelTypes.identifier(`UNKNOWN_FIELD["${name}"]`)));
211
+ return [babelTypes.tsPropertySignature(babelTypes.identifier(alias), babelTypes.tsTypeAnnotation(babelTypes.tsTypeReference(babelTypes.identifier(`UNKNOWN_FIELD["${name}"]`))))];
218
212
  }
219
213
 
220
214
  const typeField = type.fieldsByName[name];
221
- return [(0, _utils.maybeAddDescriptionComment)(typeField.description, (0, _utils.liftLeadingPropertyComments)(babelTypes.objectTypeProperty(babelTypes.identifier(alias), typeToFlow({ ...ctx,
215
+ return [(0, _utils.maybeAddDescriptionComment)(typeField.description, (0, _utils.liftLeadingPropertyComments)(babelTypes.tsPropertySignature(babelTypes.identifier(alias), babelTypes.tsTypeAnnotation(typeToFlow({ ...ctx,
222
216
  path: ctx.path.concat([alias])
223
- }, typeField.type, selection))))];
217
+ }, typeField.type, selection)))))];
224
218
 
225
219
  default:
226
- ctx.errors.push( // eslint-disable-next-line flowtype-errors/uncovered
220
+ ctx.errors.push( // @ts-expect-error: `selection` is `never` here
227
221
  `Unsupported selection kind '${selection.kind}'`);
228
222
  return [];
229
223
  }
230
- }));
224
+ });
231
225
  };
232
226
 
233
227
  exports.objectPropertiesToFlow = objectPropertiesToFlow;
@@ -248,11 +242,10 @@ const unionOrInterfaceToFlow = (ctx, type, selections) => {
248
242
 
249
243
  if (allFields) {
250
244
  const sharedAttributes = selectedAttributes[0].attributes.slice();
251
- const typeNameIndex = selectedAttributes[0].attributes.findIndex(x => x.type === 'ObjectTypeProperty' && x.key.type === 'Identifier' && x.key.name === '__typename');
245
+ const typeNameIndex = selectedAttributes[0].attributes.findIndex(x => x.type === 'TSPropertySignature' && x.key.type === 'Identifier' && x.key.name === '__typename');
252
246
 
253
247
  if (typeNameIndex !== -1) {
254
- sharedAttributes[typeNameIndex] = babelTypes.objectTypeProperty(babelTypes.identifier('__typename'), babelTypes.unionTypeAnnotation(selectedAttributes.map(attrs => // eslint-disable-next-line flowtype-errors/uncovered
255
- attrs.attributes[typeNameIndex].value)));
248
+ sharedAttributes[typeNameIndex] = babelTypes.tsPropertySignature(babelTypes.identifier('__typename'), babelTypes.tsTypeAnnotation(babelTypes.tsUnionType(selectedAttributes.map(attrs => attrs.attributes[typeNameIndex].typeAnnotation.typeAnnotation))));
256
249
  }
257
250
 
258
251
  return sortedObjectTypeAnnotation(ctx, sharedAttributes);
@@ -293,7 +286,7 @@ const unionOrInterfaceToFlow = (ctx, type, selections) => {
293
286
  */
294
287
 
295
288
 
296
- const result = babelTypes.unionTypeAnnotation(selectedAttributes.map(({
289
+ const result = babelTypes.tsUnionType(selectedAttributes.map(({
297
290
  typeName,
298
291
  attributes
299
292
  }) => sortedObjectTypeAnnotation({ ...ctx,
@@ -303,7 +296,7 @@ const unionOrInterfaceToFlow = (ctx, type, selections) => {
303
296
 
304
297
  if (ctx.allObjectTypes && ctx.path.length > 1) {
305
298
  ctx.allObjectTypes[name] = result;
306
- return babelTypes.genericTypeAnnotation(babelTypes.identifier(name));
299
+ return babelTypes.tsTypeReference(babelTypes.identifier(name));
307
300
  }
308
301
 
309
302
  return result;
@@ -314,7 +307,7 @@ exports.unionOrInterfaceToFlow = unionOrInterfaceToFlow;
314
307
  const unionOrInterfaceSelection = (config, type, possible, selection) => {
315
308
  if (selection.kind === 'Field' && selection.name.value === '__typename') {
316
309
  const alias = selection.alias ? selection.alias.value : selection.name.value;
317
- return [babelTypes.objectTypeProperty(babelTypes.identifier(alias), babelTypes.stringLiteralTypeAnnotation(possible.name))];
310
+ return [babelTypes.tsPropertySignature(babelTypes.identifier(alias), babelTypes.tsTypeAnnotation(babelTypes.tsLiteralType(babelTypes.stringLiteral(possible.name))))];
318
311
  }
319
312
 
320
313
  if (selection.kind === 'Field' && type.kind !== 'UNION') {
@@ -324,13 +317,13 @@ const unionOrInterfaceSelection = (config, type, possible, selection) => {
324
317
 
325
318
  if (!type.fieldsByName[name]) {
326
319
  config.errors.push('Unknown field: ' + name + ' on type ' + type.name + ' for possible ' + possible.name);
327
- return [babelTypes.objectTypeProperty(babelTypes.identifier(alias), babelTypes.genericTypeAnnotation(babelTypes.identifier(`UNKNOWN_FIELD`)))];
320
+ return [babelTypes.tsPropertySignature(babelTypes.identifier(alias), babelTypes.tsTypeAnnotation(babelTypes.tsTypeReference(babelTypes.identifier(`UNKNOWN_FIELD`))))];
328
321
  }
329
322
 
330
323
  const typeField = type.fieldsByName[name];
331
- return [(0, _utils.liftLeadingPropertyComments)(babelTypes.objectTypeProperty(babelTypes.identifier(alias), typeToFlow({ ...config,
324
+ return [(0, _utils.liftLeadingPropertyComments)(babelTypes.tsPropertySignature(babelTypes.identifier(alias), babelTypes.tsTypeAnnotation(typeToFlow({ ...config,
332
325
  path: config.path.concat([name])
333
- }, typeField.type, selection)))];
326
+ }, typeField.type, selection))))];
334
327
  }
335
328
 
336
329
  if (selection.kind === 'FragmentSpread') {
@@ -343,7 +336,7 @@ const unionOrInterfaceSelection = (config, type, possible, selection) => {
343
336
  const typeName = fragment.typeCondition.name.value;
344
337
 
345
338
  if (config.schema.interfacesByName[typeName] && config.schema.interfacesByName[typeName].possibleTypesByName[possible.name] || typeName === possible.name) {
346
- return [].concat(...fragment.selectionSet.selections.map(selection => unionOrInterfaceSelection(config, config.schema.typesByName[possible.name], possible, selection)));
339
+ return fragment.selectionSet.selections.flatMap(selection => unionOrInterfaceSelection(config, config.schema.typesByName[possible.name], possible, selection));
347
340
  } else {
348
341
  return [];
349
342
  }
@@ -373,5 +366,4 @@ Try using an inline fragment "... on SomeType {}".`);
373
366
  }
374
367
 
375
368
  return objectPropertiesToFlow(config, config.schema.typesByName[possible.name], possible.name, selection.selectionSet.selections);
376
- };
377
- //# sourceMappingURL=generateResponseType.js.map
369
+ };
@@ -11,14 +11,9 @@ var _path = _interopRequireDefault(require("path"));
11
11
 
12
12
  var _ = require(".");
13
13
 
14
- var _convert = require("@khanacademy/flow-to-ts/dist/convert.bundle");
15
-
16
14
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
17
15
 
18
- // eslint-disable-next-line flowtype-errors/uncovered
19
- const indexPrelude = regenerateCommand => `// @flow
20
- //
21
- // AUTOGENERATED
16
+ const indexPrelude = regenerateCommand => `// AUTOGENERATED
22
17
  // NOTE: New response types are added to this file automatically.
23
18
  // Outdated response types can be removed manually as they are deprecated.
24
19
  //${regenerateCommand ? ' To regenerate, run ' + regenerateCommand : ''}
@@ -29,12 +24,12 @@ const indexPrelude = regenerateCommand => `// @flow
29
24
  exports.indexPrelude = indexPrelude;
30
25
 
31
26
  const generateTypeFileContents = (fileName, schema, document, options, generatedDir, indexContents) => {
32
- const files = {}; /// Write export for __generated__/index.js if it doesn't exist
27
+ const files = {}; /// Write export for __generated__/index.ts if it doesn't exist
33
28
 
34
29
  const addToIndex = (filePath, typeName) => {
35
30
  if (options.typeScript || options.omitFileExtensions) {
36
31
  // Typescript doesn't like file extensions
37
- filePath = filePath.replace(/\.js$/, '');
32
+ filePath = filePath.replace(/\.ts$/, '');
38
33
  }
39
34
 
40
35
  const newLine = `export type {${typeName}} from './${_path.default.basename(filePath)}';`; // We match the entire new line to avoid issues that can arise from
@@ -56,11 +51,11 @@ const generateTypeFileContents = (fileName, schema, document, options, generated
56
51
  }) => {
57
52
  // We write all generated files to a `__generated__` subdir to keep
58
53
  // things tidy.
59
- const targetFileName = options.typeFileName ? options.typeFileName.replace('[operationName]', name) : `${name}.js`;
54
+ const targetFileName = options.typeFileName ? options.typeFileName.replace('[operationName]', name) : `${name}.ts`;
60
55
 
61
56
  const targetPath = _path.default.join(generatedDir, targetFileName);
62
57
 
63
- let fileContents = '// @' + `flow\n// AUTOGENERATED -- DO NOT EDIT\n` + `// Generated for operation '${name}' in file '../${_path.default.basename(fileName)}'\n` + (options.regenerateCommand ? `// To regenerate, run '${options.regenerateCommand}'.\n` : '') + code;
58
+ let fileContents = `// AUTOGENERATED -- DO NOT EDIT\n` + `// Generated for operation '${name}' in file '../${_path.default.basename(fileName)}'\n` + (options.regenerateCommand ? `// To regenerate, run '${options.regenerateCommand}'.\n` : '') + code;
64
59
 
65
60
  if (options.splitTypes && !isFragment) {
66
61
  fileContents += `\nexport type ${name} = ${typeName}['response'];\n` + `export type ${name}Variables = ${typeName}['variables'];\n`;
@@ -108,7 +103,7 @@ const getGeneratedDir = (fileName, options) => {
108
103
  const generateTypeFiles = (fileName, schema, document, options) => {
109
104
  const generatedDir = getGeneratedDir(fileName, options);
110
105
 
111
- const indexFile = _path.default.join(generatedDir, 'index' + (options.typeScript ? '.ts' : '.js'));
106
+ const indexFile = _path.default.join(generatedDir, 'index.ts');
112
107
 
113
108
  if (!_fs.default.existsSync(generatedDir)) {
114
109
  _fs.default.mkdirSync(generatedDir, {
@@ -127,16 +122,8 @@ const generateTypeFiles = (fileName, schema, document, options) => {
127
122
 
128
123
  _fs.default.writeFileSync(indexFile, indexContents);
129
124
 
130
- Object.keys(files).forEach(key => {
131
- let fname = key;
132
-
133
- if (options.typeScript) {
134
- // eslint-disable-next-line flowtype-errors/uncovered
135
- files[key] = (0, _convert.convert)(files[key]).replace(`variables: {}`, `variables: Record<never, never>`);
136
- fname = key.replace(/\.js$/, '.ts');
137
- }
138
-
139
- _fs.default.writeFileSync(fname, files[key]);
125
+ Object.keys(files).forEach(fname => {
126
+ _fs.default.writeFileSync(fname, files[fname]);
140
127
  });
141
128
 
142
129
  _fs.default.writeFileSync(indexFile, indexContents);
@@ -167,5 +154,4 @@ const processPragmas = (generateConfig, crawlConfig, rawSource) => {
167
154
  }
168
155
  };
169
156
 
170
- exports.processPragmas = processPragmas;
171
- //# sourceMappingURL=generateTypeFiles.js.map
157
+ exports.processPragmas = processPragmas;
@@ -19,31 +19,24 @@ function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj &&
19
19
 
20
20
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
21
21
 
22
+ // eslint-disable-line flowtype-errors/uncovered
22
23
  const inputObjectToFlow = (ctx, name) => {
23
24
  const inputObject = ctx.schema.inputObjectsByName[name];
24
25
 
25
26
  if (!inputObject) {
26
27
  ctx.errors.push(`Unknown input object ${name}`);
27
- return babelTypes.stringLiteralTypeAnnotation(`Unknown input object ${name}`);
28
+ return babelTypes.tsLiteralType(babelTypes.stringLiteral(`Unknown input object ${name}`));
28
29
  }
29
30
 
30
- return (0, _utils.maybeAddDescriptionComment)(inputObject.description, babelTypes.objectTypeAnnotation(inputObject.inputFields.map(vbl => (0, _utils.maybeAddDescriptionComment)(vbl.description, maybeOptionalObjectTypeProperty(vbl.name, inputRefToFlow(ctx, vbl.type)))), undefined
31
- /* indexers */
32
- , undefined
33
- /* callProperties */
34
- , undefined
35
- /* internalSlots */
36
- , true
37
- /* exact */
38
- ));
31
+ return (0, _utils.maybeAddDescriptionComment)(inputObject.description, (0, _utils.objectTypeFromProperties)(inputObject.inputFields.map(vbl => (0, _utils.maybeAddDescriptionComment)(vbl.description, maybeOptionalObjectTypeProperty(vbl.name, inputRefToFlow(ctx, vbl.type))))));
39
32
  };
40
33
 
41
34
  exports.inputObjectToFlow = inputObjectToFlow;
42
35
 
43
36
  const maybeOptionalObjectTypeProperty = (name, type) => {
44
- const prop = (0, _utils.liftLeadingPropertyComments)(babelTypes.objectTypeProperty(babelTypes.identifier(name), type));
37
+ const prop = (0, _utils.liftLeadingPropertyComments)(babelTypes.tsPropertySignature(babelTypes.identifier(name), babelTypes.tsTypeAnnotation(type)));
45
38
 
46
- if (type.type === 'NullableTypeAnnotation') {
39
+ if ((0, _utils.isnNullableType)(type)) {
47
40
  prop.optional = true;
48
41
  }
49
42
 
@@ -59,7 +52,7 @@ const inputRefToFlow = (ctx, inputRef) => {
59
52
 
60
53
  const result = _inputRefToFlow(ctx, inputRef);
61
54
 
62
- return (0, _utils.transferLeadingComments)(result, babelTypes.nullableTypeAnnotation(result));
55
+ return (0, _utils.transferLeadingComments)(result, (0, _utils.nullableType)(result));
63
56
  };
64
57
 
65
58
  exports.inputRefToFlow = inputRefToFlow;
@@ -78,10 +71,10 @@ const _inputRefToFlow = (ctx, inputRef) => {
78
71
  }
79
72
 
80
73
  if (inputRef.kind === 'LIST') {
81
- return babelTypes.genericTypeAnnotation(babelTypes.identifier('$ReadOnlyArray'), babelTypes.typeParameterInstantiation([inputRefToFlow(ctx, inputRef.ofType)]));
74
+ return babelTypes.tsTypeReference(babelTypes.identifier('ReadonlyArray'), babelTypes.tsTypeParameterInstantiation([inputRefToFlow(ctx, inputRef.ofType)]));
82
75
  }
83
76
 
84
- return babelTypes.stringLiteralTypeAnnotation(JSON.stringify(inputRef));
77
+ return babelTypes.tsLiteralType(babelTypes.stringLiteral(JSON.stringify(inputRef)));
85
78
  };
86
79
 
87
80
  const variableToFlow = (ctx, type) => {
@@ -91,7 +84,7 @@ const variableToFlow = (ctx, type) => {
91
84
 
92
85
  const result = _variableToFlow(ctx, type);
93
86
 
94
- return (0, _utils.transferLeadingComments)(result, babelTypes.nullableTypeAnnotation(result));
87
+ return (0, _utils.transferLeadingComments)(result, (0, _utils.nullableType)(result));
95
88
  };
96
89
 
97
90
  const _variableToFlow = (ctx, type) => {
@@ -107,33 +100,24 @@ const _variableToFlow = (ctx, type) => {
107
100
  const customScalarType = ctx.scalars[type.name.value];
108
101
 
109
102
  if (customScalarType) {
110
- return babelTypes.genericTypeAnnotation(babelTypes.identifier(customScalarType));
103
+ return babelTypes.tsTypeReference(babelTypes.identifier(customScalarType));
111
104
  }
112
105
 
113
106
  return inputObjectToFlow(ctx, type.name.value);
114
107
  }
115
108
 
116
109
  if (type.kind === 'ListType') {
117
- return babelTypes.genericTypeAnnotation(babelTypes.identifier('$ReadOnlyArray'), babelTypes.typeParameterInstantiation([variableToFlow(ctx, type.type)]));
110
+ return babelTypes.tsTypeReference(babelTypes.identifier('ReadonlyArray'), babelTypes.tsTypeParameterInstantiation([variableToFlow(ctx, type.type)]));
118
111
  }
119
112
 
120
- return babelTypes.stringLiteralTypeAnnotation('UNKNOWN' + JSON.stringify(type));
113
+ return babelTypes.tsLiteralType(babelTypes.stringLiteral('UNKNOWN' + JSON.stringify(type)));
121
114
  };
122
115
 
123
116
  const generateVariablesType = (schema, item, ctx) => {
124
- const variableObject = babelTypes.objectTypeAnnotation((item.variableDefinitions || []).map(vbl => {
117
+ const variableObject = (0, _utils.objectTypeFromProperties)((item.variableDefinitions || []).map(vbl => {
125
118
  return maybeOptionalObjectTypeProperty(vbl.variable.name.value, variableToFlow(ctx, vbl.type));
126
- }), undefined
127
- /* indexers */
128
- , undefined
129
- /* callProperties */
130
- , undefined
131
- /* internalSlots */
132
- , true
133
- /* exact */
134
- );
119
+ }));
135
120
  return (0, _generator.default)(variableObject).code; // eslint-disable-line flowtype-errors/uncovered
136
121
  };
137
122
 
138
- exports.generateVariablesType = generateVariablesType;
139
- //# sourceMappingURL=generateVariablesType.js.map
123
+ exports.generateVariablesType = generateVariablesType;
package/dist/index.js CHANGED
@@ -5,6 +5,8 @@ Object.defineProperty(exports, "__esModule", {
5
5
  });
6
6
  exports.documentToFlowTypes = exports.FlowGenerationError = void 0;
7
7
 
8
+ var _wonderStuffCore = require("@khanacademy/wonder-stuff-core");
9
+
8
10
  var _generator = _interopRequireDefault(require("@babel/generator"));
9
11
 
10
12
  var _generateResponseType = require("./generateResponseType");
@@ -13,16 +15,6 @@ var _generateVariablesType = require("./generateVariablesType");
13
15
 
14
16
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
15
17
 
16
- /* eslint-disable no-console */
17
-
18
- /* flow-uncovered-file */
19
-
20
- /**
21
- * This tool generates flowtype definitions from graphql queries.
22
- *
23
- * It relies on `introspection-query.json` existing in this directory,
24
- * which is produced by running `./tools/graphql-flow/sendIntrospection.js`.
25
- */
26
18
  // eslint-disable-line flowtype-errors/uncovered
27
19
  const optionsToConfig = (schema, definitions, options, errors = []) => {
28
20
  var _options$strictNullab, _options$readOnlyArra, _options$scalars, _options$typeScript, _options$omitFileExte;
@@ -48,7 +40,9 @@ const optionsToConfig = (schema, definitions, options, errors = []) => {
48
40
  path: [],
49
41
  experimentalEnumsMap: options !== null && options !== void 0 && options.experimentalEnums ? {} : undefined,
50
42
  ...internalOptions
51
- };
43
+ }; // @ts-expect-error: TS2322 - The type 'readonly []' is 'readonly' and cannot be
44
+ // assigned to the mutable type 'string[]'.
45
+
52
46
  return config;
53
47
  };
54
48
 
@@ -98,7 +92,7 @@ const documentToFlowTypes = (document, schema, options) => {
98
92
  const typeName = `${name}Type`; // TODO(jared): Maybe make this template configurable?
99
93
  // We'll see what's required to get webapp on board.
100
94
 
101
- const code = `export type ${typeName} = {|\n variables: ${variables},\n response: ${response}\n|};`;
95
+ const code = `export type ${typeName} = {\n variables: ${variables},\n response: ${response}\n};`;
102
96
  const extraTypes = codegenExtraTypes(types);
103
97
  const experimentalEnums = codegenExtraTypes(config.experimentalEnumsMap || {});
104
98
  return {
@@ -109,7 +103,7 @@ const documentToFlowTypes = (document, schema, options) => {
109
103
  experimentalEnums
110
104
  };
111
105
  }
112
- }).filter(Boolean);
106
+ }).filter(_wonderStuffCore.isTruthy);
113
107
 
114
108
  if (errors.length) {
115
109
  throw new FlowGenerationError(errors);
@@ -127,5 +121,4 @@ function codegenExtraTypes(types) {
127
121
  extraTypes[k] = (0, _generator.default)(types[k]).code;
128
122
  });
129
123
  return extraTypes;
130
- }
131
- //# sourceMappingURL=index.js.map
124
+ }