UNPKG

@scalars/grapi

Version:

Grapi Schema Generator For GraphQL Server

65 lines (64 loc) 2.42 kB
"use strict"; exports.__esModule = true; var lodash_1 = require("./lodash"); var RootNode = (function () { function RootNode() { this.typeDef = ""; this.typeDefQuery = ""; this.typeDefMutation = ""; } RootNode.prototype.addQuery = function (query) { var _a; if (lodash_1.isString(query)) { if (!RootNode.findInSdl(query, this.typeDefQuery)) { this.typeDefQuery = (_a = this.typeDefQuery).concat.apply(_a, ["\n", query]); } } }; RootNode.prototype.addMutation = function (mutation) { var _a; if (lodash_1.isString(mutation)) { if (!RootNode.findInSdl(mutation, this.typeDefMutation)) { this.typeDefMutation = (_a = this.typeDefMutation).concat.apply(_a, ["\n", mutation]); } } }; RootNode.prototype.addObjectType = function (type) { this.addSdl(type, false); }; RootNode.prototype.addInput = function (input) { this.addSdl(input); }; RootNode.prototype.addEnum = function (enumDef, description) { if (description === void 0) { description = undefined; } this.addSdl(enumDef, true, description); }; RootNode.prototype.addSdl = function (sdl, validate, description) { var _a; if (validate === void 0) { validate = true; } if (description === void 0) { description = undefined; } if (lodash_1.isString(sdl)) { if (!validate || !RootNode.findInSdl(sdl, this.typeDef)) { this.typeDef = (_a = this.typeDef).concat.apply(_a, ["\n", description ? "\"\"\"" + description + "\"\"\" " : "", sdl]); } } }; RootNode.prototype.print = function () { return this.typeDef.concat(this.addQueriesAndMutations()); }; RootNode.prototype.addQueriesAndMutations = function () { var typeDef = ""; if (this.typeDefMutation) { typeDef = typeDef.concat("type Query { " + this.typeDefQuery + " } "); } if (this.typeDefMutation) { typeDef = typeDef.concat("type Mutation { " + this.typeDefMutation + " } "); } return typeDef; }; RootNode.findInSdl = function (sdlToAdd, sdlActual) { return (new RegExp(sdlToAdd.split("\n").shift(), 'gm')).test(sdlActual); }; return RootNode; }()); exports["default"] = RootNode;