-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
55 lines (48 loc) · 1.34 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
#!/usr/bin/env node
const fs = require("fs");
const program = require("commander");
const _ = require("lodash");
const { generateGraph } = require("./src/graph-generator");
const { composeQueries } = require("./src/query-composer");
// Collect provided args
program
.option("--schema [value]", "path to graphql schema file")
.option("--maxDepth [value]", "maximum query depth (default is 10)")
.option(
"--optionalArgumentsToInclude [items]",
"comma seperated list of optional arguments to include (default is [])",
getList
)
.option(
"--aliasCount [value]",
"number of times to include the generated base query (default is 1)"
)
.parse(process.argv);
function getList(value) {
return value.split(",");
}
const {
schema,
maxDepth = 10,
optionalArgumentsToInclude = [],
aliasCount = 1,
} = program;
const typeDef = fs.readFileSync(schema, "utf-8");
// Schema to graph conversion
const graphqlGraph = generateGraph(typeDef);
// console.log(graphqlGraph);
// Composing queries by traversing the generated graph
const queries = composeQueries(
graphqlGraph,
maxDepth,
optionalArgumentsToInclude,
aliasCount
);
// console.log(queries);
fs.writeFile('queries.graphql', queries.join('\n\n'), function(err){
if(err) {
console.log(err);
} else {
console.log('Composed queries added to queries.graphql');
}
});