@grapi/server
Version:
Grapi Schema Generator For GraphQL Server
42 lines (41 loc) • 1.42 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
class RootNode {
typeDef = ``;
typeDefQuery = ``;
typeDefMutation = ``;
addQuery(query) {
if (!RootNode.findInSdl(query, this.typeDefQuery)) {
this.typeDefQuery = this.typeDefQuery.concat(...[`\n`, query]);
}
}
addMutation(mutation) {
this.typeDefMutation = this.typeDefMutation.concat(...[`\n`, mutation]);
}
addObjectType(type) {
this.addSdl(type, false);
}
addInput(input) {
this.addSdl(input);
}
addEnum(enumDef, description = undefined) {
this.addSdl(enumDef.getTypeDef(), true, description);
}
addSdl(sdl, validate = true, description = undefined) {
if (!validate || !RootNode.findInSdl(sdl, this.typeDef)) {
this.typeDef = this.typeDef.concat(...[`\n`, description ? `"""${description}""" ` : ``, sdl]);
}
}
print() {
return this.typeDef.concat(this.addQueriesAndMutations());
}
addQueriesAndMutations() {
return ``
.concat(this.typeDefQuery ? `type Query { ${this.typeDefQuery} } ` : ``)
.concat(this.typeDefMutation ? `type Mutation { ${this.typeDefMutation} } ` : ``);
}
static findInSdl(sdlToAdd, sdlActual) {
return (new RegExp(sdlToAdd.split(`\n`).shift(), 'gm')).test(sdlActual);
}
}
exports.default = RootNode;