-
Notifications
You must be signed in to change notification settings - Fork 0
/
SchemaBuilder.ts
177 lines (167 loc) · 4.97 KB
/
SchemaBuilder.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
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
import * as fs from "fs";
import * as path from "path";
import * as graphql from "graphql";
import {
GraphQLObjectType,
GraphQLInputObjectType,
GraphQLNonNull,
} from "graphql";
export const getGQLType = (
configPath: string,
name: string,
field: any,
isInput: boolean = false
): any => {
let type = field.type;
let isRequired = field.required;
let gql_field = (() => {
switch (type) {
case "number":
case "float":
return graphql.GraphQLFloat;
case "string":
return graphql.GraphQLString;
case "integer":
case "int":
return graphql.GraphQLInt;
case "boolean":
return graphql.GraphQLBoolean;
case "object":
let o = {
name,
fields: getGqlFields(name, configPath, field, isInput),
description: field.description,
};
return isInput
? new GraphQLInputObjectType(o)
: new GraphQLObjectType(o);
case "array":
return new graphql.GraphQLList(
getGQLType(configPath, name, field.items, isInput)
);
default:
console.error({ type, configPath, name, field });
throw new Error("graphql-json-schema: Unsupported type " + type);
}
})();
return isRequired ? new GraphQLNonNull(gql_field) : gql_field;
};
// export const buildType = (configPath: string, name: string, schema: any): GraphQLObjectType => {
// let fields = getGqlFields(configPath, schema);
// return new GraphQLObjectType({
// name,
// fields,
// description: schema.description
// });
// }
export const getGqlFields = (
parentname: string,
configPath: string,
schema: any,
isInput = false
): any => {
let fields: { [key: string]: any } = {};
let properties = schema.properties;
let isImported =
typeof properties === "string" &&
(properties as string).startsWith("require:");
let fieldConfigPath = isImported
? path.join(
path.dirname(configPath),
properties.replace(/require:/, "").trim()
)
: configPath;
let properties_data: { [key: string]: any } = isImported
? JSON.parse(fs.readFileSync(fieldConfigPath).toString("utf8"))
: properties;
let importedFields = {};
for (let key in properties_data) {
let isKeyImported =
typeof key === "string" && (key as string).startsWith("require:");
if (isKeyImported) {
// let keyConfigPath = path.join(path.dirname(configPath), properties_data[key].replace(/require:/, '').trim())
let propertyData = JSON.parse(
fs.readFileSync(fieldConfigPath).toString("utf8")
);
importedFields = { ...importedFields, ...propertyData };
} else {
fields[key] = {
type: getGQLType(
fieldConfigPath,
`${parentname}_${key}`,
properties_data[key],
isInput
),
description:
typeof properties_data[key].description === "string"
? properties_data[key].description
: JSON.stringify(properties_data[key].description),
};
}
}
properties_data = importedFields;
for (let key in properties_data) {
fields[key] = {
type: getGQLType(
fieldConfigPath,
`${parentname}_${key}`,
properties_data[key],
isInput
),
description:
typeof properties_data[key].description === "string"
? properties_data[key].description
: JSON.stringify(properties_data[key].description),
};
}
return fields;
};
export const schema_builder = (config_path: string): graphql.GraphQLSchema => {
return new graphql.GraphQLSchema(schemaConfigBuilder(config_path));
};
export const schemaConfigBuilder = (p: string): any => {
let config = JSON.parse(fs.readFileSync(p).toString("utf8"));
let dependencies = config.dependencies;
let configPathDir = path.dirname(p);
let queryFields = {};
let mutationFields = {};
for (let d of dependencies) {
let destinationBucket: any =
d.type === "Query" ? queryFields : mutationFields;
let configPath = path.join(configPathDir, d.path);
d.schema = JSON.parse(fs.readFileSync(configPath).toString("utf8"));
destinationBucket[d.name] = {
type: getGQLType(
configPath,
`response_${d.name}`,
d.schema.response,
false
),
args: getGqlFields(d.name, configPath, d.schema.request, true),
description:
typeof d.schema.request.description === "string"
? d.schema.request.description
: JSON.stringify(d.schema.request.description),
};
}
var schemaConfig: any = {};
Object.keys(queryFields).length > 0 &&
(() => {
schemaConfig.query = new GraphQLObjectType({
name: "Query",
fields: {
...queryFields,
},
});
})();
Object.keys(mutationFields).length > 0 &&
(() => {
schemaConfig.mutation = new GraphQLObjectType({
name: "Mutation",
fields: {
...mutationFields,
},
});
})();
return schemaConfig;
};