@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
@@ -5,6 +5,8 @@ Object.defineProperty(exports, "__esModule", {
5
5
  });
6
6
  exports.processFiles = exports.processFile = void 0;
7
7
 
8
+ var _wonderStuffCore = require("@khanacademy/wonder-stuff-core");
9
+
8
10
  var _parser = require("@babel/parser");
9
11
 
10
12
  var _traverse = _interopRequireDefault(require("@babel/traverse"));
@@ -62,7 +64,7 @@ const processFile = (filePath, contents) => {
62
64
  };
63
65
  const resolved = typeof contents === 'string' ? filePath : contents.resolvedPath;
64
66
  const text = typeof contents === 'string' ? contents : contents.text;
65
- const plugins = resolved.match(/\.tsx?$/) ? ['typescript', filePath.endsWith('x') ? 'jsx' : null].filter(Boolean) : [['flow', {
67
+ const plugins = resolved.match(/\.tsx?$/) ? ['typescript', filePath.endsWith('x') ? 'jsx' : null].filter(_wonderStuffCore.isTruthy) : [['flow', {
66
68
  enums: true
67
69
  }], 'jsx'];
68
70
  /* eslint-disable flowtype-errors/uncovered */
@@ -245,9 +247,7 @@ const processFile = (filePath, contents) => {
245
247
 
246
248
  exports.processFile = processFile;
247
249
 
248
- const processTemplate = (tpl, result, getTemplate // getBinding?: (name: string) => Binding,
249
- // seenTemplates,
250
- ) => {
250
+ const processTemplate = (tpl, result, getTemplate) => {
251
251
  var _tpl$loc$start$line, _tpl$loc, _tpl$start, _tpl$end;
252
252
 
253
253
  // 'cooked' is the string as runtime javascript will see it.
@@ -293,7 +293,7 @@ const processTemplate = (tpl, result, getTemplate // getBinding?: (name: string)
293
293
 
294
294
  return {
295
295
  literals,
296
- expressions: expressions.filter(Boolean),
296
+ expressions: expressions.filter(_wonderStuffCore.isTruthy),
297
297
  loc: {
298
298
  line: (_tpl$loc$start$line = (_tpl$loc = tpl.loc) === null || _tpl$loc === void 0 ? void 0 : _tpl$loc.start.line) !== null && _tpl$loc$start$line !== void 0 ? _tpl$loc$start$line : -1,
299
299
  start: (_tpl$start = tpl.start) !== null && _tpl$start !== void 0 ? _tpl$start : -1,
@@ -361,5 +361,4 @@ const processFiles = (filePaths, getFileSource) => {
361
361
  return files;
362
362
  };
363
363
 
364
- exports.processFiles = processFiles;
365
- //# sourceMappingURL=parse.js.map
364
+ exports.processFiles = processFiles;
@@ -111,5 +111,4 @@ const resolveGqlTemplate = (template, files, errors, resolved, seen) => {
111
111
  raw: template
112
112
  };
113
113
  return resolved[key].document;
114
- };
115
- //# sourceMappingURL=resolve.js.map
114
+ };
@@ -33,5 +33,4 @@ const getPathWithExtension = pathWithoutExtension => {
33
33
  throw new Error("Can't find file at " + pathWithoutExtension);
34
34
  };
35
35
 
36
- exports.getPathWithExtension = getPathWithExtension;
37
- //# sourceMappingURL=utils.js.map
36
+ exports.getPathWithExtension = getPathWithExtension;
@@ -66,5 +66,4 @@ const schemaFromIntrospectionData = schema => {
66
66
  return result;
67
67
  };
68
68
 
69
- exports.schemaFromIntrospectionData = schemaFromIntrospectionData;
70
- //# sourceMappingURL=schemaFromIntrospectionData.js.map
69
+ exports.schemaFromIntrospectionData = schemaFromIntrospectionData;
package/dist/types.js CHANGED
@@ -1,2 +1 @@
1
- "use strict";
2
- //# sourceMappingURL=types.js.map
1
+ "use strict";
package/dist/utils.js CHANGED
@@ -4,7 +4,11 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports.addCommentAsLineComments = addCommentAsLineComments;
7
- exports.transferLeadingComments = exports.maybeAddDescriptionComment = exports.liftLeadingPropertyComments = void 0;
7
+ exports.isnNullableType = isnNullableType;
8
+ exports.maybeAddDescriptionComment = exports.liftLeadingPropertyComments = void 0;
9
+ exports.nullableType = nullableType;
10
+ exports.objectTypeFromProperties = objectTypeFromProperties;
11
+ exports.transferLeadingComments = void 0;
8
12
 
9
13
  var babelTypes = _interopRequireWildcard(require("@babel/types"));
10
14
 
@@ -13,7 +17,7 @@ function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "functio
13
17
  function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
14
18
 
15
19
  const liftLeadingPropertyComments = property => {
16
- return transferLeadingComments(property.value, property);
20
+ return transferLeadingComments(property.typeAnnotation, property);
17
21
  };
18
22
 
19
23
  exports.liftLeadingPropertyComments = liftLeadingPropertyComments;
@@ -51,4 +55,40 @@ const transferLeadingComments = (source, dest) => {
51
55
  };
52
56
 
53
57
  exports.transferLeadingComments = transferLeadingComments;
54
- //# sourceMappingURL=utils.js.map
58
+
59
+ function nullableType(type) {
60
+ return babelTypes.tsUnionType([type, babelTypes.tsNullKeyword(), babelTypes.tsUndefinedKeyword()]);
61
+ }
62
+
63
+ function isnNullableType(type) {
64
+ let hasNull = false;
65
+ let hasUndefined = false;
66
+
67
+ if (type.type === 'TSUnionType') {
68
+ for (const t of type.types) {
69
+ if (t.type === 'TSNullKeyword') {
70
+ hasNull = true;
71
+ } else if (t.type === 'TSUndefinedKeyword') {
72
+ hasUndefined = true;
73
+ }
74
+ }
75
+ }
76
+
77
+ return hasNull && hasUndefined;
78
+ }
79
+
80
+ function objectTypeFromProperties(properties) {
81
+ let exitingProperties = {};
82
+ let filteredProperties = properties.filter(p => {
83
+ if (p.key.type === 'Identifier') {
84
+ if (exitingProperties[p.key.name]) {
85
+ return false;
86
+ }
87
+
88
+ exitingProperties[p.key.name] = true;
89
+ }
90
+
91
+ return true;
92
+ });
93
+ return babelTypes.tsTypeLiteral(filteredProperties);
94
+ }
package/package.json CHANGED
@@ -1,14 +1,13 @@
1
1
  {
2
2
  "name": "@khanacademy/graphql-flow",
3
- "version": "1.2.0",
3
+ "version": "3.0.0",
4
4
  "bin": {
5
5
  "graphql-flow": "./dist/cli/run.js"
6
6
  },
7
7
  "scripts": {
8
8
  "test": "jest",
9
- "publish:ci": "yarn run build && yarn run copy-flow && changeset publish",
10
- "build": "babel src -D --out-dir dist --source-maps --ignore 'src/**/*.spec.js','src/**/*.test.js'",
11
- "copy-flow": "node ./build-copy-source.js"
9
+ "publish:ci": "yarn run build && changeset publish",
10
+ "build": "babel src --extensions '.ts, .tsx' --out-dir dist --ignore 'src/**/*.spec.ts','src/**/*.test.ts' && chmod 755 dist/cli/run.js"
12
11
  },
13
12
  "main": "dist/index.js",
14
13
  "devDependencies": {
@@ -16,9 +15,10 @@
16
15
  "@babel/eslint-parser": "^7.17.0",
17
16
  "@babel/polyfill": "^7.0.0",
18
17
  "@babel/preset-env": "^7.16.11",
19
- "@babel/preset-flow": "^7.16.7",
18
+ "@babel/preset-typescript": "^7.22.5",
20
19
  "@changesets/cli": "^2.21.1",
21
20
  "@khanacademy/eslint-config": "^0.1.0",
21
+ "@types/jest": "^29.5.3",
22
22
  "babel-jest": "23.4.2",
23
23
  "eslint": "8.7.0",
24
24
  "eslint-config-prettier": "7.0.0",
@@ -30,16 +30,17 @@
30
30
  "eslint-plugin-react-hooks": "^4.3.0",
31
31
  "flow-bin": "^0.172.0",
32
32
  "graphql-tag": "2.10.1",
33
- "jest": "^27.5.1"
33
+ "jest": "^27.5.1",
34
+ "typescript": "^5.1.6"
34
35
  },
35
36
  "dependencies": {
36
37
  "@babel/core": "^7.6.2",
37
38
  "@babel/generator": "^7.17.3",
38
39
  "@babel/traverse": "^7.17.3",
39
40
  "@babel/types": "^7.17.0",
40
- "@khanacademy/flow-to-ts": "^0.5.2",
41
+ "@khanacademy/wonder-stuff-core": "^1.5.1",
41
42
  "apollo-utilities": "^1.3.4",
42
- "graphql": "14.5.8",
43
+ "graphql": "^16.3.0",
43
44
  "jsonschema": "^1.4.1",
44
45
  "prettier": "^2.5.1",
45
46
  "prettier-eslint": "^13.0.0"
@@ -1,4 +1,3 @@
1
- // @flow
2
1
  // Test the generate the type file contents
3
2
 
4
3
  import {getSchemas} from '../cli/config';
@@ -24,35 +23,33 @@ describe('generateTypeFileContents', () => {
24
23
  indexPrelude('yarn queries'),
25
24
  );
26
25
  expect(indexContents).toMatchInlineSnapshot(`
27
- "// @flow
28
- //
29
- // AUTOGENERATED
26
+ "// AUTOGENERATED
30
27
  // NOTE: New response types are added to this file automatically.
31
28
  // Outdated response types can be removed manually as they are deprecated.
32
29
  // To regenerate, run yarn queries
33
30
  //
34
31
 
35
- export type {HelloType} from './Hello.js';
32
+ export type {HelloType} from './Hello.ts';
36
33
  "
37
34
  `);
38
35
  expect(
39
36
  Object.keys(files)
40
- .map((k) => `// ${k}\n${files[k]}`)
37
+ .map((k: any) => `// ${k}\n${files[k]}`)
41
38
  .join('\n\n'),
42
39
  ).toMatchInlineSnapshot(`
43
- "// __generated__/Hello.js
44
- // @flow
40
+ "// __generated__/Hello.ts
45
41
  // AUTOGENERATED -- DO NOT EDIT
46
42
  // Generated for operation 'Hello' in file '../hello.js'
47
- export type HelloType = {|
48
- variables: {||},
49
- response: {|
43
+ export type HelloType = {
44
+ variables: {},
45
+ response: {
46
+ human:
50
47
  /** A human character*/
51
- human: ?{|
52
- id: string
53
- |}
54
- |}
55
- |};
48
+ {
49
+ id: string;
50
+ } | null | undefined;
51
+ }
52
+ };
56
53
  export type Hello = HelloType['response'];
57
54
  export type HelloVariables = HelloType['variables'];
58
55
  "
@@ -80,22 +77,22 @@ describe('generateTypeFileContents', () => {
80
77
  );
81
78
  expect(
82
79
  Object.keys(files)
83
- .map((k) => `// ${k}\n${files[k]}`)
80
+ .map((k: any) => `// ${k}\n${files[k]}`)
84
81
  .join('\n\n'),
85
82
  ).toMatchInlineSnapshot(`
86
83
  "// __generated__/prefix-Hello-suffix.js
87
- // @flow
88
84
  // AUTOGENERATED -- DO NOT EDIT
89
85
  // Generated for operation 'Hello' in file '../hello.js'
90
- export type HelloType = {|
91
- variables: {||},
92
- response: {|
86
+ export type HelloType = {
87
+ variables: {},
88
+ response: {
89
+ human:
93
90
  /** A human character*/
94
- human: ?{|
95
- id: string
96
- |}
97
- |}
98
- |};
91
+ {
92
+ id: string;
93
+ } | null | undefined;
94
+ }
95
+ };
99
96
  export type Hello = HelloType['response'];
100
97
  export type HelloVariables = HelloType['variables'];
101
98
  "
@@ -123,31 +120,31 @@ describe('generateTypeFileContents', () => {
123
120
  );
124
121
  expect(
125
122
  Object.keys(files)
126
- .map((k) => `// ${k}\n${files[k]}`)
123
+ .map((k: any) => `// ${k}\n${files[k]}`)
127
124
  .join('\n\n'),
128
125
  ).toMatchInlineSnapshot(`
129
- "// __generated__/Hello.js
130
- // @flow
126
+ "// __generated__/Hello.ts
131
127
  // AUTOGENERATED -- DO NOT EDIT
132
128
  // Generated for operation 'Hello' in file '../hello.js'
133
- export type HelloType = {|
134
- variables: {||},
135
- response: {|
129
+ export type HelloType = {
130
+ variables: {},
131
+ response: {
132
+ human:
136
133
  /** A human character*/
137
- human: ?{|
138
- appearsIn: ?$ReadOnlyArray<
134
+ {
135
+ appearsIn: ReadonlyArray<
139
136
  /** - NEW_HOPE
140
137
  - EMPIRE
141
138
  - JEDI*/
142
- ?Episode>
143
- |}
144
- |}
145
- |};
139
+ Episode | null | undefined> | null | undefined;
140
+ } | null | undefined;
141
+ }
142
+ };
146
143
  /* eslint-disable no-undef */
147
144
  export enum Episode {
148
- NEW_HOPE,
149
- EMPIRE,
150
- JEDI,
145
+ NEW_HOPE = \\"NEW_HOPE\\",
146
+ EMPIRE = \\"EMPIRE\\",
147
+ JEDI = \\"JEDI\\",
151
148
  };
152
149
  /* eslint-enable no-undef */
153
150
  "