-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.ts
85 lines (73 loc) · 1.76 KB
/
index.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
const path = require("path")
const fs = require("fs")
const glob = require("fast-glob")
export type resolverType = {
Query?: {}
Mutation?: {}
}
export const combineResolvers = ({
resolvers,
}: {
resolvers: resolverType[]
}): resolverType => {
let Query = {}
let Mutation = {}
resolvers.map((resolver: resolverType) => {
if (resolver.hasOwnProperty("Query")) {
Query = { ...Query, ...resolver.Query }
}
if (resolver.hasOwnProperty("Mutation")) {
Mutation = { ...Mutation, ...resolver.Mutation }
}
})
return { Query, Mutation }
}
export const mergeSchemas = ({
pathfiles,
}: {
pathfiles: string
}): string[] => {
let schemas: string[] = []
glob.sync(pathfiles).forEach(function (file: any) {
try {
const data: string = fs.readFileSync(path.resolve(file), "utf8")
schemas.push(data)
} catch (err) {
throw new Error(
"Oops Error ! Couldn't merge definitions Type , check schemas"
)
}
})
return schemas
}
export const generateSchema = ({
inputPath,
schemaPath = "",
typeDefsPath = "",
}: {
inputPath: string
schemaPath?: string
typeDefsPath?: string
}): void => {
const mergedSchemas = mergeSchemas({ pathfiles: inputPath })
const schema: string = mergedSchemas.join(" ")
if (schemaPath !== "") {
fs.writeFile(schemaPath, schema, (err: any) => {
if (err) {
console.error(err)
return
}
})
console.log(" ✔ Schema Generated : ", schemaPath)
}
if (typeDefsPath !== "") {
const typeDefs: string = "export default `" + schema + "`;"
fs.writeFile(typeDefsPath, typeDefs, (err: any) => {
if (err) {
console.error(err)
return
}
})
console.log(" ✔ typeDefs Generated : ", typeDefsPath)
}
}