UNPKG

graphql-composer

Version:
98 lines 2.72 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Schema = void 0; const graphql_1 = require("graphql"); const __1 = require("../.."); const Buildable_1 = require("../../classes/Buildable"); class Schema extends Buildable_1.Buildable { constructor() { super(); this._directives = []; this._types = []; } static get config() { return this._config; } get types() { return this._types; } /** * Create a new schema * @param types The type list to build */ static create(...types) { const schema = new Schema(); schema.setTypes(...types); return schema; } setConfig(config) { Schema._config = config || {}; return this; } /** * Set the type list to build * @param types The type list to build */ setTypes(...types) { this._types = [ ...types .flatMap((item) => { if (item instanceof __1.Wrapper) { return item.types; } return item; }) .filter((t) => t), ]; return this; } /** * Add a type to the list to build * @param types The type list to build */ addTypes(...types) { return this.setTypes(...this._types, ...types); } /** * Remove some types to the list * @param types The type list to remove */ removeTypes(...types) { return this.setTypes(...__1.ArrayHelper.remove(types, this._types)); } setDirectives(...directives) { this._directives = directives; return this; } addDirectives(...directives) { return this.setDirectives(...this._directives, ...directives); } removeDirectives(...directives) { return this.setDirectives(...__1.ArrayHelper.remove(directives, this._directives)); } /** * Build the schema and all the types contained in it */ build() { const types = this._types.map((t) => [ t.name, t.build(), ]); const typesMap = new Map(types); const query = typesMap.get("Query"); const mutation = typesMap.get("Mutation"); const subscription = typesMap.get("Subscription"); this._built = new graphql_1.GraphQLSchema({ query, mutation, subscription, types: types.map((t) => t[1]), description: this._description, directives: this._directives, }); return this._built; } } exports.Schema = Schema; Schema._config = {}; //# sourceMappingURL=Schema.js.map