-
Notifications
You must be signed in to change notification settings - Fork 0
/
SchemaBuilder.js
129 lines (129 loc) · 5.4 KB
/
SchemaBuilder.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
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.schemaConfigBuilder = exports.schema_builder = exports.getGqlFields = exports.getGQLType = void 0;
const fs = require("fs");
const path = require("path");
const graphql = require("graphql");
const graphql_1 = require("graphql");
const getGQLType = (configPath, name, field, isInput = false) => {
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: exports.getGqlFields(name, configPath, field, isInput),
description: field.description,
};
return isInput
? new graphql_1.GraphQLInputObjectType(o)
: new graphql_1.GraphQLObjectType(o);
case "array":
return new graphql.GraphQLList(exports.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 graphql_1.GraphQLNonNull(gql_field) : gql_field;
};
exports.getGQLType = getGQLType;
// export const buildType = (configPath: string, name: string, schema: any): GraphQLObjectType => {
// let fields = getGqlFields(configPath, schema);
// return new GraphQLObjectType({
// name,
// fields,
// description: schema.description
// });
// }
const getGqlFields = (parentname, configPath, schema, isInput = false) => {
let fields = {};
let properties = schema.properties;
let isImported = typeof properties === "string" &&
properties.startsWith("require:");
let fieldConfigPath = isImported
? path.join(path.dirname(configPath), properties.replace(/require:/, "").trim())
: configPath;
let properties_data = isImported
? JSON.parse(fs.readFileSync(fieldConfigPath).toString("utf8"))
: properties;
let importedFields = {};
for (let key in properties_data) {
let isKeyImported = typeof key === "string" && key.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 = Object.assign(Object.assign({}, importedFields), propertyData);
}
else {
fields[key] = {
type: exports.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: exports.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;
};
exports.getGqlFields = getGqlFields;
const schema_builder = (config_path) => {
return new graphql.GraphQLSchema(exports.schemaConfigBuilder(config_path));
};
exports.schema_builder = schema_builder;
const schemaConfigBuilder = (p) => {
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 = 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: exports.getGQLType(configPath, `response_${d.name}`, d.schema.response, false),
args: exports.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 = {};
Object.keys(queryFields).length > 0 &&
(() => {
schemaConfig.query = new graphql_1.GraphQLObjectType({
name: "Query",
fields: Object.assign({}, queryFields),
});
})();
Object.keys(mutationFields).length > 0 &&
(() => {
schemaConfig.mutation = new graphql_1.GraphQLObjectType({
name: "Mutation",
fields: Object.assign({}, mutationFields),
});
})();
return schemaConfig;
};
exports.schemaConfigBuilder = schemaConfigBuilder;