@khanacademy/graphql-flow 3.1.1 → 3.1.3
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/CHANGELOG.md +12 -0
- package/dist/cli/config.js +3 -24
- package/dist/cli/run.js +17 -44
- package/dist/enums.js +6 -22
- package/dist/generateResponseType.js +16 -89
- package/dist/generateTypeFiles.js +21 -48
- package/dist/generateVariablesType.js +7 -42
- package/dist/index.js +20 -23
- package/dist/parser/parse.js +42 -69
- package/dist/parser/resolve.js +5 -26
- package/dist/parser/utils.js +1 -14
- package/dist/schemaFromIntrospectionData.js +5 -13
- package/dist/utils.js +2 -23
- package/package.json +13 -7
- package/src/__test__/example-schema.graphql +1 -0
- package/src/__test__/generateTypeFileContents.test.ts +4 -10
- package/src/__test__/graphql-flow.test.ts +35 -77
- package/src/cli/run.ts +12 -1
- package/src/generateResponseType.ts +2 -2
- package/src/generateTypeFiles.ts +18 -15
- package/src/generateVariablesType.ts +5 -3
- package/src/index.ts +1 -1
- package/src/parser/parse.ts +8 -3
package/dist/index.js
CHANGED
|
@@ -4,20 +4,22 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
6
|
exports.documentToFlowTypes = exports.FlowGenerationError = void 0;
|
|
7
|
-
|
|
8
7
|
var _wonderStuffCore = require("@khanacademy/wonder-stuff-core");
|
|
9
|
-
|
|
10
8
|
var _generator = _interopRequireDefault(require("@babel/generator"));
|
|
11
|
-
|
|
12
9
|
var _generateResponseType = require("./generateResponseType");
|
|
13
|
-
|
|
14
10
|
var _generateVariablesType = require("./generateVariablesType");
|
|
15
|
-
|
|
16
|
-
|
|
11
|
+
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
|
|
12
|
+
/* eslint-disable no-console */
|
|
13
|
+
/* flow-uncovered-file */
|
|
14
|
+
/**
|
|
15
|
+
* This tool generates flowtype definitions from graphql queries.
|
|
16
|
+
*
|
|
17
|
+
* It relies on `introspection-query.json` existing in this directory,
|
|
18
|
+
* which is produced by running `./tools/graphql-flow/sendIntrospection.js`.
|
|
19
|
+
*/
|
|
17
20
|
|
|
18
21
|
const optionsToConfig = (schema, definitions, options, errors = []) => {
|
|
19
22
|
var _options$strictNullab, _options$readOnlyArra, _options$scalars, _options$typeScript, _options$omitFileExte;
|
|
20
|
-
|
|
21
23
|
const internalOptions = {
|
|
22
24
|
strictNullability: (_options$strictNullab = options === null || options === void 0 ? void 0 : options.strictNullability) !== null && _options$strictNullab !== void 0 ? _options$strictNullab : true,
|
|
23
25
|
readOnlyArray: (_options$readOnlyArra = options === null || options === void 0 ? void 0 : options.readOnlyArray) !== null && _options$readOnlyArra !== void 0 ? _options$readOnlyArra : true,
|
|
@@ -39,22 +41,19 @@ const optionsToConfig = (schema, definitions, options, errors = []) => {
|
|
|
39
41
|
path: [],
|
|
40
42
|
experimentalEnumsMap: options !== null && options !== void 0 && options.experimentalEnums ? {} : undefined,
|
|
41
43
|
...internalOptions
|
|
42
|
-
};
|
|
43
|
-
// assigned to the mutable type 'string[]'.
|
|
44
|
+
};
|
|
44
45
|
|
|
46
|
+
// @ts-expect-error: TS2322 - The type 'readonly []' is 'readonly' and cannot be
|
|
47
|
+
// assigned to the mutable type 'string[]'.
|
|
45
48
|
return config;
|
|
46
49
|
};
|
|
47
|
-
|
|
48
50
|
class FlowGenerationError extends Error {
|
|
49
51
|
constructor(errors) {
|
|
50
52
|
super(`Graphql-flow type generation failed! ${errors.join("; ")}`);
|
|
51
53
|
this.messages = errors;
|
|
52
54
|
}
|
|
53
|
-
|
|
54
55
|
}
|
|
55
|
-
|
|
56
56
|
exports.FlowGenerationError = FlowGenerationError;
|
|
57
|
-
|
|
58
57
|
const documentToFlowTypes = (document, schema, options) => {
|
|
59
58
|
const errors = [];
|
|
60
59
|
const config = optionsToConfig(schema, document.definitions, options, errors);
|
|
@@ -62,7 +61,8 @@ const documentToFlowTypes = (document, schema, options) => {
|
|
|
62
61
|
if (item.kind === "FragmentDefinition") {
|
|
63
62
|
const name = item.name.value;
|
|
64
63
|
const types = {};
|
|
65
|
-
const code = `export type ${name} = ${(0, _generateResponseType.generateFragmentType)(schema, item, {
|
|
64
|
+
const code = `export type ${name} = ${(0, _generateResponseType.generateFragmentType)(schema, item, {
|
|
65
|
+
...config,
|
|
66
66
|
path: [name],
|
|
67
67
|
allObjectTypes: options !== null && options !== void 0 && options.exportAllObjectTypes ? types : null
|
|
68
68
|
})};`;
|
|
@@ -77,20 +77,21 @@ const documentToFlowTypes = (document, schema, options) => {
|
|
|
77
77
|
experimentalEnums
|
|
78
78
|
};
|
|
79
79
|
}
|
|
80
|
-
|
|
81
80
|
if (item.kind === "OperationDefinition" && (item.operation === "query" || item.operation === "mutation") && item.name) {
|
|
82
81
|
const types = {};
|
|
83
82
|
const name = item.name.value;
|
|
84
|
-
const response = (0, _generateResponseType.generateResponseType)(schema, item, {
|
|
83
|
+
const response = (0, _generateResponseType.generateResponseType)(schema, item, {
|
|
84
|
+
...config,
|
|
85
85
|
path: [name],
|
|
86
86
|
allObjectTypes: options !== null && options !== void 0 && options.exportAllObjectTypes ? types : null
|
|
87
87
|
});
|
|
88
|
-
const variables = (0, _generateVariablesType.generateVariablesType)(schema, item, {
|
|
88
|
+
const variables = (0, _generateVariablesType.generateVariablesType)(schema, item, {
|
|
89
|
+
...config,
|
|
89
90
|
path: [name]
|
|
90
91
|
});
|
|
91
|
-
const typeName = `${name}Type`;
|
|
92
|
+
const typeName = `${name}Type`;
|
|
93
|
+
// TODO(jared): Maybe make this template configurable?
|
|
92
94
|
// We'll see what's required to get webapp on board.
|
|
93
|
-
|
|
94
95
|
const code = `export type ${typeName} = {\n variables: ${variables},\n response: ${response}\n};`;
|
|
95
96
|
const extraTypes = codegenExtraTypes(types);
|
|
96
97
|
const experimentalEnums = codegenExtraTypes(config.experimentalEnumsMap || {});
|
|
@@ -103,16 +104,12 @@ const documentToFlowTypes = (document, schema, options) => {
|
|
|
103
104
|
};
|
|
104
105
|
}
|
|
105
106
|
}).filter(_wonderStuffCore.isTruthy);
|
|
106
|
-
|
|
107
107
|
if (errors.length) {
|
|
108
108
|
throw new FlowGenerationError(errors);
|
|
109
109
|
}
|
|
110
|
-
|
|
111
110
|
return result;
|
|
112
111
|
};
|
|
113
|
-
|
|
114
112
|
exports.documentToFlowTypes = documentToFlowTypes;
|
|
115
|
-
|
|
116
113
|
function codegenExtraTypes(types) {
|
|
117
114
|
const extraTypes = {};
|
|
118
115
|
Object.keys(types).forEach(k => {
|
package/dist/parser/parse.js
CHANGED
|
@@ -4,18 +4,40 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
6
|
exports.processFiles = exports.processFile = void 0;
|
|
7
|
-
|
|
8
7
|
var _wonderStuffCore = require("@khanacademy/wonder-stuff-core");
|
|
9
|
-
|
|
10
8
|
var _parser = require("@babel/parser");
|
|
11
|
-
|
|
12
9
|
var _traverse = _interopRequireDefault(require("@babel/traverse"));
|
|
13
|
-
|
|
14
10
|
var _path = _interopRequireDefault(require("path"));
|
|
15
|
-
|
|
16
11
|
var _utils = require("./utils");
|
|
17
|
-
|
|
18
|
-
|
|
12
|
+
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
|
|
13
|
+
/**
|
|
14
|
+
* This file is responsible for finding all gql`-annotated
|
|
15
|
+
* template strings in a set of provided files, and for resolving
|
|
16
|
+
* all fragment references to their eventual sources.
|
|
17
|
+
*
|
|
18
|
+
* Things that are supported:
|
|
19
|
+
* - importing fragments from other files
|
|
20
|
+
* - re-exporting fragments that were imported
|
|
21
|
+
* - using locally-defined fragments, even if they're
|
|
22
|
+
* not at the top level (scope is honored correctly)
|
|
23
|
+
* - importing the gql tag as some other name
|
|
24
|
+
* (e.g. 'import blah from "graphql-tag"')
|
|
25
|
+
* - renaming fragments at the top level
|
|
26
|
+
*
|
|
27
|
+
* Things that are *not* supported:
|
|
28
|
+
* - doing anything other than 'const x = gql`my template`'
|
|
29
|
+
* e.g. const x = someCond ? one fragment literal : another fragment literal
|
|
30
|
+
* or const x = someFragmentPreprocessor(fragment literal)
|
|
31
|
+
* - importing the graphql tag fn from anywhere other than "graphql-tag"
|
|
32
|
+
* - anything else fancy with the graphql tag fn, e.g. 'const blah = gql; blah`xyz`'
|
|
33
|
+
* - including a fragment in an operation by anything other than a bare identifier,
|
|
34
|
+
* e.g. 'const myQuery = gql`query xyz {...} ${cond ? someFrag : otherFrag}`.
|
|
35
|
+
* - getting fragments from e.g. function arguments, or renaming non-toplevel fragments
|
|
36
|
+
*
|
|
37
|
+
* Things that could be supported, but are not yet:
|
|
38
|
+
* - tracking whether a given graphql operation has already been wrapped
|
|
39
|
+
* in `gqlOp<Type>()` or not (to inform an auto-wrapper of the future)
|
|
40
|
+
*/
|
|
19
41
|
|
|
20
42
|
/**
|
|
21
43
|
* Finds all referenced imports that might possibly be relevant
|
|
@@ -27,12 +49,10 @@ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { de
|
|
|
27
49
|
*/
|
|
28
50
|
const listExternalReferences = (file, config) => {
|
|
29
51
|
const paths = {};
|
|
30
|
-
|
|
31
52
|
const add = (v, followImports) => {
|
|
32
53
|
if (v.type === "import") {
|
|
33
54
|
if (followImports) {
|
|
34
55
|
const absPath = (0, _utils.getPathWithExtension)(v.path, config);
|
|
35
|
-
|
|
36
56
|
if (absPath) {
|
|
37
57
|
paths[absPath] = true;
|
|
38
58
|
}
|
|
@@ -41,20 +61,20 @@ const listExternalReferences = (file, config) => {
|
|
|
41
61
|
v.source.expressions.forEach(expr => add(expr, true));
|
|
42
62
|
}
|
|
43
63
|
};
|
|
44
|
-
|
|
45
|
-
|
|
64
|
+
Object.keys(file.exports).forEach(k => add(file.exports[k],
|
|
65
|
+
// If we're re-exporting something, we need to follow that import.
|
|
46
66
|
true));
|
|
47
|
-
Object.keys(file.locals).forEach(k => add(file.locals[k],
|
|
67
|
+
Object.keys(file.locals).forEach(k => add(file.locals[k],
|
|
68
|
+
// If we've imported something but haven't used it or exported it,
|
|
48
69
|
// we don't need to follow the import.
|
|
49
70
|
false));
|
|
50
|
-
file.operations.forEach(op => op.source.expressions.forEach(expr => add(expr,
|
|
71
|
+
file.operations.forEach(op => op.source.expressions.forEach(expr => add(expr,
|
|
72
|
+
// Imports that are used in graphql expressions definitely need to be followed.
|
|
51
73
|
true)));
|
|
52
74
|
return Object.keys(paths);
|
|
53
75
|
};
|
|
54
|
-
|
|
55
76
|
const processFile = (filePath, contents, config) => {
|
|
56
77
|
const dir = _path.default.dirname(filePath);
|
|
57
|
-
|
|
58
78
|
const result = {
|
|
59
79
|
path: filePath,
|
|
60
80
|
operations: [],
|
|
@@ -73,35 +93,28 @@ const processFile = (filePath, contents, config) => {
|
|
|
73
93
|
const seenTemplates = {};
|
|
74
94
|
ast.program.body.forEach(toplevel => {
|
|
75
95
|
var _toplevel$declaration;
|
|
76
|
-
|
|
77
96
|
if (toplevel.type === "ImportDeclaration") {
|
|
78
97
|
const newLocals = getLocals(dir, toplevel, filePath, config);
|
|
79
|
-
|
|
80
98
|
if (newLocals) {
|
|
81
99
|
Object.keys(newLocals).forEach(k => {
|
|
82
100
|
const local = newLocals[k];
|
|
83
|
-
|
|
84
101
|
if (local.path.startsWith("/")) {
|
|
85
102
|
result.locals[k] = local;
|
|
86
103
|
}
|
|
87
|
-
|
|
88
104
|
if (local.path === "graphql-tag" && local.name === "default") {
|
|
89
105
|
gqlTagNames.push(k);
|
|
90
106
|
}
|
|
91
107
|
});
|
|
92
108
|
}
|
|
93
109
|
}
|
|
94
|
-
|
|
95
110
|
if (toplevel.type === "ExportNamedDeclaration") {
|
|
96
111
|
if (toplevel.source) {
|
|
97
112
|
var _toplevel$specifiers;
|
|
98
|
-
|
|
99
113
|
const source = toplevel.source;
|
|
100
114
|
const importPath = source.value.startsWith(".") ? _path.default.resolve(_path.default.join(dir, source.value)) : source.value;
|
|
101
|
-
(_toplevel$specifiers = toplevel.specifiers) === null || _toplevel$specifiers === void 0
|
|
115
|
+
(_toplevel$specifiers = toplevel.specifiers) === null || _toplevel$specifiers === void 0 || _toplevel$specifiers.forEach(spec => {
|
|
102
116
|
if (spec.type === "ExportSpecifier" && spec.exported.type === "Identifier") {
|
|
103
117
|
var _spec$start, _spec$end, _spec$loc$start$line, _spec$loc;
|
|
104
|
-
|
|
105
118
|
result.exports[spec.exported.name] = {
|
|
106
119
|
type: "import",
|
|
107
120
|
name: spec.local.name,
|
|
@@ -117,11 +130,9 @@ const processFile = (filePath, contents, config) => {
|
|
|
117
130
|
});
|
|
118
131
|
} else {
|
|
119
132
|
var _toplevel$specifiers2;
|
|
120
|
-
|
|
121
|
-
(_toplevel$specifiers2 = toplevel.specifiers) === null || _toplevel$specifiers2 === void 0 ? void 0 : _toplevel$specifiers2.forEach(spec => {
|
|
133
|
+
(_toplevel$specifiers2 = toplevel.specifiers) === null || _toplevel$specifiers2 === void 0 || _toplevel$specifiers2.forEach(spec => {
|
|
122
134
|
if (spec.type === "ExportSpecifier") {
|
|
123
135
|
const local = result.locals[spec.local.name];
|
|
124
|
-
|
|
125
136
|
if (local && spec.exported.type === "Identifier") {
|
|
126
137
|
result.exports[spec.exported.name] = local;
|
|
127
138
|
}
|
|
@@ -129,79 +140,62 @@ const processFile = (filePath, contents, config) => {
|
|
|
129
140
|
});
|
|
130
141
|
}
|
|
131
142
|
}
|
|
132
|
-
|
|
133
143
|
const processDeclarator = (decl, isExported) => {
|
|
134
144
|
if (decl.id.type !== "Identifier" || !decl.init) {
|
|
135
145
|
return;
|
|
136
146
|
}
|
|
137
|
-
|
|
138
147
|
const {
|
|
139
148
|
init
|
|
140
149
|
} = decl;
|
|
141
150
|
const id = decl.id.name;
|
|
142
|
-
|
|
143
151
|
if (init.type === "TaggedTemplateExpression" && init.tag.type === "Identifier") {
|
|
144
152
|
if (gqlTagNames.includes(init.tag.name)) {
|
|
145
153
|
const tpl = processTemplate(init, result);
|
|
146
|
-
|
|
147
154
|
if (tpl) {
|
|
148
155
|
var _init$start;
|
|
149
|
-
|
|
150
156
|
const document = result.locals[id] = {
|
|
151
157
|
type: "document",
|
|
152
158
|
source: tpl
|
|
153
159
|
};
|
|
154
160
|
seenTemplates[(_init$start = init.start) !== null && _init$start !== void 0 ? _init$start : -1] = document;
|
|
155
|
-
|
|
156
161
|
if (isExported) {
|
|
157
162
|
result.exports[id] = document;
|
|
158
163
|
}
|
|
159
164
|
} else {
|
|
160
165
|
var _init$start2;
|
|
161
|
-
|
|
162
166
|
seenTemplates[(_init$start2 = init.start) !== null && _init$start2 !== void 0 ? _init$start2 : -1] = false;
|
|
163
167
|
}
|
|
164
168
|
}
|
|
165
169
|
}
|
|
166
|
-
|
|
167
170
|
if (init.type === "Identifier" && result.locals[init.name]) {
|
|
168
171
|
result.locals[id] = result.locals[init.name];
|
|
169
|
-
|
|
170
172
|
if (isExported) {
|
|
171
173
|
result.exports[id] = result.locals[init.name];
|
|
172
174
|
}
|
|
173
175
|
}
|
|
174
176
|
};
|
|
175
|
-
|
|
176
177
|
if (toplevel.type === "VariableDeclaration") {
|
|
177
178
|
toplevel.declarations.forEach(decl => {
|
|
178
179
|
processDeclarator(decl, false);
|
|
179
180
|
});
|
|
180
181
|
}
|
|
181
|
-
|
|
182
182
|
if (toplevel.type === "ExportNamedDeclaration" && ((_toplevel$declaration = toplevel.declaration) === null || _toplevel$declaration === void 0 ? void 0 : _toplevel$declaration.type) === "VariableDeclaration") {
|
|
183
183
|
toplevel.declaration.declarations.forEach(decl => {
|
|
184
184
|
processDeclarator(decl, true);
|
|
185
185
|
});
|
|
186
186
|
}
|
|
187
187
|
});
|
|
188
|
-
|
|
189
188
|
const visitTpl = (node, getBinding) => {
|
|
190
189
|
var _node$start;
|
|
191
|
-
|
|
192
190
|
if (seenTemplates[(_node$start = node.start) !== null && _node$start !== void 0 ? _node$start : -1] != null) {
|
|
193
191
|
return;
|
|
194
192
|
}
|
|
195
|
-
|
|
196
193
|
if (node.tag.type !== "Identifier" || !gqlTagNames.includes(node.tag.name)) {
|
|
197
194
|
return;
|
|
198
195
|
}
|
|
199
|
-
|
|
200
196
|
const tpl = processTemplate(node, result, getBinding);
|
|
201
|
-
|
|
202
197
|
if (tpl) {
|
|
203
198
|
var _node$start2;
|
|
204
|
-
|
|
205
199
|
seenTemplates[(_node$start2 = node.start) !== null && _node$start2 !== void 0 ? _node$start2 : -1] = {
|
|
206
200
|
type: "document",
|
|
207
201
|
source: tpl
|
|
@@ -211,51 +205,39 @@ const processFile = (filePath, contents, config) => {
|
|
|
211
205
|
});
|
|
212
206
|
} else {
|
|
213
207
|
var _node$start3;
|
|
214
|
-
|
|
215
208
|
seenTemplates[(_node$start3 = node.start) !== null && _node$start3 !== void 0 ? _node$start3 : -1] = false;
|
|
216
209
|
}
|
|
217
210
|
};
|
|
218
|
-
|
|
219
211
|
(0, _traverse.default)(ast, {
|
|
220
212
|
TaggedTemplateExpression(path) {
|
|
221
213
|
visitTpl(path.node, name => {
|
|
222
214
|
const binding = path.scope.getBinding(name);
|
|
223
|
-
|
|
224
215
|
if (!binding) {
|
|
225
216
|
return null;
|
|
226
217
|
}
|
|
227
|
-
|
|
228
218
|
const start = "init" in binding.path.node && binding.path.node.init ? binding.path.node.init.start : null;
|
|
229
|
-
|
|
230
219
|
if (start && seenTemplates[start]) {
|
|
231
220
|
return seenTemplates[start] || null;
|
|
232
221
|
}
|
|
233
|
-
|
|
234
222
|
return null;
|
|
235
223
|
});
|
|
236
224
|
}
|
|
237
|
-
|
|
238
225
|
});
|
|
239
226
|
return result;
|
|
240
227
|
};
|
|
241
|
-
|
|
242
228
|
exports.processFile = processFile;
|
|
243
|
-
|
|
244
229
|
const processTemplate = (tpl, result, getTemplate) => {
|
|
245
230
|
var _tpl$loc$start$line, _tpl$loc, _tpl$start, _tpl$end;
|
|
246
|
-
|
|
247
231
|
// 'cooked' is the string as runtime javascript will see it.
|
|
248
232
|
const literals = tpl.quasi.quasis.map(q => q.value.cooked || "");
|
|
249
233
|
const expressions = tpl.quasi.expressions.map(expr => {
|
|
250
234
|
var _expr$start, _expr$end, _expr$loc$start$line, _expr$loc;
|
|
251
|
-
|
|
252
235
|
const loc = {
|
|
253
236
|
start: (_expr$start = expr.start) !== null && _expr$start !== void 0 ? _expr$start : -1,
|
|
254
237
|
end: (_expr$end = expr.end) !== null && _expr$end !== void 0 ? _expr$end : -1,
|
|
255
238
|
line: (_expr$loc$start$line = (_expr$loc = expr.loc) === null || _expr$loc === void 0 ? void 0 : _expr$loc.start.line) !== null && _expr$loc$start$line !== void 0 ? _expr$loc$start$line : -1,
|
|
256
239
|
path: result.path
|
|
257
240
|
};
|
|
258
|
-
|
|
259
241
|
if (expr.type !== "Identifier") {
|
|
260
242
|
result.errors.push({
|
|
261
243
|
loc,
|
|
@@ -263,28 +245,23 @@ const processTemplate = (tpl, result, getTemplate) => {
|
|
|
263
245
|
});
|
|
264
246
|
return null;
|
|
265
247
|
}
|
|
266
|
-
|
|
267
248
|
if (!result.locals[expr.name]) {
|
|
268
249
|
if (getTemplate) {
|
|
269
250
|
const found = getTemplate(expr.name);
|
|
270
251
|
return found;
|
|
271
252
|
}
|
|
272
|
-
|
|
273
253
|
result.errors.push({
|
|
274
254
|
loc,
|
|
275
255
|
message: `Unable to resolve ${expr.name}`
|
|
276
256
|
});
|
|
277
257
|
return null;
|
|
278
258
|
}
|
|
279
|
-
|
|
280
259
|
return result.locals[expr.name];
|
|
281
260
|
});
|
|
282
|
-
|
|
283
261
|
if (expressions.includes(null)) {
|
|
284
262
|
// bail, stop processing.
|
|
285
263
|
return;
|
|
286
264
|
}
|
|
287
|
-
|
|
288
265
|
return {
|
|
289
266
|
literals,
|
|
290
267
|
expressions: expressions.filter(_wonderStuffCore.isTruthy),
|
|
@@ -296,12 +273,10 @@ const processTemplate = (tpl, result, getTemplate) => {
|
|
|
296
273
|
}
|
|
297
274
|
};
|
|
298
275
|
};
|
|
299
|
-
|
|
300
276
|
const getLocals = (dir, toplevel, myPath, config) => {
|
|
301
277
|
if (toplevel.importKind === "type") {
|
|
302
278
|
return null;
|
|
303
279
|
}
|
|
304
|
-
|
|
305
280
|
const fixedPath = (0, _utils.fixPathResolution)(toplevel.source.value, config);
|
|
306
281
|
const importPath = fixedPath.startsWith(".") ? _path.default.resolve(_path.default.join(dir, fixedPath)) : fixedPath;
|
|
307
282
|
const locals = {};
|
|
@@ -332,19 +307,19 @@ const getLocals = (dir, toplevel, myPath, config) => {
|
|
|
332
307
|
});
|
|
333
308
|
return locals;
|
|
334
309
|
};
|
|
335
|
-
|
|
336
310
|
const processFiles = (filePaths, config, getFileSource) => {
|
|
337
311
|
const files = {};
|
|
338
312
|
const toProcess = filePaths.slice();
|
|
339
|
-
|
|
340
313
|
while (toProcess.length) {
|
|
341
314
|
const next = toProcess.shift();
|
|
342
|
-
|
|
343
315
|
if (!next || files[next]) {
|
|
344
316
|
continue;
|
|
345
317
|
}
|
|
346
|
-
|
|
347
|
-
|
|
318
|
+
const source = getFileSource(next);
|
|
319
|
+
if (!source) {
|
|
320
|
+
continue;
|
|
321
|
+
}
|
|
322
|
+
const result = processFile(next, source, config);
|
|
348
323
|
files[next] = result;
|
|
349
324
|
listExternalReferences(result, config).forEach(path => {
|
|
350
325
|
if (!files[path] && !toProcess.includes(path)) {
|
|
@@ -352,8 +327,6 @@ const processFiles = (filePaths, config, getFileSource) => {
|
|
|
352
327
|
}
|
|
353
328
|
});
|
|
354
329
|
}
|
|
355
|
-
|
|
356
330
|
return files;
|
|
357
331
|
};
|
|
358
|
-
|
|
359
332
|
exports.processFiles = processFiles;
|
package/dist/parser/resolve.js
CHANGED
|
@@ -4,13 +4,9 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
6
|
exports.resolveDocuments = void 0;
|
|
7
|
-
|
|
8
7
|
var _graphqlTag = _interopRequireDefault(require("graphql-tag"));
|
|
9
|
-
|
|
10
8
|
var _utils = require("./utils");
|
|
11
|
-
|
|
12
|
-
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
13
|
-
|
|
9
|
+
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
|
|
14
10
|
const resolveDocuments = (files, config) => {
|
|
15
11
|
const resolved = {};
|
|
16
12
|
const errors = [];
|
|
@@ -21,7 +17,6 @@ const resolveDocuments = (files, config) => {
|
|
|
21
17
|
});
|
|
22
18
|
Object.keys(file.locals).forEach(k => {
|
|
23
19
|
const local = file.locals[k];
|
|
24
|
-
|
|
25
20
|
if (local.type === "document") {
|
|
26
21
|
resolveGqlTemplate(local.source, files, errors, resolved, {}, config);
|
|
27
22
|
}
|
|
@@ -32,16 +27,12 @@ const resolveDocuments = (files, config) => {
|
|
|
32
27
|
errors
|
|
33
28
|
};
|
|
34
29
|
};
|
|
35
|
-
|
|
36
30
|
exports.resolveDocuments = resolveDocuments;
|
|
37
|
-
|
|
38
31
|
const resolveImport = (expr, files, errors, seen, config) => {
|
|
39
32
|
const absPath = (0, _utils.getPathWithExtension)(expr.path, config);
|
|
40
|
-
|
|
41
33
|
if (!absPath) {
|
|
42
34
|
return null;
|
|
43
35
|
}
|
|
44
|
-
|
|
45
36
|
if (seen[absPath]) {
|
|
46
37
|
errors.push({
|
|
47
38
|
loc: expr.loc,
|
|
@@ -49,10 +40,8 @@ const resolveImport = (expr, files, errors, seen, config) => {
|
|
|
49
40
|
});
|
|
50
41
|
return null;
|
|
51
42
|
}
|
|
52
|
-
|
|
53
43
|
seen[absPath] = true;
|
|
54
44
|
const res = files[absPath];
|
|
55
|
-
|
|
56
45
|
if (!res) {
|
|
57
46
|
errors.push({
|
|
58
47
|
loc: expr.loc,
|
|
@@ -60,7 +49,6 @@ const resolveImport = (expr, files, errors, seen, config) => {
|
|
|
60
49
|
});
|
|
61
50
|
return null;
|
|
62
51
|
}
|
|
63
|
-
|
|
64
52
|
if (!res.exports[expr.name]) {
|
|
65
53
|
errors.push({
|
|
66
54
|
loc: expr.loc,
|
|
@@ -68,19 +56,14 @@ const resolveImport = (expr, files, errors, seen, config) => {
|
|
|
68
56
|
});
|
|
69
57
|
return null;
|
|
70
58
|
}
|
|
71
|
-
|
|
72
59
|
const value = res.exports[expr.name];
|
|
73
|
-
|
|
74
60
|
if (value.type === "import") {
|
|
75
61
|
return resolveImport(value, files, errors, seen, config);
|
|
76
62
|
}
|
|
77
|
-
|
|
78
63
|
return value;
|
|
79
64
|
};
|
|
80
|
-
|
|
81
65
|
const resolveGqlTemplate = (template, files, errors, resolved, seen, config) => {
|
|
82
66
|
const key = template.loc.path + ":" + template.loc.line;
|
|
83
|
-
|
|
84
67
|
if (seen[key]) {
|
|
85
68
|
errors.push({
|
|
86
69
|
loc: template.loc,
|
|
@@ -88,28 +71,24 @@ const resolveGqlTemplate = (template, files, errors, resolved, seen, config) =>
|
|
|
88
71
|
});
|
|
89
72
|
return null;
|
|
90
73
|
}
|
|
91
|
-
|
|
92
74
|
seen[key] = template;
|
|
93
|
-
|
|
94
75
|
if (resolved[key]) {
|
|
95
76
|
return resolved[key].document;
|
|
96
77
|
}
|
|
97
|
-
|
|
98
78
|
const expressions = template.expressions.map(expr => {
|
|
99
79
|
if (expr.type === "import") {
|
|
100
80
|
const document = resolveImport(expr, files, errors, {}, config);
|
|
101
|
-
return document ? resolveGqlTemplate(document.source, files, errors, resolved, {
|
|
81
|
+
return document ? resolveGqlTemplate(document.source, files, errors, resolved, {
|
|
82
|
+
...seen
|
|
102
83
|
}, config) : null;
|
|
103
84
|
}
|
|
104
|
-
|
|
105
|
-
|
|
85
|
+
return resolveGqlTemplate(expr.source, files, errors, resolved, {
|
|
86
|
+
...seen
|
|
106
87
|
}, config);
|
|
107
88
|
});
|
|
108
|
-
|
|
109
89
|
if (expressions.includes(null)) {
|
|
110
90
|
return null;
|
|
111
91
|
}
|
|
112
|
-
|
|
113
92
|
resolved[key] = {
|
|
114
93
|
document: (0, _graphqlTag.default)(template.literals, ...expressions),
|
|
115
94
|
raw: template
|
package/dist/parser/utils.js
CHANGED
|
@@ -4,11 +4,8 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
6
|
exports.getPathWithExtension = exports.fixPathResolution = void 0;
|
|
7
|
-
|
|
8
7
|
var _fs = _interopRequireDefault(require("fs"));
|
|
9
|
-
|
|
10
|
-
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
11
|
-
|
|
8
|
+
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
|
|
12
9
|
const fixPathResolution = (path, config) => {
|
|
13
10
|
if (config.alias) {
|
|
14
11
|
for (const {
|
|
@@ -18,36 +15,26 @@ const fixPathResolution = (path, config) => {
|
|
|
18
15
|
path = path.replace(find, replacement);
|
|
19
16
|
}
|
|
20
17
|
}
|
|
21
|
-
|
|
22
18
|
return path;
|
|
23
19
|
};
|
|
24
|
-
|
|
25
20
|
exports.fixPathResolution = fixPathResolution;
|
|
26
|
-
|
|
27
21
|
const getPathWithExtension = (pathWithoutExtension, config) => {
|
|
28
22
|
pathWithoutExtension = fixPathResolution(pathWithoutExtension, config);
|
|
29
|
-
|
|
30
23
|
if (/\.(less|css|png|gif|jpg|jpeg|js|jsx|ts|tsx|mjs)$/.test(pathWithoutExtension)) {
|
|
31
24
|
return pathWithoutExtension;
|
|
32
25
|
}
|
|
33
|
-
|
|
34
26
|
if (_fs.default.existsSync(pathWithoutExtension + ".js")) {
|
|
35
27
|
return pathWithoutExtension + ".js";
|
|
36
28
|
}
|
|
37
|
-
|
|
38
29
|
if (_fs.default.existsSync(pathWithoutExtension + ".jsx")) {
|
|
39
30
|
return pathWithoutExtension + ".jsx";
|
|
40
31
|
}
|
|
41
|
-
|
|
42
32
|
if (_fs.default.existsSync(pathWithoutExtension + ".tsx")) {
|
|
43
33
|
return pathWithoutExtension + ".tsx";
|
|
44
34
|
}
|
|
45
|
-
|
|
46
35
|
if (_fs.default.existsSync(pathWithoutExtension + ".ts")) {
|
|
47
36
|
return pathWithoutExtension + ".ts";
|
|
48
37
|
}
|
|
49
|
-
|
|
50
38
|
return null;
|
|
51
39
|
};
|
|
52
|
-
|
|
53
40
|
exports.getPathWithExtension = getPathWithExtension;
|
|
@@ -4,11 +4,11 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
6
|
exports.schemaFromIntrospectionData = void 0;
|
|
7
|
-
|
|
8
7
|
/**
|
|
9
8
|
* Takes the introspectionQuery response and parses it into the "Schema"
|
|
10
9
|
* type that we use to look up types, interfaces, etc.
|
|
11
10
|
*/
|
|
11
|
+
|
|
12
12
|
const schemaFromIntrospectionData = schema => {
|
|
13
13
|
const result = {
|
|
14
14
|
interfacesByName: {},
|
|
@@ -17,20 +17,18 @@ const schemaFromIntrospectionData = schema => {
|
|
|
17
17
|
unionsByName: {},
|
|
18
18
|
enumsByName: {}
|
|
19
19
|
};
|
|
20
|
-
|
|
21
20
|
schema.__schema.types.forEach(type => {
|
|
22
21
|
if (type.kind === "ENUM") {
|
|
23
22
|
result.enumsByName[type.name] = type;
|
|
24
23
|
return;
|
|
25
24
|
}
|
|
26
|
-
|
|
27
25
|
if (type.kind === "UNION") {
|
|
28
26
|
result.unionsByName[type.name] = type;
|
|
29
27
|
return;
|
|
30
28
|
}
|
|
31
|
-
|
|
32
29
|
if (type.kind === "INTERFACE") {
|
|
33
|
-
result.interfacesByName[type.name] = {
|
|
30
|
+
result.interfacesByName[type.name] = {
|
|
31
|
+
...type,
|
|
34
32
|
possibleTypesByName: {},
|
|
35
33
|
fieldsByName: {}
|
|
36
34
|
};
|
|
@@ -40,30 +38,24 @@ const schemaFromIntrospectionData = schema => {
|
|
|
40
38
|
});
|
|
41
39
|
return;
|
|
42
40
|
}
|
|
43
|
-
|
|
44
41
|
if (type.kind === "INPUT_OBJECT") {
|
|
45
42
|
result.inputObjectsByName[type.name] = type;
|
|
46
43
|
return;
|
|
47
44
|
}
|
|
48
|
-
|
|
49
45
|
if (type.kind === "SCALAR") {
|
|
50
46
|
return;
|
|
51
47
|
}
|
|
52
|
-
|
|
53
|
-
|
|
48
|
+
result.typesByName[type.name] = {
|
|
49
|
+
...type,
|
|
54
50
|
fieldsByName: {}
|
|
55
51
|
};
|
|
56
|
-
|
|
57
52
|
if (!type.fields) {
|
|
58
53
|
return;
|
|
59
54
|
}
|
|
60
|
-
|
|
61
55
|
type.fields.forEach(field => {
|
|
62
56
|
result.typesByName[type.name].fieldsByName[field.name] = field;
|
|
63
57
|
});
|
|
64
58
|
});
|
|
65
|
-
|
|
66
59
|
return result;
|
|
67
60
|
};
|
|
68
|
-
|
|
69
61
|
exports.schemaFromIntrospectionData = schemaFromIntrospectionData;
|