@notadd/graphql
Version:
notadd core none dependence
162 lines (161 loc) • 5.78 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.isObjectTypeDefinitionNode = exports.DefaultSchemaBuilder = exports.getTsconfig = void 0;
const tslib_1 = require("tslib");
const core_1 = require("@notadd/core");
const graphql_1 = require("graphql");
const path_1 = require("path");
const platform_1 = require("@notadd/platform");
const token_1 = require("./token");
const graphql_tools_1 = require("graphql-tools");
async function getTsconfig(path, existsSync) {
const dir = path_1.join(path, 'tsconfig.json');
if (await existsSync(dir) || await existsSync(path_1.join(path, 'package.json')))
return dir;
return getTsconfig(path_1.join(path, '..'), existsSync);
}
exports.getTsconfig = getTsconfig;
let DefaultSchemaBuilder = class DefaultSchemaBuilder {
constructor(injector) {
this.injector = injector;
}
async buildDocument() {
try {
if (this._node)
return this._node;
const mainPath = this.injector.get(core_1.MAIN_PATH);
const fs = this.injector.get(platform_1.Fs);
const ext = path_1.extname(mainPath);
const graphqlPath = mainPath.replace(ext, '.graphql');
const platformName = this.injector.get(core_1.PLATFORM_NAME, 'core');
if (!core_1.isDevMode()) {
const buffer = await fs.readFile(graphqlPath);
this._node = buffer.toString('utf-8');
}
else {
const toGraphql = require('@notadd/ast.ts-graphql').toGraphql;
const tsconfig = await getTsconfig(path_1.dirname(mainPath), fs.exists);
const graphql = toGraphql(mainPath, tsconfig, platformName);
await fs.writeFile(graphqlPath, graphql.graphql);
this._node = graphql.graphql;
}
return this._node;
}
catch (e) {
console.log(`build document`, e.message);
}
}
async buildSchema() {
if (this._schema)
return this._schema;
const ast = await this.buildDocument();
if (ast) {
const fields = await this.injector.get(core_1.GRAPHQL_FIELDS, Promise.resolve([]));
const resolver = this.injector.get(token_1.RESOLVER);
fields.push(ast);
const _ast = fields.map(it => graphql_1.parse(it)).reduce((a, b) => {
if (a) {
return this.mergeDocumentNode(a, b);
}
return b;
}, null);
const schema = graphql_tools_1.makeExecutableSchema({
typeDefs: [
_ast
],
resolvers: resolver
});
this._schema = schema;
return this._schema;
}
}
mergeObjectTypeDefinitionNode(a, b) {
if (!b.fields) {
return a;
}
if (b.fields.length === 0) {
return a;
}
if (!a.fields) {
return b;
}
if (a.fields.length === 0) {
return b;
}
const fields = [];
a.fields.forEach(afield => {
const same = fields.find(bfield => afield.name.value === bfield.name.value);
if (!same) {
fields.push(afield);
}
});
b.fields.forEach(afield => {
const same = fields.find(bfield => afield.name.value === bfield.name.value);
if (!same) {
fields.push(afield);
}
});
return {
...a,
fields
};
}
mergeDocumentNode(a, b) {
const definitions = [];
if (b.definitions.length === 0) {
return a;
}
else if (a.definitions.length === 0) {
return b;
}
else {
b.definitions.forEach((bit) => {
const same = definitions.find((ait) => ait.name.value === bit.name.value);
if (same) {
if (isObjectTypeDefinitionNode(same)) {
definitions.splice(definitions.indexOf(same), 1);
definitions.push(this.mergeObjectTypeDefinitionNode(same, bit));
}
}
else {
definitions.push(bit);
}
});
a.definitions.forEach((bit) => {
const same = definitions.find((ait) => ait.name.value === bit.name.value);
if (same) {
if (isObjectTypeDefinitionNode(same)) {
definitions.splice(definitions.indexOf(same), 1);
definitions.push(this.mergeObjectTypeDefinitionNode(same, bit));
}
}
else {
definitions.push(bit);
}
});
}
return {
kind: 'Document',
loc: undefined,
definitions
};
}
async buildIntrospection() {
if (this._introspection)
return this._introspection;
const schema = await this.buildSchema();
if (schema) {
this._introspection = graphql_1.introspectionFromSchema(schema);
return this._introspection;
}
}
};
DefaultSchemaBuilder = tslib_1.__decorate([
core_1.Injectable(),
tslib_1.__metadata("design:paramtypes", [core_1.Injector])
], DefaultSchemaBuilder);
exports.DefaultSchemaBuilder = DefaultSchemaBuilder;
function isObjectTypeDefinitionNode(val) {
return Reflect.get(val, 'kind') === 'ObjectTypeDefinition';
}
exports.isObjectTypeDefinitionNode = isObjectTypeDefinitionNode;