@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.
- package/.babelrc +1 -1
- package/.eslintrc.js +0 -1
- package/.github/workflows/changeset-release.yml +1 -1
- package/CHANGELOG.md +16 -0
- package/dist/cli/config.js +2 -4
- package/dist/cli/run.js +1 -2
- package/dist/enums.js +8 -9
- package/dist/generateResponseType.js +33 -41
- package/dist/generateTypeFiles.js +13 -35
- package/dist/generateVariablesType.js +15 -31
- package/dist/index.js +11 -17
- package/dist/parser/parse.js +10 -8
- package/dist/parser/resolve.js +11 -8
- package/dist/parser/utils.js +36 -0
- package/dist/schemaFromIntrospectionData.js +1 -2
- package/dist/types.js +1 -2
- package/dist/utils.js +43 -3
- package/package.json +8 -7
- package/{src/cli/schema.json → schema.json} +3 -0
- package/{dist/__test__/generateTypeFileContents.test.js → src/__test__/generateTypeFileContents.test.ts} +38 -41
- package/{dist/__test__/graphql-flow.test.js → src/__test__/graphql-flow.test.ts} +232 -235
- package/src/__test__/{processPragmas.test.js → processPragmas.test.ts} +0 -1
- package/{dist/cli/__test__/config.test.js → src/cli/__test__/config.test.ts} +5 -6
- package/{dist/cli/config.js.flow → src/cli/config.ts} +6 -11
- package/src/cli/{run.js → run.ts} +5 -4
- package/src/{enums.js → enums.ts} +20 -22
- package/{dist/generateResponseType.js.flow → src/generateResponseType.ts} +167 -182
- package/src/{generateTypeFiles.js → generateTypeFiles.ts} +24 -40
- package/src/{generateVariablesType.js → generateVariablesType.ts} +34 -44
- package/{dist/index.js.flow → src/index.ts} +33 -24
- package/{dist/parser/__test__/parse.test.js → src/parser/__test__/parse.test.ts} +12 -11
- package/{dist/parser/parse.js.flow → src/parser/parse.ts} +69 -48
- package/{dist/parser/resolve.js.flow → src/parser/resolve.ts} +25 -19
- package/src/parser/utils.ts +24 -0
- package/{dist/schemaFromIntrospectionData.js.flow → src/schemaFromIntrospectionData.ts} +1 -4
- package/src/types.ts +97 -0
- package/src/utils.ts +73 -0
- package/tools/{find-files-with-gql.js → find-files-with-gql.ts} +2 -3
- package/tsconfig.json +110 -0
- package/types/flow-to-ts.d.ts +1 -0
- package/dist/__test__/example-schema.graphql +0 -67
- package/dist/__test__/processPragmas.test.js +0 -76
- package/dist/cli/config.js.map +0 -1
- package/dist/cli/run.js.flow +0 -236
- package/dist/cli/run.js.map +0 -1
- package/dist/cli/schema.json +0 -94
- package/dist/enums.js.flow +0 -98
- package/dist/enums.js.map +0 -1
- package/dist/generateResponseType.js.map +0 -1
- package/dist/generateTypeFiles.js.flow +0 -197
- package/dist/generateTypeFiles.js.map +0 -1
- package/dist/generateVariablesType.js.flow +0 -156
- package/dist/generateVariablesType.js.map +0 -1
- package/dist/index.js.map +0 -1
- package/dist/parser/parse.js.map +0 -1
- package/dist/parser/resolve.js.map +0 -1
- package/dist/schemaFromIntrospectionData.js.map +0 -1
- package/dist/types.js.flow +0 -87
- package/dist/types.js.map +0 -1
- package/dist/utils.js.flow +0 -50
- package/dist/utils.js.map +0 -1
- package/flow-typed/npm/@babel/types_vx.x.x.js +0 -5331
- package/flow-typed/npm/jest_v23.x.x.js +0 -1155
- package/flow-typed/overrides.js +0 -435
- package/src/__test__/generateTypeFileContents.test.js +0 -157
- package/src/__test__/graphql-flow.test.js +0 -639
- package/src/cli/__test__/config.test.js +0 -120
- package/src/cli/config.js +0 -84
- package/src/generateResponseType.js +0 -583
- package/src/index.js +0 -159
- package/src/parser/__test__/parse.test.js +0 -249
- package/src/parser/parse.js +0 -414
- package/src/parser/resolve.js +0 -117
- package/src/schemaFromIntrospectionData.js +0 -68
- package/src/types.js +0 -87
- package/src/utils.js +0 -50
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
|
|
1
|
+
import {isTruthy} from '@khanacademy/wonder-stuff-core';
|
|
2
2
|
import type {
|
|
3
3
|
BabelNodeImportDeclaration,
|
|
4
4
|
BabelNodeVariableDeclarator,
|
|
@@ -11,6 +11,8 @@ import traverse from '@babel/traverse'; // eslint-disable-line flowtype-errors/u
|
|
|
11
11
|
|
|
12
12
|
import path from 'path';
|
|
13
13
|
|
|
14
|
+
import {getPathWithExtension} from './utils';
|
|
15
|
+
|
|
14
16
|
/**
|
|
15
17
|
* This file is responsible for finding all gql`-annotated
|
|
16
18
|
* template strings in a set of provided files, and for resolving
|
|
@@ -40,40 +42,54 @@ import path from 'path';
|
|
|
40
42
|
* in `gqlOp<Type>()` or not (to inform an auto-wrapper of the future)
|
|
41
43
|
*/
|
|
42
44
|
|
|
43
|
-
export type Template = {
|
|
44
|
-
literals: Array<string
|
|
45
|
-
expressions: Array<Document | Import
|
|
46
|
-
loc: Loc
|
|
47
|
-
|
|
48
|
-
export type Loc = {
|
|
45
|
+
export type Template = {
|
|
46
|
+
literals: Array<string>
|
|
47
|
+
expressions: Array<Document | Import>
|
|
48
|
+
loc: Loc
|
|
49
|
+
};
|
|
50
|
+
export type Loc = {
|
|
51
|
+
start: number
|
|
52
|
+
end: number
|
|
53
|
+
path: string
|
|
54
|
+
line: number
|
|
55
|
+
};
|
|
49
56
|
|
|
50
|
-
export type Document = {
|
|
51
|
-
type: 'document'
|
|
52
|
-
source: Template
|
|
53
|
-
|
|
54
|
-
export type Import = {
|
|
55
|
-
type: 'import'
|
|
56
|
-
name: string
|
|
57
|
-
path: string
|
|
58
|
-
loc: Loc
|
|
59
|
-
|
|
57
|
+
export type Document = {
|
|
58
|
+
type: 'document'
|
|
59
|
+
source: Template
|
|
60
|
+
};
|
|
61
|
+
export type Import = {
|
|
62
|
+
type: 'import'
|
|
63
|
+
name: string
|
|
64
|
+
path: string
|
|
65
|
+
loc: Loc
|
|
66
|
+
};
|
|
60
67
|
|
|
61
|
-
export type Operation = {
|
|
62
|
-
source: Template
|
|
68
|
+
export type Operation = {
|
|
69
|
+
source: Template
|
|
63
70
|
// TODO: Determine if an operation is already wrapped
|
|
64
71
|
// in `gqlOp` so we can automatically wrap if needed.
|
|
65
72
|
// needsWrapping: boolean,
|
|
66
|
-
|
|
73
|
+
};
|
|
67
74
|
|
|
68
|
-
export type FileResult = {
|
|
69
|
-
path: string
|
|
70
|
-
operations: Array<Operation
|
|
71
|
-
exports: {
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
+
export type FileResult = {
|
|
76
|
+
path: string
|
|
77
|
+
operations: Array<Operation>
|
|
78
|
+
exports: {
|
|
79
|
+
[key: string]: Document | Import
|
|
80
|
+
}
|
|
81
|
+
locals: {
|
|
82
|
+
[key: string]: Document | Import
|
|
83
|
+
}
|
|
84
|
+
errors: Array<{
|
|
85
|
+
loc: Loc
|
|
86
|
+
message: string
|
|
87
|
+
}>
|
|
88
|
+
};
|
|
75
89
|
|
|
76
|
-
export type Files = {
|
|
90
|
+
export type Files = {
|
|
91
|
+
[path: string]: FileResult
|
|
92
|
+
};
|
|
77
93
|
|
|
78
94
|
/**
|
|
79
95
|
* Finds all referenced imports that might possibly be relevant
|
|
@@ -84,11 +100,12 @@ export type Files = {[path: string]: FileResult};
|
|
|
84
100
|
* from a graphql template are treated as relevant.
|
|
85
101
|
*/
|
|
86
102
|
const listExternalReferences = (file: FileResult): Array<string> => {
|
|
87
|
-
const paths = {};
|
|
103
|
+
const paths: Record<string, any> = {};
|
|
88
104
|
const add = (v: Document | Import, followImports: boolean) => {
|
|
89
105
|
if (v.type === 'import') {
|
|
90
106
|
if (followImports) {
|
|
91
|
-
|
|
107
|
+
const absPath = getPathWithExtension(v.path);
|
|
108
|
+
paths[absPath] = true;
|
|
92
109
|
}
|
|
93
110
|
} else {
|
|
94
111
|
v.source.expressions.forEach((expr) => add(expr, true));
|
|
@@ -123,7 +140,10 @@ const listExternalReferences = (file: FileResult): Array<string> => {
|
|
|
123
140
|
|
|
124
141
|
export const processFile = (
|
|
125
142
|
filePath: string,
|
|
126
|
-
contents: string | {
|
|
143
|
+
contents: string | {
|
|
144
|
+
text: string
|
|
145
|
+
resolvedPath: string
|
|
146
|
+
},
|
|
127
147
|
): FileResult => {
|
|
128
148
|
const dir = path.dirname(filePath);
|
|
129
149
|
const result: FileResult = {
|
|
@@ -137,7 +157,7 @@ export const processFile = (
|
|
|
137
157
|
typeof contents === 'string' ? filePath : contents.resolvedPath;
|
|
138
158
|
const text = typeof contents === 'string' ? contents : contents.text;
|
|
139
159
|
const plugins = resolved.match(/\.tsx?$/)
|
|
140
|
-
? ['typescript', filePath.endsWith('x') ? 'jsx' : null].filter(
|
|
160
|
+
? ['typescript', filePath.endsWith('x') ? 'jsx' : null].filter(isTruthy)
|
|
141
161
|
: [['flow', {enums: true}], 'jsx'];
|
|
142
162
|
/* eslint-disable flowtype-errors/uncovered */
|
|
143
163
|
const ast: BabelNodeFile = parse(text, {
|
|
@@ -147,7 +167,9 @@ export const processFile = (
|
|
|
147
167
|
});
|
|
148
168
|
/* eslint-enable flowtype-errors/uncovered */
|
|
149
169
|
const gqlTagNames = [];
|
|
150
|
-
const seenTemplates: {
|
|
170
|
+
const seenTemplates: {
|
|
171
|
+
[key: number]: Document | false
|
|
172
|
+
} = {};
|
|
151
173
|
|
|
152
174
|
ast.program.body.forEach((toplevel) => {
|
|
153
175
|
if (toplevel.type === 'ImportDeclaration') {
|
|
@@ -173,7 +195,7 @@ export const processFile = (
|
|
|
173
195
|
const importPath = source.value.startsWith('.')
|
|
174
196
|
? path.resolve(path.join(dir, source.value))
|
|
175
197
|
: source.value;
|
|
176
|
-
toplevel.specifiers?.forEach((spec) => {
|
|
198
|
+
toplevel.specifiers?.forEach((spec: unknown) => {
|
|
177
199
|
if (
|
|
178
200
|
spec.type === 'ExportSpecifier' &&
|
|
179
201
|
spec.exported.type === 'Identifier'
|
|
@@ -192,7 +214,7 @@ export const processFile = (
|
|
|
192
214
|
}
|
|
193
215
|
});
|
|
194
216
|
} else {
|
|
195
|
-
toplevel.specifiers?.forEach((spec) => {
|
|
217
|
+
toplevel.specifiers?.forEach((spec: unknown) => {
|
|
196
218
|
if (spec.type === 'ExportSpecifier') {
|
|
197
219
|
const local = result.locals[spec.local.name];
|
|
198
220
|
if (local && spec.exported.type === 'Identifier') {
|
|
@@ -282,7 +304,7 @@ export const processFile = (
|
|
|
282
304
|
|
|
283
305
|
/* eslint-disable flowtype-errors/uncovered */
|
|
284
306
|
traverse(ast, {
|
|
285
|
-
TaggedTemplateExpression(path) {
|
|
307
|
+
TaggedTemplateExpression(path: unknown) {
|
|
286
308
|
visitTpl(path.node, (name) => {
|
|
287
309
|
const binding = path.scope.getBinding(name);
|
|
288
310
|
const start = binding.path.node.init
|
|
@@ -303,10 +325,10 @@ export const processFile = (
|
|
|
303
325
|
const processTemplate = (
|
|
304
326
|
tpl: BabelNodeTaggedTemplateExpression,
|
|
305
327
|
result: FileResult,
|
|
306
|
-
getTemplate?: (name: string) => Document | null,
|
|
307
328
|
// getBinding?: (name: string) => Binding,
|
|
308
329
|
// seenTemplates,
|
|
309
|
-
|
|
330
|
+
getTemplate?: (name: string) => Document | null,
|
|
331
|
+
): Template | null | undefined => {
|
|
310
332
|
// 'cooked' is the string as runtime javascript will see it.
|
|
311
333
|
const literals = tpl.quasi.quasis.map((q) => q.value.cooked || '');
|
|
312
334
|
const expressions = tpl.quasi.expressions.map(
|
|
@@ -344,7 +366,7 @@ const processTemplate = (
|
|
|
344
366
|
}
|
|
345
367
|
return {
|
|
346
368
|
literals,
|
|
347
|
-
expressions: expressions.filter(
|
|
369
|
+
expressions: expressions.filter(isTruthy),
|
|
348
370
|
loc: {
|
|
349
371
|
line: tpl.loc?.start.line ?? -1,
|
|
350
372
|
start: tpl.start ?? -1,
|
|
@@ -354,18 +376,16 @@ const processTemplate = (
|
|
|
354
376
|
};
|
|
355
377
|
};
|
|
356
378
|
|
|
357
|
-
const getLocals = (
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
myPath: string,
|
|
361
|
-
): ?{[key: string]: Import} => {
|
|
379
|
+
const getLocals = (dir: unknown, toplevel: BabelNodeImportDeclaration, myPath: string): {
|
|
380
|
+
[key: string]: Import
|
|
381
|
+
} | null | undefined => {
|
|
362
382
|
if (toplevel.importKind === 'type') {
|
|
363
383
|
return null;
|
|
364
384
|
}
|
|
365
385
|
const importPath = toplevel.source.value.startsWith('.')
|
|
366
386
|
? path.resolve(path.join(dir, toplevel.source.value))
|
|
367
387
|
: toplevel.source.value;
|
|
368
|
-
const locals = {};
|
|
388
|
+
const locals: Record<string, any> = {};
|
|
369
389
|
toplevel.specifiers.forEach((spec) => {
|
|
370
390
|
if (spec.type === 'ImportDefaultSpecifier') {
|
|
371
391
|
locals[spec.local.name] = {
|
|
@@ -391,9 +411,10 @@ const getLocals = (
|
|
|
391
411
|
|
|
392
412
|
export const processFiles = (
|
|
393
413
|
filePaths: Array<string>,
|
|
394
|
-
getFileSource: (
|
|
395
|
-
|
|
396
|
-
|
|
414
|
+
getFileSource: (path: string) => string | {
|
|
415
|
+
text: string
|
|
416
|
+
resolvedPath: string
|
|
417
|
+
},
|
|
397
418
|
): Files => {
|
|
398
419
|
const files: Files = {};
|
|
399
420
|
const toProcess = filePaths.slice();
|
|
@@ -1,18 +1,19 @@
|
|
|
1
|
-
// @flow
|
|
2
1
|
import gql from 'graphql-tag';
|
|
2
|
+
import {getPathWithExtension} from './utils';
|
|
3
3
|
import type {DocumentNode} from 'graphql/language/ast';
|
|
4
4
|
import type {FileResult, Files, Import, Template, Document} from './parse';
|
|
5
5
|
|
|
6
6
|
export type Resolved = {
|
|
7
7
|
[key: string]: {
|
|
8
|
-
document: DocumentNode
|
|
9
|
-
raw: Template
|
|
10
|
-
}
|
|
8
|
+
document: DocumentNode
|
|
9
|
+
raw: Template
|
|
10
|
+
}
|
|
11
11
|
};
|
|
12
12
|
|
|
13
|
-
export const resolveDocuments = (
|
|
14
|
-
|
|
15
|
-
|
|
13
|
+
export const resolveDocuments = (files: Files): {
|
|
14
|
+
resolved: Resolved
|
|
15
|
+
errors: FileResult['errors']
|
|
16
|
+
} => {
|
|
16
17
|
const resolved: Resolved = {};
|
|
17
18
|
const errors: FileResult['errors'] = [];
|
|
18
19
|
Object.keys(files).forEach((path) => {
|
|
@@ -34,27 +35,30 @@ const resolveImport = (
|
|
|
34
35
|
expr: Import,
|
|
35
36
|
files: Files,
|
|
36
37
|
errors: FileResult['errors'],
|
|
37
|
-
seen: {
|
|
38
|
-
|
|
39
|
-
|
|
38
|
+
seen: {
|
|
39
|
+
[key: string]: true
|
|
40
|
+
},
|
|
41
|
+
): Document | null | undefined => {
|
|
42
|
+
const absPath: string = getPathWithExtension(expr.path);
|
|
43
|
+
if (seen[absPath]) {
|
|
40
44
|
errors.push({
|
|
41
45
|
loc: expr.loc,
|
|
42
|
-
message: `Circular import ${Object.keys(seen).join(
|
|
43
|
-
|
|
44
|
-
}`,
|
|
46
|
+
message: `Circular import ${Object.keys(seen).join(
|
|
47
|
+
' -> ',
|
|
48
|
+
)} -> ${absPath}`,
|
|
45
49
|
});
|
|
46
50
|
return null;
|
|
47
51
|
}
|
|
48
|
-
seen[
|
|
49
|
-
const res = files[
|
|
52
|
+
seen[absPath] = true;
|
|
53
|
+
const res = files[absPath];
|
|
50
54
|
if (!res) {
|
|
51
|
-
errors.push({loc: expr.loc, message: `No file ${
|
|
55
|
+
errors.push({loc: expr.loc, message: `No file ${absPath}`});
|
|
52
56
|
return null;
|
|
53
57
|
}
|
|
54
58
|
if (!res.exports[expr.name]) {
|
|
55
59
|
errors.push({
|
|
56
60
|
loc: expr.loc,
|
|
57
|
-
message: `${
|
|
61
|
+
message: `${absPath} has no valid gql export ${expr.name}`,
|
|
58
62
|
});
|
|
59
63
|
return null;
|
|
60
64
|
}
|
|
@@ -70,8 +74,10 @@ const resolveGqlTemplate = (
|
|
|
70
74
|
files: Files,
|
|
71
75
|
errors: FileResult['errors'],
|
|
72
76
|
resolved: Resolved,
|
|
73
|
-
seen: {
|
|
74
|
-
|
|
77
|
+
seen: {
|
|
78
|
+
[key: string]: Template
|
|
79
|
+
},
|
|
80
|
+
): DocumentNode | null | undefined => {
|
|
75
81
|
const key = template.loc.path + ':' + template.loc.line;
|
|
76
82
|
if (seen[key]) {
|
|
77
83
|
errors.push({
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import fs from 'fs';
|
|
2
|
+
|
|
3
|
+
export const getPathWithExtension = (pathWithoutExtension: string): string => {
|
|
4
|
+
if (
|
|
5
|
+
/\.(less|css|png|gif|jpg|jpeg|js|jsx|ts|tsx|mjs)$/.test(
|
|
6
|
+
pathWithoutExtension,
|
|
7
|
+
)
|
|
8
|
+
) {
|
|
9
|
+
return pathWithoutExtension;
|
|
10
|
+
}
|
|
11
|
+
if (fs.existsSync(pathWithoutExtension + '.js')) {
|
|
12
|
+
return pathWithoutExtension + '.js';
|
|
13
|
+
}
|
|
14
|
+
if (fs.existsSync(pathWithoutExtension + '.jsx')) {
|
|
15
|
+
return pathWithoutExtension + '.jsx';
|
|
16
|
+
}
|
|
17
|
+
if (fs.existsSync(pathWithoutExtension + '.tsx')) {
|
|
18
|
+
return pathWithoutExtension + '.tsx';
|
|
19
|
+
}
|
|
20
|
+
if (fs.existsSync(pathWithoutExtension + '.ts')) {
|
|
21
|
+
return pathWithoutExtension + '.ts';
|
|
22
|
+
}
|
|
23
|
+
throw new Error("Can't find file at " + pathWithoutExtension);
|
|
24
|
+
};
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
// @flow
|
|
2
1
|
/**
|
|
3
2
|
* Takes the introspectionQuery response and parses it into the "Schema"
|
|
4
3
|
* type that we use to look up types, interfaces, etc.
|
|
@@ -6,9 +5,7 @@
|
|
|
6
5
|
import type {IntrospectionQuery} from 'graphql';
|
|
7
6
|
import type {Schema} from './types';
|
|
8
7
|
|
|
9
|
-
export const schemaFromIntrospectionData = (
|
|
10
|
-
schema: IntrospectionQuery,
|
|
11
|
-
): Schema => {
|
|
8
|
+
export const schemaFromIntrospectionData = (schema: IntrospectionQuery): Schema => {
|
|
12
9
|
const result: Schema = {
|
|
13
10
|
interfacesByName: {},
|
|
14
11
|
typesByName: {},
|
package/src/types.ts
ADDED
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
import type {Node} from '@babel/types';
|
|
2
|
+
import type {
|
|
3
|
+
FragmentDefinitionNode,
|
|
4
|
+
IntrospectionEnumType,
|
|
5
|
+
IntrospectionField,
|
|
6
|
+
IntrospectionInputObjectType,
|
|
7
|
+
IntrospectionInterfaceType,
|
|
8
|
+
IntrospectionObjectType,
|
|
9
|
+
IntrospectionUnionType,
|
|
10
|
+
SelectionNode,
|
|
11
|
+
} from 'graphql';
|
|
12
|
+
|
|
13
|
+
export type Selections = ReadonlyArray<SelectionNode>;
|
|
14
|
+
|
|
15
|
+
export type GenerateConfig = {
|
|
16
|
+
schemaFilePath: string
|
|
17
|
+
match?: ReadonlyArray<RegExp | string>
|
|
18
|
+
exclude?: ReadonlyArray<RegExp | string>
|
|
19
|
+
typeScript?: boolean
|
|
20
|
+
scalars?: Scalars
|
|
21
|
+
strictNullability?: boolean
|
|
22
|
+
/**
|
|
23
|
+
* The command that users should run to regenerate the types files.
|
|
24
|
+
*/
|
|
25
|
+
regenerateCommand?: string
|
|
26
|
+
readOnlyArray?: boolean
|
|
27
|
+
splitTypes?: boolean
|
|
28
|
+
generatedDirectory?: string
|
|
29
|
+
exportAllObjectTypes?: boolean
|
|
30
|
+
typeFileName?: string
|
|
31
|
+
experimentalEnums?: boolean
|
|
32
|
+
omitFileExtensions?: boolean
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
export type CrawlConfig = {
|
|
36
|
+
root: string
|
|
37
|
+
pragma?: string
|
|
38
|
+
loosePragma?: string
|
|
39
|
+
ignorePragma?: string
|
|
40
|
+
dumpOperations?: string
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
export type Config = {
|
|
44
|
+
crawl: CrawlConfig
|
|
45
|
+
generate: GenerateConfig | Array<GenerateConfig>
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
export type Schema = {
|
|
49
|
+
interfacesByName: {
|
|
50
|
+
[key: string]: IntrospectionInterfaceType & {
|
|
51
|
+
fieldsByName: {
|
|
52
|
+
[key: string]: IntrospectionField
|
|
53
|
+
}
|
|
54
|
+
possibleTypesByName: {
|
|
55
|
+
[key: string]: boolean
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
inputObjectsByName: {
|
|
60
|
+
[key: string]: IntrospectionInputObjectType
|
|
61
|
+
}
|
|
62
|
+
typesByName: {
|
|
63
|
+
[key: string]: IntrospectionObjectType & {
|
|
64
|
+
fieldsByName: {
|
|
65
|
+
[key: string]: IntrospectionField
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
unionsByName: {
|
|
70
|
+
[key: string]: IntrospectionUnionType
|
|
71
|
+
}
|
|
72
|
+
enumsByName: {
|
|
73
|
+
[key: string]: IntrospectionEnumType
|
|
74
|
+
}
|
|
75
|
+
};
|
|
76
|
+
|
|
77
|
+
export type Context = {
|
|
78
|
+
path: Array<string>
|
|
79
|
+
strictNullability: boolean
|
|
80
|
+
readOnlyArray: boolean
|
|
81
|
+
fragments: {
|
|
82
|
+
[key: string]: FragmentDefinitionNode
|
|
83
|
+
}
|
|
84
|
+
schema: Schema
|
|
85
|
+
scalars: Scalars
|
|
86
|
+
errors: Array<string>
|
|
87
|
+
allObjectTypes: null | {
|
|
88
|
+
[key: string]: Node
|
|
89
|
+
}
|
|
90
|
+
typeScript: boolean
|
|
91
|
+
experimentalEnumsMap?: {
|
|
92
|
+
[key: string]: Node
|
|
93
|
+
} // index signature that is populated with declarations
|
|
94
|
+
};
|
|
95
|
+
export type Scalars = {
|
|
96
|
+
[key: string]: 'string' | 'number' | 'boolean'
|
|
97
|
+
};
|
package/src/utils.ts
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import * as babelTypes from '@babel/types';
|
|
2
|
+
import {TSPropertySignature} from '@babel/types';
|
|
3
|
+
|
|
4
|
+
export const liftLeadingPropertyComments = (property: TSPropertySignature): TSPropertySignature => {
|
|
5
|
+
return transferLeadingComments(property.typeAnnotation!, property);
|
|
6
|
+
};
|
|
7
|
+
|
|
8
|
+
export const maybeAddDescriptionComment = <T extends babelTypes.Node>(description: string | null | undefined, node: T): T => {
|
|
9
|
+
if (description) {
|
|
10
|
+
addCommentAsLineComments(description, node);
|
|
11
|
+
}
|
|
12
|
+
return node;
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
export function addCommentAsLineComments(
|
|
16
|
+
description: string,
|
|
17
|
+
res: babelTypes.Node,
|
|
18
|
+
) {
|
|
19
|
+
if (res.leadingComments?.length) {
|
|
20
|
+
res.leadingComments[0].value += '\n\n---\n\n' + description;
|
|
21
|
+
} else {
|
|
22
|
+
babelTypes.addComment(
|
|
23
|
+
res,
|
|
24
|
+
'leading',
|
|
25
|
+
'* ' + description,
|
|
26
|
+
false, // this specifies that it's a block comment, not a line comment
|
|
27
|
+
);
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export const transferLeadingComments = <T extends babelTypes.Node>(source: babelTypes.Node, dest: T): T => {
|
|
32
|
+
if (source.leadingComments?.length) {
|
|
33
|
+
dest.leadingComments = [
|
|
34
|
+
...(dest.leadingComments || []),
|
|
35
|
+
...source.leadingComments,
|
|
36
|
+
];
|
|
37
|
+
source.leadingComments = [];
|
|
38
|
+
}
|
|
39
|
+
return dest;
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
export function nullableType(type: babelTypes.TSType): babelTypes.TSType {
|
|
43
|
+
return babelTypes.tsUnionType([type, babelTypes.tsNullKeyword(), babelTypes.tsUndefinedKeyword()]);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export function isnNullableType(type: babelTypes.TSType): boolean {
|
|
47
|
+
let hasNull = false;
|
|
48
|
+
let hasUndefined = false;
|
|
49
|
+
if (type.type === 'TSUnionType') {
|
|
50
|
+
for (const t of type.types) {
|
|
51
|
+
if (t.type === 'TSNullKeyword') {
|
|
52
|
+
hasNull = true;
|
|
53
|
+
} else if (t.type === 'TSUndefinedKeyword') {
|
|
54
|
+
hasUndefined = true;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
return hasNull && hasUndefined;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
export function objectTypeFromProperties(properties: babelTypes.TSPropertySignature[]): babelTypes.TSTypeLiteral {
|
|
62
|
+
let exitingProperties: Record<string, boolean> = {};
|
|
63
|
+
let filteredProperties = properties.filter((p) => {
|
|
64
|
+
if (p.key.type === 'Identifier') {
|
|
65
|
+
if (exitingProperties[p.key.name]) {
|
|
66
|
+
return false;
|
|
67
|
+
}
|
|
68
|
+
exitingProperties[p.key.name] = true;
|
|
69
|
+
}
|
|
70
|
+
return true;
|
|
71
|
+
});
|
|
72
|
+
return babelTypes.tsTypeLiteral(filteredProperties);
|
|
73
|
+
}
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
// @flow
|
|
2
1
|
const path = require('path');
|
|
3
2
|
const {execSync} = require('child_process');
|
|
4
3
|
|
|
@@ -9,7 +8,7 @@ export const findRepoRoot = (): string => {
|
|
|
9
8
|
});
|
|
10
9
|
return res.trim();
|
|
11
10
|
// eslint-disable-next-line flowtype-errors/uncovered
|
|
12
|
-
} catch (err) {
|
|
11
|
+
} catch (err: any) {
|
|
13
12
|
throw new Error(
|
|
14
13
|
// eslint-disable-next-line flowtype-errors/uncovered
|
|
15
14
|
`Unable to use git rev-parse to find the repository root. ${err.message}`,
|
|
@@ -31,7 +30,7 @@ export const findGraphqlTagReferences = (root: string): Array<string> => {
|
|
|
31
30
|
.split('\n')
|
|
32
31
|
.map((relative) => path.join(root, relative));
|
|
33
32
|
// eslint-disable-next-line flowtype-errors/uncovered
|
|
34
|
-
} catch (err) {
|
|
33
|
+
} catch (err: any) {
|
|
35
34
|
throw new Error(
|
|
36
35
|
// eslint-disable-next-line flowtype-errors/uncovered
|
|
37
36
|
`Unable to use git grep to find files with gql tags. ${err.message}`,
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
{
|
|
2
|
+
"exclude": ["node_modules/**", "dist/**"],
|
|
3
|
+
"compilerOptions": {
|
|
4
|
+
/* Visit https://aka.ms/tsconfig to read more about this file */
|
|
5
|
+
|
|
6
|
+
/* Projects */
|
|
7
|
+
// "incremental": true, /* Save .tsbuildinfo files to allow for incremental compilation of projects. */
|
|
8
|
+
// "composite": true, /* Enable constraints that allow a TypeScript project to be used with project references. */
|
|
9
|
+
// "tsBuildInfoFile": "./.tsbuildinfo", /* Specify the path to .tsbuildinfo incremental compilation file. */
|
|
10
|
+
// "disableSourceOfProjectReferenceRedirect": true, /* Disable preferring source files instead of declaration files when referencing composite projects. */
|
|
11
|
+
// "disableSolutionSearching": true, /* Opt a project out of multi-project reference checking when editing. */
|
|
12
|
+
// "disableReferencedProjectLoad": true, /* Reduce the number of projects loaded automatically by TypeScript. */
|
|
13
|
+
|
|
14
|
+
/* Language and Environment */
|
|
15
|
+
"target": "es2018", /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */
|
|
16
|
+
// "lib": [], /* Specify a set of bundled library declaration files that describe the target runtime environment. */
|
|
17
|
+
// "jsx": "preserve", /* Specify what JSX code is generated. */
|
|
18
|
+
// "experimentalDecorators": true, /* Enable experimental support for legacy experimental decorators. */
|
|
19
|
+
// "emitDecoratorMetadata": true, /* Emit design-type metadata for decorated declarations in source files. */
|
|
20
|
+
// "jsxFactory": "", /* Specify the JSX factory function used when targeting React JSX emit, e.g. 'React.createElement' or 'h'. */
|
|
21
|
+
// "jsxFragmentFactory": "", /* Specify the JSX Fragment reference used for fragments when targeting React JSX emit e.g. 'React.Fragment' or 'Fragment'. */
|
|
22
|
+
// "jsxImportSource": "", /* Specify module specifier used to import the JSX factory functions when using 'jsx: react-jsx*'. */
|
|
23
|
+
// "reactNamespace": "", /* Specify the object invoked for 'createElement'. This only applies when targeting 'react' JSX emit. */
|
|
24
|
+
// "noLib": true, /* Disable including any library files, including the default lib.d.ts. */
|
|
25
|
+
// "useDefineForClassFields": true, /* Emit ECMAScript-standard-compliant class fields. */
|
|
26
|
+
// "moduleDetection": "auto", /* Control what method is used to detect module-format JS files. */
|
|
27
|
+
|
|
28
|
+
/* Modules */
|
|
29
|
+
"module": "es2015", /* Specify what module code is generated. */
|
|
30
|
+
// "rootDir": "./", /* Specify the root folder within your source files. */
|
|
31
|
+
"moduleResolution": "bundler", /* Specify how TypeScript looks up a file from a given module specifier. */
|
|
32
|
+
// "baseUrl": "./", /* Specify the base directory to resolve non-relative module names. */
|
|
33
|
+
// "paths": {}, /* Specify a set of entries that re-map imports to additional lookup locations. */
|
|
34
|
+
// "rootDirs": [], /* Allow multiple folders to be treated as one when resolving modules. */
|
|
35
|
+
// "typeRoots": [], /* Specify multiple folders that act like './node_modules/@types'. */
|
|
36
|
+
// "types": [], /* Specify type package names to be included without being referenced in a source file. */
|
|
37
|
+
// "allowUmdGlobalAccess": true, /* Allow accessing UMD globals from modules. */
|
|
38
|
+
// "moduleSuffixes": [], /* List of file name suffixes to search when resolving a module. */
|
|
39
|
+
// "allowImportingTsExtensions": true, /* Allow imports to include TypeScript file extensions. Requires '--moduleResolution bundler' and either '--noEmit' or '--emitDeclarationOnly' to be set. */
|
|
40
|
+
// "resolvePackageJsonExports": true, /* Use the package.json 'exports' field when resolving package imports. */
|
|
41
|
+
"resolvePackageJsonImports": true, /* Use the package.json 'imports' field when resolving imports. */
|
|
42
|
+
// "customConditions": [], /* Conditions to set in addition to the resolver-specific defaults when resolving imports. */
|
|
43
|
+
// "resolveJsonModule": true, /* Enable importing .json files. */
|
|
44
|
+
// "allowArbitraryExtensions": true, /* Enable importing files with any extension, provided a declaration file is present. */
|
|
45
|
+
// "noResolve": true, /* Disallow 'import's, 'require's or '<reference>'s from expanding the number of files TypeScript should add to a project. */
|
|
46
|
+
|
|
47
|
+
/* JavaScript Support */
|
|
48
|
+
// "allowJs": true, /* Allow JavaScript files to be a part of your program. Use the 'checkJS' option to get errors from these files. */
|
|
49
|
+
// "checkJs": true, /* Enable error reporting in type-checked JavaScript files. */
|
|
50
|
+
// "maxNodeModuleJsDepth": 1, /* Specify the maximum folder depth used for checking JavaScript files from 'node_modules'. Only applicable with 'allowJs'. */
|
|
51
|
+
|
|
52
|
+
/* Emit */
|
|
53
|
+
// "declaration": true, /* Generate .d.ts files from TypeScript and JavaScript files in your project. */
|
|
54
|
+
// "declarationMap": true, /* Create sourcemaps for d.ts files. */
|
|
55
|
+
// "emitDeclarationOnly": true, /* Only output d.ts files and not JavaScript files. */
|
|
56
|
+
// "sourceMap": true, /* Create source map files for emitted JavaScript files. */
|
|
57
|
+
// "inlineSourceMap": true, /* Include sourcemap files inside the emitted JavaScript. */
|
|
58
|
+
// "outFile": "./", /* Specify a file that bundles all outputs into one JavaScript file. If 'declaration' is true, also designates a file that bundles all .d.ts output. */
|
|
59
|
+
// "outDir": "./", /* Specify an output folder for all emitted files. */
|
|
60
|
+
// "removeComments": true, /* Disable emitting comments. */
|
|
61
|
+
"noEmit": true, /* Disable emitting files from a compilation. */
|
|
62
|
+
// "importHelpers": true, /* Allow importing helper functions from tslib once per project, instead of including them per-file. */
|
|
63
|
+
// "importsNotUsedAsValues": "remove", /* Specify emit/checking behavior for imports that are only used for types. */
|
|
64
|
+
// "downlevelIteration": true, /* Emit more compliant, but verbose and less performant JavaScript for iteration. */
|
|
65
|
+
// "sourceRoot": "", /* Specify the root path for debuggers to find the reference source code. */
|
|
66
|
+
// "mapRoot": "", /* Specify the location where debugger should locate map files instead of generated locations. */
|
|
67
|
+
// "inlineSources": true, /* Include source code in the sourcemaps inside the emitted JavaScript. */
|
|
68
|
+
// "emitBOM": true, /* Emit a UTF-8 Byte Order Mark (BOM) in the beginning of output files. */
|
|
69
|
+
// "newLine": "crlf", /* Set the newline character for emitting files. */
|
|
70
|
+
// "stripInternal": true, /* Disable emitting declarations that have '@internal' in their JSDoc comments. */
|
|
71
|
+
// "noEmitHelpers": true, /* Disable generating custom helper functions like '__extends' in compiled output. */
|
|
72
|
+
// "noEmitOnError": true, /* Disable emitting files if any type checking errors are reported. */
|
|
73
|
+
// "preserveConstEnums": true, /* Disable erasing 'const enum' declarations in generated code. */
|
|
74
|
+
// "declarationDir": "./", /* Specify the output directory for generated declaration files. */
|
|
75
|
+
// "preserveValueImports": true, /* Preserve unused imported values in the JavaScript output that would otherwise be removed. */
|
|
76
|
+
|
|
77
|
+
/* Interop Constraints */
|
|
78
|
+
// "isolatedModules": true, /* Ensure that each file can be safely transpiled without relying on other imports. */
|
|
79
|
+
// "verbatimModuleSyntax": true, /* Do not transform or elide any imports or exports not marked as type-only, ensuring they are written in the output file's format based on the 'module' setting. */
|
|
80
|
+
// "allowSyntheticDefaultImports": true, /* Allow 'import x from y' when a module doesn't have a default export. */
|
|
81
|
+
"esModuleInterop": true, /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables 'allowSyntheticDefaultImports' for type compatibility. */
|
|
82
|
+
// "preserveSymlinks": true, /* Disable resolving symlinks to their realpath. This correlates to the same flag in node. */
|
|
83
|
+
"forceConsistentCasingInFileNames": true, /* Ensure that casing is correct in imports. */
|
|
84
|
+
|
|
85
|
+
/* Type Checking */
|
|
86
|
+
"strict": true, /* Enable all strict type-checking options. */
|
|
87
|
+
// "noImplicitAny": true, /* Enable error reporting for expressions and declarations with an implied 'any' type. */
|
|
88
|
+
// "strictNullChecks": true, /* When type checking, take into account 'null' and 'undefined'. */
|
|
89
|
+
// "strictFunctionTypes": true, /* When assigning functions, check to ensure parameters and the return values are subtype-compatible. */
|
|
90
|
+
// "strictBindCallApply": true, /* Check that the arguments for 'bind', 'call', and 'apply' methods match the original function. */
|
|
91
|
+
// "strictPropertyInitialization": true, /* Check for class properties that are declared but not set in the constructor. */
|
|
92
|
+
// "noImplicitThis": true, /* Enable error reporting when 'this' is given the type 'any'. */
|
|
93
|
+
// "useUnknownInCatchVariables": true, /* Default catch clause variables as 'unknown' instead of 'any'. */
|
|
94
|
+
// "alwaysStrict": true, /* Ensure 'use strict' is always emitted. */
|
|
95
|
+
// "noUnusedLocals": true, /* Enable error reporting when local variables aren't read. */
|
|
96
|
+
// "noUnusedParameters": true, /* Raise an error when a function parameter isn't read. */
|
|
97
|
+
// "exactOptionalPropertyTypes": true, /* Interpret optional property types as written, rather than adding 'undefined'. */
|
|
98
|
+
// "noImplicitReturns": true, /* Enable error reporting for codepaths that do not explicitly return in a function. */
|
|
99
|
+
// "noFallthroughCasesInSwitch": true, /* Enable error reporting for fallthrough cases in switch statements. */
|
|
100
|
+
// "noUncheckedIndexedAccess": true, /* Add 'undefined' to a type when accessed using an index. */
|
|
101
|
+
// "noImplicitOverride": true, /* Ensure overriding members in derived classes are marked with an override modifier. */
|
|
102
|
+
// "noPropertyAccessFromIndexSignature": true, /* Enforces using indexed accessors for keys declared using an indexed type. */
|
|
103
|
+
// "allowUnusedLabels": true, /* Disable error reporting for unused labels. */
|
|
104
|
+
// "allowUnreachableCode": true, /* Disable error reporting for unreachable code. */
|
|
105
|
+
|
|
106
|
+
/* Completeness */
|
|
107
|
+
// "skipDefaultLibCheck": true, /* Skip type checking .d.ts files that are included with TypeScript. */
|
|
108
|
+
"skipLibCheck": true /* Skip type checking all .d.ts files. */
|
|
109
|
+
}
|
|
110
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
declare module '@khanacademy/flow-to-ts/dist/convert.bundle';
|