-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmerge.ts
69 lines (60 loc) · 2.18 KB
/
merge.ts
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
import { Command, flags } from "@oclif/command";
import { GraphQLSchema, printSchema } from "graphql";
import { makeExecutableSchema } from "@graphql-tools/schema";
import { startSpinner, stopSpinner } from "../lib/spinner";
import { writeFileAsync, mkdirRecursiveAsync } from "../lib/fs";
import { mergeInputs } from "../lib/inquirer";
import { APITYPE } from "../utils/constants";
const tools = require("graphql-schema-utilities");
export default class Merge extends Command {
static description = "Merge multiple GraphQL schema in one";
static flags = {
help: flags.help({ char: "h" }),
};
async run() {
const { flags } = this.parse(Merge);
let config = await mergeInputs();
const mergeSpinner = startSpinner("Merging GraphQL Schema");
if (config.api_type === APITYPE.graphql) {
tools
.mergeGQLSchemas(config.schema)
.then((schema: GraphQLSchema) => {
let data = "";
data = printSchema(schema);
if (
schema.getQueryType()!.toString() === "Query" &&
(!schema.getMutationType() ||
schema.getMutationType()!.toString() === "Mutation") &&
(!schema.getSubscriptionType() ||
schema.getSubscriptionType()!.toString() === "Subscription")
) {
const typeDefs = `schema { \n query: Query ${
schema.getMutationType() ? "\n mutation: Mutation" : ""
} ${
schema.getSubscriptionType()
? "\n subscription: Subscription"
: ""
} \n}\n\n`;
data = typeDefs + data;
}
// Validating Schema
makeExecutableSchema({ typeDefs: data });
writeFileAsync(config.output, data, (err: string) => {
if (err) {
stopSpinner(mergeSpinner, `Error: ${err}`, true);
process.exit(1);
}
});
stopSpinner(mergeSpinner, `Done`, false);
})
.catch((error:Error) => {
stopSpinner(
mergeSpinner,
`schema files were merged, and the valid schema is: ${error}`,
true
);
process.exit(1);
});
}
}
}