UNPKG

graphql-composer

Version:
100 lines 3.07 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.InputType = void 0; const __1 = require("../../../.."); const graphql_1 = require("graphql"); const GQLType_1 = require("../GQLType"); class InputType extends GQLType_1.GQLType { constructor(name) { super(name); } get fields() { return this._fields; } get definitionNode() { return { kind: "InputObjectTypeDefinition", name: { kind: "Name", value: this.name, }, description: { kind: "StringValue", value: this.description, }, fields: this._fields.map((f) => f.definitionNode), directives: this.directives.map((d) => d.definitionNode), }; } static create(nameOrType) { if (typeof nameOrType === "string") { return new InputType(nameOrType); } else if (nameOrType instanceof GQLType_1.GQLType) { const obj = InputType.create(nameOrType.name) .setExtensions(nameOrType.extensions) .setDirectives(...nameOrType.directives) .setDescription(nameOrType.description); if (nameOrType instanceof InputType) { obj.setFields(...nameOrType.fields); } else { const objType = nameOrType; obj.setFields(...objType.fields.map((f) => __1.InputField.create(f))); } return obj; } else { const t = InputType.create(nameOrType.name); t._classType = nameOrType; return t; } } build() { const input = new graphql_1.GraphQLInputObjectType({ name: this._name, description: this._description, fields: () => { return this.fields.reduce((prev, field) => { prev[field.name] = field.build(); delete prev[field.name]["isDeprecated"]; return prev; }, {}); }, extensions: this.extensions, }); this._built = input; return input; } setFields(...fields) { this._fields = fields; return this; } addFields(...fields) { return this.setFields(...this._fields, ...fields); } removeFields(...fields) { return this.setFields(...__1.ArrayHelper.remove(fields, this._fields)); } /** * Add a suffix to the name of your type ("Input" by default) * @param suffix The suffix to add to the name */ suffix(suffix = "Input") { return this.setName(this.name + suffix); } copy() { return InputType.create(this); } convert(to) { return to.create(this); } transformFields(cb) { this.fields.map((field) => { cb(field); }); return this; } } exports.InputType = InputType; //# sourceMappingURL=InputType.js.map