forked from timqian/gql-generator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·340 lines (312 loc) · 12.6 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
#!/usr/bin/env node
const fs = require('fs');
const path = require('path');
const program = require('commander');
const { Source, buildSchema, isEqualType } = require('graphql');
const del = require('del');
const indent = (string, amount = 1) => string.replace(/^/gm, ' '.repeat(amount));
const unindent = (string, amount = 1) => string.replace(new RegExp("^" + ' '.repeat(amount), "gm"), '');
function main ({
schemaFilePath,
destDirPath,
depthLimit = 100,
includeDeprecatedFields = false,
fileExtension,
assumeValid,
includeCrossReferences = false,
filterPath,
} = {}) {
let assume = false;
if (assumeValid === 'true') {
assume = true;
}
const typeDef = fs.readFileSync(schemaFilePath, 'utf-8');
const source = new Source(typeDef);
const gqlSchema = buildSchema(source, { assumeValidSDL: assume });
const filter = filterPath ? require(path.resolve(filterPath)) : () => true;
del.sync(destDirPath);
path.resolve(destDirPath).split(path.sep).reduce((before, cur) => {
const pathTmp = path.join(before, cur + path.sep);
if (!fs.existsSync(pathTmp)) {
fs.mkdirSync(pathTmp);
}
return path.join(before, cur + path.sep);
}, '');
let indexJsExportAll = '';
/**
* Compile arguments dictionary for a field
* @param field current field object
* @param duplicateArgCounts map for deduping argument name collisions
* @param allArgsDict dictionary of all arguments
*/
const getFieldArgsDict = (
field,
duplicateArgCounts,
allArgsDict = {},
) => field.args.reduce((o, arg) => {
if (arg.name in duplicateArgCounts) {
const index = duplicateArgCounts[arg.name] + 1;
duplicateArgCounts[arg.name] = index;
o[`${arg.name}${index}`] = arg;
} else if (allArgsDict[arg.name]) {
duplicateArgCounts[arg.name] = 1;
o[`${arg.name}1`] = arg;
} else {
o[arg.name] = arg;
}
return o;
}, {});
/**
* Generate variables string
* @param dict dictionary of arguments
*/
const getArgsToVarsStr = dict => Object.entries(dict)
.map(([varName, arg]) => `${arg.name}: $${varName}`)
.join(', ');
/**
* Generate types string
* @param dict dictionary of arguments
*/
const getVarsToTypesStr = dict => Object.entries(dict)
.map(([varName, arg]) => `$${varName}: ${arg.type}`)
.join(', ');
/**
* Generate the query for the specified field
* @param curName name of the current field
* @param curParentType parent type of the current field
* @param curParentName parent name of the current field
* @param argumentsDict dictionary of arguments from all fields
* @param duplicateArgCounts map for deduping argument name collisions
* @param crossReferenceKeyList list of the cross reference
* @param curDepth current depth of field
* @param fromUnion adds additional depth for unions to avoid empty child
*/
const generateQuery = (
curName,
curParentType,
curParentName,
argumentsDict = {},
duplicateArgCounts = {},
crossReferenceKeyList = [], // [`${curParentName}To${curName}Key`]
curDepth = 1,
fromUnion = false,
_path = [], // all parent fields
) => {
const field = gqlSchema.getType(curParentType).getFields()[curName];
const path = [..._path, field];
/* run through custom filter function */
if (!filter(path, gqlSchema)) return '';
const curTypeName = field.type.toJSON().replace(/[[\]!]/g, '');
const curType = gqlSchema.getType(curTypeName);
let queryStr = '';
let childQuery = '';
if (curType.getFields) {
const crossReferenceKey = `${curParentName}To${curName}Key`;
if (
(!includeCrossReferences && crossReferenceKeyList.indexOf(crossReferenceKey) !== -1)
|| (fromUnion ? curDepth - 2 : curDepth) > depthLimit
) {
return '';
}
if (!fromUnion) {
crossReferenceKeyList.push(crossReferenceKey);
}
const childKeys = Object.keys(curType.getFields());
childQuery = childKeys
.filter((fieldName) => {
/* Exclude deprecated fields */
const fieldSchema = gqlSchema.getType(curType).getFields()[fieldName];
return includeDeprecatedFields || !fieldSchema.deprecationReason;
})
.map(cur => generateQuery(cur, curType, curName, argumentsDict, duplicateArgCounts,
crossReferenceKeyList, curDepth + 1, fromUnion, path).queryStr)
.filter(cur => Boolean(cur))
.join('\n');
}
if (!(curType.getFields && !childQuery)) {
queryStr = `${' '.repeat(curDepth)}${field.name}`;
if (field.args.length > 0) {
const dict = getFieldArgsDict(field, duplicateArgCounts, argumentsDict);
Object.assign(argumentsDict, dict);
queryStr += `(${getArgsToVarsStr(dict)})`;
}
if (childQuery) {
queryStr += `{\n${childQuery}\n${' '.repeat(curDepth)}}`;
}
}
/**
* Interface Types
*
* [1] Whenever we encounter an Interface, we need to add it's possible types' fields.
* [2] Fields common across all types shall not be added multiple times.
* [3] Graphql requires that across the possible types, there must not be any fields
* sharing the same name but different definitions. In such cases, we need to alias
* these fields, which we do by appending the type name to the field name.
*
* @example
* Schema:
* interface Item { id: Int! }
* type ItemA extends Item { title: String! }
* type ItemB extends Item { title: String }
* type Query { getItem: Item! }
*
* Result:
* getItem {
* id
* __typename
* ... on ItemA { title_ItemA: title }
* ... on ItemB { title_ItemB: title }
* }
*/
if (curType.astNode && curType.astNode.kind === 'InterfaceTypeDefinition') {
const types = gqlSchema.getPossibleTypes(curType); // [1]
const commonFields = Object.keys(curType.getFields()); // [2]
const allFields = types.reduce((acc, type) => [...acc, ...Object.values(type.getFields())], []);
const conflictFields = allFields
.filter((f1, i) => allFields.find((f2, j) => i !== j && f1.name === f2.name && !isEqualType(f1.type, f2.type)))
.map((field) => field.name)
.filter((field, index, self) => self.indexOf(field) === index);
if (types && types.length) {
const childQuery = types.map((type) => {
const depth = curDepth + 1;
const queryString = Object.keys(type.getFields())
.filter((field) => !commonFields.includes(field)) // [2]
.map(cur => {
const { queryStr } = generateQuery(cur, type, curName, argumentsDict, duplicateArgCounts,
crossReferenceKeyList, depth, false, path);
// use aliases for conflicting field names [3]
if (queryStr && conflictFields.includes(cur)) {
return queryStr.replace(cur, `${cur}_${type.name}: ${cur}`);
}
return queryStr;
})
.filter(cur => Boolean(cur))
.join('\n');
return queryString
? `... on ${type.name} {\n${unindent(queryString, depth - 1)}\n}`
: undefined;
}).filter(Boolean);
const interfaceQuery = ["__typename", ...childQuery].join("\n");
// insert before trailing "}", added by previous code
queryStr = queryStr.replace(/\n(\s*\})$/, `\n${indent(interfaceQuery, curDepth + 1)}\n\$1`);
}
}
/* Union types */
if (curType.astNode && curType.astNode.kind === 'UnionTypeDefinition') {
const types = curType.getTypes();
if (types && types.length) {
const indent = `${' '.repeat(curDepth)}`;
const fragIndent = `${' '.repeat(curDepth + 1)}`;
queryStr += '{\n';
queryStr += `${fragIndent}__typename\n`
for (let i = 0, len = types.length; i < len; i++) {
const valueTypeName = types[i];
const valueType = gqlSchema.getType(valueTypeName);
const unionChildQuery = Object.keys(valueType.getFields())
.map(cur => generateQuery(cur, valueType, curName, argumentsDict, duplicateArgCounts,
crossReferenceKeyList, curDepth + 2, true, path).queryStr)
.filter(cur => Boolean(cur))
.join('\n');
/* Exclude empty unions */
if (unionChildQuery) {
queryStr += `${fragIndent}... on ${valueTypeName} {\n${unionChildQuery}\n${fragIndent}}\n`;
}
}
queryStr += `${indent}}`;
}
}
return { queryStr, argumentsDict };
};
/**
* Generate the query for the specified field
* @param obj one of the root objects(Query, Mutation, Subscription)
* @param description description of the current object
*/
const generateFile = (obj, description) => {
let indexJs = 'const fs = require(\'fs\');\nconst path = require(\'path\');\n\n';
let outputFolderName;
switch (true) {
case /Mutation.*$/.test(description):
case /mutation.*$/.test(description):
outputFolderName = 'mutations';
break;
case /Query.*$/.test(description):
case /query.*$/.test(description):
outputFolderName = 'queries';
break;
case /Subscription.*$/.test(description):
case /subscription.*$/.test(description):
outputFolderName = 'subscriptions';
break;
default:
console.log('[gqlg warning]:', 'description is required');
}
const writeFolder = path.join(destDirPath, `./${outputFolderName}`);
try {
fs.mkdirSync(writeFolder);
} catch (err) {
if (err.code !== 'EEXIST') throw err;
}
Object.keys(obj).forEach((type) => {
const field = gqlSchema.getType(description).getFields()[type];
/* Only process non-deprecated queries/mutations: */
if (includeDeprecatedFields || !field.deprecationReason) {
const queryResult = generateQuery(type, description);
const varsToTypesStr = getVarsToTypesStr(queryResult.argumentsDict);
let query = queryResult.queryStr;
let queryName;
switch (true) {
case /Mutation/.test(description):
case /mutation/.test(description):
queryName = 'mutation';
break;
case /Query/.test(description):
case /query/.test(description):
queryName = 'query';
break;
case /Subscription/.test(description):
case /subscription/.test(description):
queryName = 'subscription';
break;
default:
break;
}
query = `${queryName || description.toLowerCase()} ${type}${varsToTypesStr ? `(${varsToTypesStr})` : ''}{\n${query}\n}`;
fs.writeFileSync(path.join(writeFolder, `./${type}.${fileExtension}`), query);
indexJs += `module.exports.${type} = fs.readFileSync(path.join(__dirname, '${type}.${fileExtension}'), 'utf8');\n`;
}
});
fs.writeFileSync(path.join(writeFolder, 'index.js'), indexJs);
indexJsExportAll += `module.exports.${outputFolderName} = require('./${outputFolderName}');\n`;
};
if (gqlSchema.getMutationType()) {
generateFile(gqlSchema.getMutationType().getFields(), gqlSchema.getMutationType().name);
} else {
console.log('[gqlg warning]:', 'No mutation type found in your schema');
}
if (gqlSchema.getQueryType()) {
generateFile(gqlSchema.getQueryType().getFields(), gqlSchema.getQueryType().name);
} else {
console.log('[gqlg warning]:', 'No query type found in your schema');
}
if (gqlSchema.getSubscriptionType()) {
generateFile(gqlSchema.getSubscriptionType().getFields(), gqlSchema.getSubscriptionType().name);
} else {
console.log('[gqlg warning]:', 'No subscription type found in your schema');
}
fs.writeFileSync(path.join(destDirPath, 'index.js'), indexJsExportAll);
}
module.exports = main
if (require.main === module) {
program
.option('--schemaFilePath [value]', 'path of your graphql schema file')
.option('--destDirPath [value]', 'dir you want to store the generated queries')
.option('--depthLimit [value]', 'query depth you want to limit (The default is 100)')
.option('--assumeValid [value]', 'assume the SDL is valid (The default is false)')
.option('--ext [value]', 'extension file to use', 'gql')
.option('-C, --includeDeprecatedFields [value]', 'Flag to include deprecated fields (The default is to exclude)')
.option('-R, --includeCrossReferences', 'Flag to include fields that have been added to parent queries already (The default is to exclude)')
.option('--filterPath [value]', 'path of a file containing a javascript function which filters fields')
.parse(process.argv);
return main({...program, fileExtension: program.ext })
}