-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
138 lines (138 loc) Β· 6.87 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
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
"use strict";
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
var __param = (this && this.__param) || function (paramIndex, decorator) {
return function (target, key) { decorator(target, key, paramIndex); }
};
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
// import sub modules
const helper_1 = require("./helper");
const parser_1 = require("./parser");
const gQlAstToDbMeta_1 = require("./gQlAstToDbMeta");
exports.registerDirectiveParser = gQlAstToDbMeta_1.registerDirectiveParser;
// fullstack-one core
const di_1 = require("@fullstack-one/di");
// DI imports
const logger_1 = require("@fullstack-one/logger");
const config_1 = require("@fullstack-one/config");
const boot_loader_1 = require("@fullstack-one/boot-loader");
const helper_2 = require("@fullstack-one/helper");
const utils = require("./parser/utils");
exports.utils = utils;
let GraphQlParser = class GraphQlParser {
constructor(loggerFactory, config, bootLoader) {
this.gQlSdlExtensions = [];
this.parsers = [];
// register package config
config.addConfigFolder(__dirname + '/../config');
this.logger = loggerFactory.create('GraphQl');
this.graphQlConfig = config.getConfig('graphql');
this.ENVIRONMENT = config.ENVIRONMENT;
bootLoader.addBootFunction(this.boot.bind(this));
}
addParser(parser) {
this.parsers.push(parser);
}
getDbMeta() {
return this.dbMeta;
}
extendSchema(schema) {
this.gQlSdlExtensions.push(schema);
}
getGqlRuntimeData() {
return {
dbMeta: this.dbMeta,
views: this.views,
expressions: this.expressions,
gQlRuntimeDocument: this.gQlRuntimeDocument,
gQlRuntimeSchema: this.gQlRuntimeSchema,
gQlTypes: this.gQlTypes,
mutations: this.mutations,
queries: this.queries,
customOperations: this.customOperations
};
}
getGQlSdl() {
// return copy insted of ref
return Object.assign({}, this.gQlSdl);
}
getGQlAst() {
// return copy insted of ref
return Object.assign({}, this.gQlAst);
}
boot() {
return __awaiter(this, void 0, void 0, function* () {
try {
// load schema
const gQlSdlPattern = this.ENVIRONMENT.path + this.graphQlConfig.schemaPattern;
this.gQlSdl = yield helper_2.helper.loadFilesByGlobPattern(gQlSdlPattern);
// Combine all Schemas to a big one and add extensions from other modules
const gQlSdlCombined = this.gQlSdl.concat(this.gQlSdlExtensions.slice()).join('\n');
this.gQlAst = helper_1.graphQl.helper.parseGraphQlSchema(gQlSdlCombined);
this.dbMeta = gQlAstToDbMeta_1.parseGQlAstToDbMeta(this.gQlAst);
// load permissions and expressions and generate views and put them into schemas
try {
// load permissions
const viewsPattern = this.ENVIRONMENT.path + this.graphQlConfig.viewsPattern;
const viewsArray = yield helper_2.helper.requireFilesByGlobPattern(viewsPattern);
this.views = [].concat.apply([], viewsArray);
// load expressions
const expressionsPattern = this.ENVIRONMENT.path + this.graphQlConfig.expressionsPattern;
const expressionsArray = yield helper_2.helper.requireFilesByGlobPattern(expressionsPattern);
this.expressions = [].concat.apply([], expressionsArray);
const viewSchemaName = di_1.Container.get(config_1.Config).getConfig('db').viewSchemaName;
const combinedSchemaInformation = parser_1.runtimeParser(this.gQlAst, this.views, this.expressions, this.dbMeta, viewSchemaName, this.parsers);
this.gQlRuntimeDocument = combinedSchemaInformation.document;
this.gQlRuntimeSchema = helper_1.graphQl.helper.printGraphQlDocument(this.gQlRuntimeDocument);
this.gQlTypes = combinedSchemaInformation.gQlTypes;
this.queries = combinedSchemaInformation.queries;
this.mutations = combinedSchemaInformation.mutations;
this.customOperations = {
fields: combinedSchemaInformation.customFields,
queries: combinedSchemaInformation.customQueries,
mutations: combinedSchemaInformation.customMutations
};
Object.values(combinedSchemaInformation.dbViews).forEach((dbView) => {
if (this.dbMeta.schemas[dbView.viewSchemaName] == null) {
this.dbMeta.schemas[dbView.viewSchemaName] = {
tables: {},
views: {}
};
}
this.dbMeta.schemas[dbView.viewSchemaName].views[dbView.viewName] = dbView;
});
}
catch (err) {
throw err;
}
return this.dbMeta;
}
catch (err) {
this.logger.warn('boot.error', err);
}
});
}
};
GraphQlParser = __decorate([
di_1.Service(),
__param(0, di_1.Inject(type => logger_1.LoggerFactory)),
__param(1, di_1.Inject(type => config_1.Config)),
__param(2, di_1.Inject(type => boot_loader_1.BootLoader)),
__metadata("design:paramtypes", [Object, Object, Object])
], GraphQlParser);
exports.GraphQlParser = GraphQlParser;