@khanacademy/graphql-flow 3.1.2 → 3.2.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/CHANGELOG.md +14 -0
- package/dist/cli/config.js +60 -30
- package/dist/cli/run.js +24 -64
- package/dist/enums.js +8 -24
- package/dist/generateResponseType.js +22 -91
- package/dist/generateTypeFiles.js +21 -48
- package/dist/generateVariablesType.js +6 -41
- package/dist/index.js +30 -30
- 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 +10 -8
- package/schema.json +3 -0
- package/src/__test__/generateTypeFileContents.test.ts +4 -10
- package/src/__test__/graphql-flow.test.ts +65 -77
- package/src/cli/__test__/config.test.ts +40 -1
- package/src/cli/config.ts +79 -5
- package/src/cli/run.ts +28 -43
- package/src/enums.ts +2 -2
- package/src/generateResponseType.ts +2 -2
- package/src/generateTypeFiles.ts +18 -15
- package/src/generateVariablesType.ts +3 -1
- package/src/index.ts +12 -4
- package/src/parser/parse.ts +8 -3
- package/src/types.ts +8 -0
|
@@ -4,29 +4,22 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
6
|
exports.unionOrInterfaceToFlow = exports.typeToFlow = exports.objectPropertiesToFlow = exports.generateResponseType = exports.generateFragmentType = void 0;
|
|
7
|
-
|
|
8
7
|
var _generator = _interopRequireDefault(require("@babel/generator"));
|
|
9
|
-
|
|
10
8
|
var babelTypes = _interopRequireWildcard(require("@babel/types"));
|
|
11
|
-
|
|
12
9
|
var _utils = require("./utils");
|
|
13
|
-
|
|
14
10
|
var _enums = require("./enums");
|
|
15
|
-
|
|
16
|
-
function
|
|
17
|
-
|
|
18
|
-
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; }
|
|
19
|
-
|
|
20
|
-
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
21
|
-
|
|
11
|
+
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function (e) { return e ? t : r; })(e); }
|
|
12
|
+
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
|
|
13
|
+
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
|
|
22
14
|
/* eslint-disable no-console */
|
|
15
|
+
|
|
23
16
|
const generateResponseType = (schema, query, ctx) => {
|
|
24
17
|
const ast = querySelectionToObjectType(ctx, query.selectionSet.selections, query.operation === "mutation" ? schema.typesByName.Mutation : schema.typesByName.Query, query.operation === "mutation" ? "mutation" : "query");
|
|
25
|
-
return (0, _generator.default)(ast
|
|
18
|
+
return (0, _generator.default)(ast, {
|
|
19
|
+
comments: ctx.noComments ? false : true
|
|
20
|
+
}).code;
|
|
26
21
|
};
|
|
27
|
-
|
|
28
22
|
exports.generateResponseType = generateResponseType;
|
|
29
|
-
|
|
30
23
|
const sortedObjectTypeAnnotation = (ctx, properties) => {
|
|
31
24
|
const obj = (0, _utils.objectTypeFromProperties)(properties.sort((a, b) => {
|
|
32
25
|
if (a.type === "TSPropertySignature" && b.type === "TSPropertySignature") {
|
|
@@ -34,12 +27,10 @@ const sortedObjectTypeAnnotation = (ctx, properties) => {
|
|
|
34
27
|
const bName = b.key.type === "Identifier" ? b.key.name : "";
|
|
35
28
|
return aName < bName ? -1 : 1;
|
|
36
29
|
}
|
|
37
|
-
|
|
38
30
|
return 0;
|
|
39
31
|
}));
|
|
40
32
|
const name = ctx.path.join("_");
|
|
41
33
|
const isTopLevelType = ctx.path.length <= 1;
|
|
42
|
-
|
|
43
34
|
if (ctx.allObjectTypes != null && !isTopLevelType) {
|
|
44
35
|
ctx.allObjectTypes[name] = obj;
|
|
45
36
|
return babelTypes.tsTypeReference(babelTypes.identifier(name));
|
|
@@ -47,11 +38,9 @@ const sortedObjectTypeAnnotation = (ctx, properties) => {
|
|
|
47
38
|
return obj;
|
|
48
39
|
}
|
|
49
40
|
};
|
|
50
|
-
|
|
51
41
|
const generateFragmentType = (schema, fragment, ctx) => {
|
|
52
42
|
const onType = fragment.typeCondition.name.value;
|
|
53
43
|
let ast;
|
|
54
|
-
|
|
55
44
|
if (schema.typesByName[onType]) {
|
|
56
45
|
ast = sortedObjectTypeAnnotation(ctx, objectPropertiesToFlow(ctx, schema.typesByName[onType], onType, fragment.selectionSet.selections));
|
|
57
46
|
} else if (schema.interfacesByName[onType]) {
|
|
@@ -61,86 +50,66 @@ const generateFragmentType = (schema, fragment, ctx) => {
|
|
|
61
50
|
} else {
|
|
62
51
|
throw new Error(`Unknown ${onType}`);
|
|
63
52
|
}
|
|
64
|
-
|
|
65
|
-
|
|
53
|
+
return (0, _generator.default)(ast, {
|
|
54
|
+
comments: ctx.noComments ? false : true
|
|
55
|
+
}).code;
|
|
66
56
|
};
|
|
67
|
-
|
|
68
57
|
exports.generateFragmentType = generateFragmentType;
|
|
69
|
-
|
|
70
58
|
const _typeToFlow = (ctx, type, selection) => {
|
|
71
59
|
if (type.kind === "SCALAR") {
|
|
72
60
|
return (0, _enums.scalarTypeToFlow)(ctx, type.name);
|
|
73
61
|
}
|
|
74
|
-
|
|
75
62
|
if (type.kind === "LIST") {
|
|
76
63
|
return babelTypes.tsTypeReference(ctx.readOnlyArray ? babelTypes.identifier("ReadonlyArray") : babelTypes.identifier("Array"), babelTypes.tsTypeParameterInstantiation([typeToFlow(ctx, type.ofType, selection)]));
|
|
77
64
|
}
|
|
78
|
-
|
|
79
65
|
if (type.kind === "UNION") {
|
|
80
66
|
const union = ctx.schema.unionsByName[type.name];
|
|
81
|
-
|
|
82
67
|
if (!selection.selectionSet) {
|
|
83
68
|
console.log("no selection set", selection);
|
|
84
69
|
return babelTypes.tsAnyKeyword();
|
|
85
70
|
}
|
|
86
|
-
|
|
87
71
|
return unionOrInterfaceToFlow(ctx, union, selection.selectionSet.selections);
|
|
88
72
|
}
|
|
89
|
-
|
|
90
73
|
if (type.kind === "INTERFACE") {
|
|
91
74
|
if (!selection.selectionSet) {
|
|
92
75
|
console.log("no selection set", selection);
|
|
93
76
|
return babelTypes.tsAnyKeyword();
|
|
94
77
|
}
|
|
95
|
-
|
|
96
78
|
return unionOrInterfaceToFlow(ctx, ctx.schema.interfacesByName[type.name], selection.selectionSet.selections);
|
|
97
79
|
}
|
|
98
|
-
|
|
99
80
|
if (type.kind === "ENUM") {
|
|
100
81
|
return (0, _enums.enumTypeToFlow)(ctx, type.name);
|
|
101
82
|
}
|
|
102
|
-
|
|
103
83
|
if (type.kind !== "OBJECT") {
|
|
104
84
|
console.log("not object", type);
|
|
105
85
|
return babelTypes.tsAnyKeyword();
|
|
106
86
|
}
|
|
107
|
-
|
|
108
87
|
const tname = type.name;
|
|
109
|
-
|
|
110
88
|
if (!ctx.schema.typesByName[tname]) {
|
|
111
89
|
console.log("unknown referenced type", tname);
|
|
112
90
|
return babelTypes.tsAnyKeyword();
|
|
113
91
|
}
|
|
114
|
-
|
|
115
92
|
const childType = ctx.schema.typesByName[tname];
|
|
116
|
-
|
|
117
93
|
if (!selection.selectionSet) {
|
|
118
94
|
console.log("no selection set", selection);
|
|
119
95
|
return babelTypes.tsAnyKeyword();
|
|
120
96
|
}
|
|
121
|
-
|
|
122
97
|
return (0, _utils.maybeAddDescriptionComment)(childType.description, querySelectionToObjectType(ctx, selection.selectionSet.selections, childType, tname));
|
|
123
98
|
};
|
|
124
|
-
|
|
125
99
|
const typeToFlow = (ctx, type, selection) => {
|
|
126
100
|
// throw new Error('npoe');
|
|
127
101
|
if (type.kind === "NON_NULL") {
|
|
128
102
|
return _typeToFlow(ctx, type.ofType, selection);
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
|
|
103
|
+
}
|
|
104
|
+
// If we don'babelTypes care about strict nullability checking, then pretend everything is non-null
|
|
132
105
|
if (!ctx.strictNullability) {
|
|
133
106
|
return _typeToFlow(ctx, type, selection);
|
|
134
107
|
}
|
|
135
|
-
|
|
136
108
|
const inner = _typeToFlow(ctx, type, selection);
|
|
137
|
-
|
|
138
109
|
const result = (0, _utils.nullableType)(inner);
|
|
139
110
|
return (0, _utils.transferLeadingComments)(inner, result);
|
|
140
111
|
};
|
|
141
|
-
|
|
142
112
|
exports.typeToFlow = typeToFlow;
|
|
143
|
-
|
|
144
113
|
const ensureOnlyOneTypenameProperty = properties => {
|
|
145
114
|
let seenTypeName = false;
|
|
146
115
|
return properties.filter(type => {
|
|
@@ -149,104 +118,84 @@ const ensureOnlyOneTypenameProperty = properties => {
|
|
|
149
118
|
// the extra one here.
|
|
150
119
|
if (type.type === "TSPropertySignature" && type.key.type === "Identifier" && type.key.name === "__typename") {
|
|
151
120
|
var _type$typeAnnotation;
|
|
152
|
-
|
|
153
121
|
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";
|
|
154
|
-
|
|
155
122
|
if (seenTypeName) {
|
|
156
123
|
if (name !== seenTypeName) {
|
|
157
124
|
throw new Error(`Got two different type names ${name}, ${seenTypeName}`);
|
|
158
125
|
}
|
|
159
|
-
|
|
160
126
|
return false;
|
|
161
127
|
}
|
|
162
|
-
|
|
163
128
|
seenTypeName = name;
|
|
164
129
|
}
|
|
165
|
-
|
|
166
130
|
return true;
|
|
167
131
|
});
|
|
168
132
|
};
|
|
169
|
-
|
|
170
133
|
const querySelectionToObjectType = (ctx, selections, type, typeName) => {
|
|
171
134
|
return sortedObjectTypeAnnotation(ctx, ensureOnlyOneTypenameProperty(objectPropertiesToFlow(ctx, type, typeName, selections)));
|
|
172
135
|
};
|
|
173
|
-
|
|
174
136
|
const objectPropertiesToFlow = (ctx, type, typeName, selections) => {
|
|
175
137
|
return selections.flatMap(selection => {
|
|
176
138
|
switch (selection.kind) {
|
|
177
139
|
case "InlineFragment":
|
|
178
140
|
{
|
|
179
141
|
var _selection$typeCondit, _selection$typeCondit2;
|
|
180
|
-
|
|
181
142
|
const newTypeName = (_selection$typeCondit = (_selection$typeCondit2 = selection.typeCondition) === null || _selection$typeCondit2 === void 0 ? void 0 : _selection$typeCondit2.name.value) !== null && _selection$typeCondit !== void 0 ? _selection$typeCondit : typeName;
|
|
182
|
-
|
|
183
143
|
if (newTypeName !== typeName) {
|
|
184
144
|
return [];
|
|
185
145
|
}
|
|
186
|
-
|
|
187
146
|
return objectPropertiesToFlow(ctx, ctx.schema.typesByName[newTypeName], newTypeName, selection.selectionSet.selections);
|
|
188
147
|
}
|
|
189
|
-
|
|
190
148
|
case "FragmentSpread":
|
|
191
149
|
if (!ctx.fragments[selection.name.value]) {
|
|
192
150
|
ctx.errors.push(`No fragment named '${selection.name.value}'. Did you forget to include it in the template literal?`);
|
|
193
151
|
return [babelTypes.tsPropertySignature(babelTypes.identifier(selection.name.value), babelTypes.tsTypeAnnotation(babelTypes.tsTypeReference(babelTypes.identifier(`UNKNOWN_FRAGMENT`))))];
|
|
194
152
|
}
|
|
195
|
-
|
|
196
153
|
return objectPropertiesToFlow(ctx, type, typeName, ctx.fragments[selection.name.value].selectionSet.selections);
|
|
197
|
-
|
|
198
154
|
case "Field":
|
|
199
155
|
const name = selection.name.value;
|
|
200
156
|
const alias = selection.alias ? selection.alias.value : name;
|
|
201
|
-
|
|
202
157
|
if (name === "__typename") {
|
|
203
158
|
return [babelTypes.tsPropertySignature(babelTypes.identifier(alias), babelTypes.tsTypeAnnotation(babelTypes.tsLiteralType(babelTypes.stringLiteral(typeName))))];
|
|
204
159
|
}
|
|
205
|
-
|
|
206
160
|
if (!type.fieldsByName[name]) {
|
|
207
161
|
ctx.errors.push(`Unknown field '${name}' for type '${typeName}'`);
|
|
208
162
|
return [babelTypes.tsPropertySignature(babelTypes.identifier(alias), babelTypes.tsTypeAnnotation(babelTypes.tsTypeReference(babelTypes.identifier(`UNKNOWN_FIELD["${name}"]`))))];
|
|
209
163
|
}
|
|
210
|
-
|
|
211
164
|
const typeField = type.fieldsByName[name];
|
|
212
|
-
return [(0, _utils.maybeAddDescriptionComment)(typeField.description, (0, _utils.liftLeadingPropertyComments)(babelTypes.tsPropertySignature(babelTypes.identifier(alias), babelTypes.tsTypeAnnotation(typeToFlow({
|
|
165
|
+
return [(0, _utils.maybeAddDescriptionComment)(typeField.description, (0, _utils.liftLeadingPropertyComments)(babelTypes.tsPropertySignature(babelTypes.identifier(alias), babelTypes.tsTypeAnnotation(typeToFlow({
|
|
166
|
+
...ctx,
|
|
213
167
|
path: ctx.path.concat([alias])
|
|
214
168
|
}, typeField.type, selection)))))];
|
|
215
|
-
|
|
216
169
|
default:
|
|
217
170
|
ctx.errors.push(`Unsupported selection kind '${selection.kind}'`);
|
|
218
171
|
return [];
|
|
219
172
|
}
|
|
220
173
|
});
|
|
221
174
|
};
|
|
222
|
-
|
|
223
175
|
exports.objectPropertiesToFlow = objectPropertiesToFlow;
|
|
224
|
-
|
|
225
176
|
const unionOrInterfaceToFlow = (ctx, type, selections) => {
|
|
226
177
|
const allFields = selections.every(selection => selection.kind === "Field");
|
|
227
178
|
const selectedAttributes = type.possibleTypes.slice().sort((a, b) => {
|
|
228
179
|
return a.name < b.name ? -1 : 1;
|
|
229
180
|
}).map(possible => {
|
|
230
|
-
const configWithUpdatedPath = {
|
|
181
|
+
const configWithUpdatedPath = {
|
|
182
|
+
...ctx,
|
|
231
183
|
path: allFields ? ctx.path : ctx.path.concat([possible.name])
|
|
232
184
|
};
|
|
233
185
|
return {
|
|
234
186
|
typeName: possible.name,
|
|
235
187
|
attributes: ensureOnlyOneTypenameProperty(selections.map(selection => unionOrInterfaceSelection(configWithUpdatedPath, type, possible, selection)).flat())
|
|
236
188
|
};
|
|
237
|
-
});
|
|
238
|
-
|
|
189
|
+
});
|
|
190
|
+
// If they're all fields, the only selection that could be different is __typename
|
|
239
191
|
if (allFields) {
|
|
240
192
|
const sharedAttributes = selectedAttributes[0].attributes.slice();
|
|
241
193
|
const typeNameIndex = selectedAttributes[0].attributes.findIndex(x => x.type === "TSPropertySignature" && x.key.type === "Identifier" && x.key.name === "__typename");
|
|
242
|
-
|
|
243
194
|
if (typeNameIndex !== -1) {
|
|
244
195
|
sharedAttributes[typeNameIndex] = babelTypes.tsPropertySignature(babelTypes.identifier("__typename"), babelTypes.tsTypeAnnotation(babelTypes.tsUnionType(selectedAttributes.map(attrs => attrs.attributes[typeNameIndex].typeAnnotation.typeAnnotation))));
|
|
245
196
|
}
|
|
246
|
-
|
|
247
197
|
return sortedObjectTypeAnnotation(ctx, sharedAttributes);
|
|
248
198
|
}
|
|
249
|
-
|
|
250
199
|
if (selectedAttributes.length === 1) {
|
|
251
200
|
return sortedObjectTypeAnnotation(ctx, selectedAttributes[0].attributes);
|
|
252
201
|
}
|
|
@@ -280,86 +229,68 @@ const unionOrInterfaceToFlow = (ctx, type, selections) => {
|
|
|
280
229
|
* ```
|
|
281
230
|
* instead of e.g. `getHuman_me_Human`.
|
|
282
231
|
*/
|
|
283
|
-
|
|
284
|
-
|
|
285
232
|
const result = babelTypes.tsUnionType(selectedAttributes.map(({
|
|
286
233
|
typeName,
|
|
287
234
|
attributes
|
|
288
|
-
}) => sortedObjectTypeAnnotation({
|
|
235
|
+
}) => sortedObjectTypeAnnotation({
|
|
236
|
+
...ctx,
|
|
289
237
|
path: ctx.path.concat([typeName])
|
|
290
238
|
}, attributes)));
|
|
291
239
|
const name = ctx.path.join("_");
|
|
292
|
-
|
|
293
240
|
if (ctx.allObjectTypes && ctx.path.length > 1) {
|
|
294
241
|
ctx.allObjectTypes[name] = result;
|
|
295
242
|
return babelTypes.tsTypeReference(babelTypes.identifier(name));
|
|
296
243
|
}
|
|
297
|
-
|
|
298
244
|
return result;
|
|
299
245
|
};
|
|
300
|
-
|
|
301
246
|
exports.unionOrInterfaceToFlow = unionOrInterfaceToFlow;
|
|
302
|
-
|
|
303
247
|
const unionOrInterfaceSelection = (config, type, possible, selection) => {
|
|
304
248
|
if (selection.kind === "Field" && selection.name.value === "__typename") {
|
|
305
249
|
const alias = selection.alias ? selection.alias.value : selection.name.value;
|
|
306
250
|
return [babelTypes.tsPropertySignature(babelTypes.identifier(alias), babelTypes.tsTypeAnnotation(babelTypes.tsLiteralType(babelTypes.stringLiteral(possible.name))))];
|
|
307
251
|
}
|
|
308
|
-
|
|
309
252
|
if (selection.kind === "Field" && type.kind !== "UNION") {
|
|
310
253
|
// this is an interface
|
|
311
254
|
const name = selection.name.value;
|
|
312
255
|
const alias = selection.alias ? selection.alias.value : name;
|
|
313
|
-
|
|
314
256
|
if (!type.fieldsByName[name]) {
|
|
315
257
|
config.errors.push("Unknown field: " + name + " on type " + type.name + " for possible " + possible.name);
|
|
316
258
|
return [babelTypes.tsPropertySignature(babelTypes.identifier(alias), babelTypes.tsTypeAnnotation(babelTypes.tsTypeReference(babelTypes.identifier(`UNKNOWN_FIELD`))))];
|
|
317
259
|
}
|
|
318
|
-
|
|
319
260
|
const typeField = type.fieldsByName[name];
|
|
320
|
-
return [(0, _utils.liftLeadingPropertyComments)(babelTypes.tsPropertySignature(babelTypes.identifier(alias), babelTypes.tsTypeAnnotation(typeToFlow({
|
|
261
|
+
return [(0, _utils.liftLeadingPropertyComments)(babelTypes.tsPropertySignature(babelTypes.identifier(alias), babelTypes.tsTypeAnnotation(typeToFlow({
|
|
262
|
+
...config,
|
|
321
263
|
path: config.path.concat([name])
|
|
322
264
|
}, typeField.type, selection))))];
|
|
323
265
|
}
|
|
324
|
-
|
|
325
266
|
if (selection.kind === "FragmentSpread") {
|
|
326
267
|
const fragment = config.fragments[selection.name.value];
|
|
327
|
-
|
|
328
268
|
if (!fragment) {
|
|
329
269
|
throw new Error(`Unknown fragment ${selection.name.value}`);
|
|
330
270
|
}
|
|
331
|
-
|
|
332
271
|
const typeName = fragment.typeCondition.name.value;
|
|
333
|
-
|
|
334
272
|
if (config.schema.interfacesByName[typeName] && config.schema.interfacesByName[typeName].possibleTypesByName[possible.name] || typeName === possible.name) {
|
|
335
273
|
return fragment.selectionSet.selections.flatMap(selection => unionOrInterfaceSelection(config, config.schema.typesByName[possible.name], possible, selection));
|
|
336
274
|
} else {
|
|
337
275
|
return [];
|
|
338
276
|
}
|
|
339
277
|
}
|
|
340
|
-
|
|
341
278
|
if (selection.kind !== "InlineFragment") {
|
|
342
279
|
config.errors.push(`union selectors must be inline fragment: found ${selection.kind}`);
|
|
343
|
-
|
|
344
280
|
if (type.kind === "UNION") {
|
|
345
281
|
config.errors.push(`You're trying to select a field from the union ${type.name},
|
|
346
282
|
but the only field you're allowed to select is "__typename".
|
|
347
283
|
Try using an inline fragment "... on SomeType {}".`);
|
|
348
284
|
}
|
|
349
|
-
|
|
350
285
|
return [];
|
|
351
286
|
}
|
|
352
|
-
|
|
353
287
|
if (selection.typeCondition) {
|
|
354
288
|
var _config$schema$interf;
|
|
355
|
-
|
|
356
289
|
const typeName = selection.typeCondition.name.value;
|
|
357
290
|
const indirectMatch = (_config$schema$interf = config.schema.interfacesByName[typeName]) === null || _config$schema$interf === void 0 ? void 0 : _config$schema$interf.possibleTypesByName[possible.name];
|
|
358
|
-
|
|
359
291
|
if (typeName !== possible.name && !indirectMatch) {
|
|
360
292
|
return [];
|
|
361
293
|
}
|
|
362
294
|
}
|
|
363
|
-
|
|
364
295
|
return objectPropertiesToFlow(config, config.schema.typesByName[possible.name], possible.name, selection.selectionSet.selections);
|
|
365
296
|
};
|
|
@@ -4,15 +4,10 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
6
|
exports.processPragmas = exports.indexPrelude = exports.generateTypeFiles = exports.generateTypeFileContents = void 0;
|
|
7
|
-
|
|
8
7
|
var _fs = _interopRequireDefault(require("fs"));
|
|
9
|
-
|
|
10
8
|
var _path = _interopRequireDefault(require("path"));
|
|
11
|
-
|
|
12
9
|
var _ = require(".");
|
|
13
|
-
|
|
14
|
-
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
15
|
-
|
|
10
|
+
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
|
|
16
11
|
const indexPrelude = regenerateCommand => `// AUTOGENERATED
|
|
17
12
|
// NOTE: New response types are added to this file automatically.
|
|
18
13
|
// Outdated response types can be removed manually as they are deprecated.
|
|
@@ -20,26 +15,22 @@ const indexPrelude = regenerateCommand => `// AUTOGENERATED
|
|
|
20
15
|
//
|
|
21
16
|
|
|
22
17
|
`;
|
|
23
|
-
|
|
24
18
|
exports.indexPrelude = indexPrelude;
|
|
19
|
+
const generateTypeFileContents = (fileName, schema, document, options, generatedDir, originalIndexContents) => {
|
|
20
|
+
let indexContents = originalIndexContents;
|
|
21
|
+
const files = {};
|
|
25
22
|
|
|
26
|
-
|
|
27
|
-
const files = {}; /// Write export for __generated__/index.ts if it doesn't exist
|
|
28
|
-
|
|
23
|
+
/// Write export for __generated__/index.ts if it doesn't exist
|
|
29
24
|
const addToIndex = (filePath, typeName) => {
|
|
30
|
-
if (options.
|
|
25
|
+
if (options.omitFileExtensions) {
|
|
31
26
|
// Typescript doesn't like file extensions
|
|
32
27
|
filePath = filePath.replace(/\.ts$/, "");
|
|
33
28
|
}
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
// prefix matches.
|
|
37
|
-
|
|
38
|
-
if (indexContents.indexOf(newLine) === -1) {
|
|
29
|
+
const newLine = `export type {${typeName}} from "./${_path.default.basename(filePath)}";`;
|
|
30
|
+
if (!indexContents.includes(newLine)) {
|
|
39
31
|
indexContents += newLine + "\n";
|
|
40
32
|
}
|
|
41
33
|
};
|
|
42
|
-
|
|
43
34
|
const generated = (0, _.documentToFlowTypes)(document, schema, options);
|
|
44
35
|
generated.forEach(({
|
|
45
36
|
name,
|
|
@@ -52,18 +43,13 @@ const generateTypeFileContents = (fileName, schema, document, options, generated
|
|
|
52
43
|
// We write all generated files to a `__generated__` subdir to keep
|
|
53
44
|
// things tidy.
|
|
54
45
|
const targetFileName = options.typeFileName ? options.typeFileName.replace("[operationName]", name) : `${name}.ts`;
|
|
55
|
-
|
|
56
46
|
const targetPath = _path.default.join(generatedDir, targetFileName);
|
|
57
|
-
|
|
58
47
|
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;
|
|
59
|
-
|
|
60
48
|
if (options.splitTypes && !isFragment) {
|
|
61
49
|
fileContents += `\nexport type ${name} = ${typeName}['response'];\n` + `export type ${name}Variables = ${typeName}['variables'];\n`;
|
|
62
50
|
}
|
|
63
|
-
|
|
64
51
|
Object.keys(extraTypes).forEach(name => fileContents += `\n\nexport type ${name} = ${extraTypes[name]};`);
|
|
65
52
|
const enumNames = Object.keys(experimentalEnums);
|
|
66
|
-
|
|
67
53
|
if (options.experimentalEnums && enumNames.length) {
|
|
68
54
|
// TODO(somewhatabstract, FEI-4172): Update to fixed eslint-plugin-flowtype
|
|
69
55
|
// and remove this disable.
|
|
@@ -71,9 +57,9 @@ const generateTypeFileContents = (fileName, schema, document, options, generated
|
|
|
71
57
|
enumNames.forEach(name => fileContents += `\nexport ${experimentalEnums[name]};\n`);
|
|
72
58
|
fileContents += `/* eslint-enable no-undef */`;
|
|
73
59
|
}
|
|
74
|
-
|
|
75
60
|
addToIndex(targetPath, typeName);
|
|
76
|
-
files[targetPath] = fileContents
|
|
61
|
+
files[targetPath] = fileContents
|
|
62
|
+
// Remove whitespace from the ends of lines; babel's generate sometimes
|
|
77
63
|
// leaves them hanging around.
|
|
78
64
|
.replace(/\s+$/gm, "") + "\n";
|
|
79
65
|
});
|
|
@@ -82,14 +68,10 @@ const generateTypeFileContents = (fileName, schema, document, options, generated
|
|
|
82
68
|
indexContents
|
|
83
69
|
};
|
|
84
70
|
};
|
|
85
|
-
|
|
86
71
|
exports.generateTypeFileContents = generateTypeFileContents;
|
|
87
|
-
|
|
88
72
|
const getGeneratedDir = (fileName, options) => {
|
|
89
73
|
var _options$generatedDir;
|
|
90
|
-
|
|
91
74
|
const generatedDirectory = (_options$generatedDir = options.generatedDirectory) !== null && _options$generatedDir !== void 0 ? _options$generatedDir : "__generated__";
|
|
92
|
-
|
|
93
75
|
if (_path.default.isAbsolute(generatedDirectory)) {
|
|
94
76
|
// fileName is absolute here, so we make it relative to cwd
|
|
95
77
|
// for more reasonable filenames. We convert leading ..'s
|
|
@@ -99,49 +81,41 @@ const getGeneratedDir = (fileName, options) => {
|
|
|
99
81
|
return _path.default.join(_path.default.dirname(fileName), generatedDirectory);
|
|
100
82
|
}
|
|
101
83
|
};
|
|
102
|
-
|
|
103
|
-
const generateTypeFiles = (fileName, schema, document, options) => {
|
|
84
|
+
const generateTypeFiles = (fileName, schema, document, options, outputFiles) => {
|
|
104
85
|
const generatedDir = getGeneratedDir(fileName, options);
|
|
105
|
-
|
|
106
86
|
const indexFile = _path.default.join(generatedDir, "index.ts");
|
|
107
|
-
|
|
108
87
|
if (!_fs.default.existsSync(generatedDir)) {
|
|
109
88
|
_fs.default.mkdirSync(generatedDir, {
|
|
110
89
|
recursive: true
|
|
111
90
|
});
|
|
112
91
|
}
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
_fs.default.writeFileSync(indexFile, indexPrelude(options.regenerateCommand));
|
|
92
|
+
if (!outputFiles[indexFile]) {
|
|
93
|
+
outputFiles[indexFile] = indexPrelude(options.regenerateCommand);
|
|
116
94
|
}
|
|
117
|
-
|
|
118
95
|
const {
|
|
119
96
|
indexContents,
|
|
120
97
|
files
|
|
121
|
-
} = generateTypeFileContents(fileName, schema, document, options, generatedDir,
|
|
98
|
+
} = generateTypeFileContents(fileName, schema, document, options, generatedDir, outputFiles[indexFile]);
|
|
122
99
|
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
});
|
|
100
|
+
// Write out files
|
|
101
|
+
for (const [fname, content] of Object.entries(files)) {
|
|
102
|
+
outputFiles[fname] = content;
|
|
103
|
+
}
|
|
128
104
|
|
|
129
|
-
|
|
105
|
+
// Write out index file
|
|
106
|
+
outputFiles[indexFile] = indexContents;
|
|
107
|
+
return outputFiles;
|
|
130
108
|
};
|
|
131
|
-
|
|
132
109
|
exports.generateTypeFiles = generateTypeFiles;
|
|
133
|
-
|
|
134
110
|
const processPragmas = (generateConfig, crawlConfig, rawSource) => {
|
|
135
111
|
if (crawlConfig.ignorePragma && rawSource.includes(crawlConfig.ignorePragma)) {
|
|
136
112
|
return {
|
|
137
113
|
generate: false
|
|
138
114
|
};
|
|
139
115
|
}
|
|
140
|
-
|
|
141
116
|
const autogen = crawlConfig.loosePragma ? rawSource.includes(crawlConfig.loosePragma) : false;
|
|
142
117
|
const autogenStrict = crawlConfig.pragma ? rawSource.includes(crawlConfig.pragma) : false;
|
|
143
118
|
const noPragmas = !crawlConfig.loosePragma && !crawlConfig.pragma;
|
|
144
|
-
|
|
145
119
|
if (autogen || autogenStrict || noPragmas) {
|
|
146
120
|
return {
|
|
147
121
|
generate: true,
|
|
@@ -153,5 +127,4 @@ const processPragmas = (generateConfig, crawlConfig, rawSource) => {
|
|
|
153
127
|
};
|
|
154
128
|
}
|
|
155
129
|
};
|
|
156
|
-
|
|
157
130
|
exports.processPragmas = processPragmas;
|
|
@@ -4,121 +4,86 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
6
|
exports.maybeOptionalObjectTypeProperty = exports.inputRefToFlow = exports.inputObjectToFlow = exports.generateVariablesType = void 0;
|
|
7
|
-
|
|
8
7
|
var _generator = _interopRequireDefault(require("@babel/generator"));
|
|
9
|
-
|
|
10
8
|
var babelTypes = _interopRequireWildcard(require("@babel/types"));
|
|
11
|
-
|
|
12
9
|
var _enums = require("./enums");
|
|
13
|
-
|
|
14
10
|
var _utils = require("./utils");
|
|
15
|
-
|
|
16
|
-
function
|
|
17
|
-
|
|
18
|
-
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; }
|
|
19
|
-
|
|
20
|
-
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
21
|
-
|
|
11
|
+
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function (e) { return e ? t : r; })(e); }
|
|
12
|
+
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
|
|
13
|
+
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
|
|
22
14
|
const inputObjectToFlow = (ctx, name) => {
|
|
23
15
|
const inputObject = ctx.schema.inputObjectsByName[name];
|
|
24
|
-
|
|
25
16
|
if (!inputObject) {
|
|
26
17
|
ctx.errors.push(`Unknown input object ${name}`);
|
|
27
18
|
return babelTypes.tsLiteralType(babelTypes.stringLiteral(`Unknown input object ${name}`));
|
|
28
19
|
}
|
|
29
|
-
|
|
30
20
|
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, vbl.defaultValue != null))))));
|
|
31
21
|
};
|
|
32
|
-
|
|
33
22
|
exports.inputObjectToFlow = inputObjectToFlow;
|
|
34
|
-
|
|
35
23
|
const maybeOptionalObjectTypeProperty = (name, type) => {
|
|
36
24
|
const prop = (0, _utils.liftLeadingPropertyComments)(babelTypes.tsPropertySignature(babelTypes.identifier(name), babelTypes.tsTypeAnnotation(type)));
|
|
37
|
-
|
|
38
25
|
if ((0, _utils.isnNullableType)(type)) {
|
|
39
26
|
prop.optional = true;
|
|
40
27
|
}
|
|
41
|
-
|
|
42
28
|
return prop;
|
|
43
29
|
};
|
|
44
|
-
|
|
45
30
|
exports.maybeOptionalObjectTypeProperty = maybeOptionalObjectTypeProperty;
|
|
46
|
-
|
|
47
31
|
const inputRefToFlow = (ctx, inputRef, hasDefaultValue = false) => {
|
|
48
32
|
if (inputRef.kind === "NON_NULL") {
|
|
49
33
|
const result = _inputRefToFlow(ctx, inputRef.ofType);
|
|
50
|
-
|
|
51
34
|
return hasDefaultValue ? (0, _utils.nullableType)(result) : result;
|
|
52
35
|
}
|
|
53
|
-
|
|
54
36
|
const result = _inputRefToFlow(ctx, inputRef);
|
|
55
|
-
|
|
56
37
|
return (0, _utils.transferLeadingComments)(result, (0, _utils.nullableType)(result));
|
|
57
38
|
};
|
|
58
|
-
|
|
59
39
|
exports.inputRefToFlow = inputRefToFlow;
|
|
60
|
-
|
|
61
40
|
const _inputRefToFlow = (ctx, inputRef) => {
|
|
62
41
|
if (inputRef.kind === "SCALAR") {
|
|
63
42
|
return (0, _enums.scalarTypeToFlow)(ctx, inputRef.name);
|
|
64
43
|
}
|
|
65
|
-
|
|
66
44
|
if (inputRef.kind === "ENUM") {
|
|
67
45
|
return (0, _enums.enumTypeToFlow)(ctx, inputRef.name);
|
|
68
46
|
}
|
|
69
|
-
|
|
70
47
|
if (inputRef.kind === "INPUT_OBJECT") {
|
|
71
48
|
return inputObjectToFlow(ctx, inputRef.name);
|
|
72
49
|
}
|
|
73
|
-
|
|
74
50
|
if (inputRef.kind === "LIST") {
|
|
75
51
|
return babelTypes.tsTypeReference(babelTypes.identifier("ReadonlyArray"), babelTypes.tsTypeParameterInstantiation([inputRefToFlow(ctx, inputRef.ofType)]));
|
|
76
52
|
}
|
|
77
|
-
|
|
78
53
|
return babelTypes.tsLiteralType(babelTypes.stringLiteral(JSON.stringify(inputRef)));
|
|
79
54
|
};
|
|
80
|
-
|
|
81
55
|
const variableToFlow = (ctx, type) => {
|
|
82
56
|
if (type.kind === "NonNullType") {
|
|
83
57
|
return _variableToFlow(ctx, type.type);
|
|
84
58
|
}
|
|
85
|
-
|
|
86
59
|
const result = _variableToFlow(ctx, type);
|
|
87
|
-
|
|
88
60
|
return (0, _utils.transferLeadingComments)(result, (0, _utils.nullableType)(result));
|
|
89
61
|
};
|
|
90
|
-
|
|
91
62
|
const _variableToFlow = (ctx, type) => {
|
|
92
63
|
if (type.kind === "NamedType") {
|
|
93
64
|
if (_enums.builtinScalars[type.name.value]) {
|
|
94
65
|
return (0, _enums.scalarTypeToFlow)(ctx, type.name.value);
|
|
95
66
|
}
|
|
96
|
-
|
|
97
67
|
if (ctx.schema.enumsByName[type.name.value]) {
|
|
98
68
|
return (0, _enums.enumTypeToFlow)(ctx, type.name.value);
|
|
99
69
|
}
|
|
100
|
-
|
|
101
70
|
const customScalarType = ctx.scalars[type.name.value];
|
|
102
|
-
|
|
103
71
|
if (customScalarType) {
|
|
104
72
|
return babelTypes.tsTypeReference(babelTypes.identifier(customScalarType));
|
|
105
73
|
}
|
|
106
|
-
|
|
107
74
|
return inputObjectToFlow(ctx, type.name.value);
|
|
108
75
|
}
|
|
109
|
-
|
|
110
76
|
if (type.kind === "ListType") {
|
|
111
77
|
return babelTypes.tsTypeReference(babelTypes.identifier("ReadonlyArray"), babelTypes.tsTypeParameterInstantiation([variableToFlow(ctx, type.type)]));
|
|
112
78
|
}
|
|
113
|
-
|
|
114
79
|
return babelTypes.tsLiteralType(babelTypes.stringLiteral("UNKNOWN" + JSON.stringify(type)));
|
|
115
80
|
};
|
|
116
|
-
|
|
117
81
|
const generateVariablesType = (schema, item, ctx) => {
|
|
118
82
|
const variableObject = (0, _utils.objectTypeFromProperties)((item.variableDefinitions || []).map(vbl => {
|
|
119
83
|
return maybeOptionalObjectTypeProperty(vbl.variable.name.value, variableToFlow(ctx, vbl.type));
|
|
120
84
|
}));
|
|
121
|
-
return (0, _generator.default)(variableObject
|
|
85
|
+
return (0, _generator.default)(variableObject, {
|
|
86
|
+
comments: ctx.noComments ? false : true
|
|
87
|
+
}).code;
|
|
122
88
|
};
|
|
123
|
-
|
|
124
89
|
exports.generateVariablesType = generateVariablesType;
|