UNPKG

@pothos/core

Version:

Pothos (formerly GiraphQL) is a plugin based schema builder for creating code-first GraphQL schemas in typescript

152 lines (151 loc) 6.16 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); Object.defineProperty(exports, "InputFieldBuilder", { enumerable: true, get: function() { return InputFieldBuilder; } }); const _arg = require("../refs/arg"); const _inputfield = require("../refs/input-field"); const _inputlist = require("../refs/input-list"); const _utils = require("../utils"); function _define_property(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } class InputFieldBuilder { argBuilder() { const builder = this.field.bind(this); const protoKeys = Object.keys(Object.getPrototypeOf(this)).filter((key)=>typeof this[key] === 'function' && Function.prototype[key] === undefined); for (const key of [ ...Object.keys(this), ...protoKeys ]){ builder[key] = typeof this[key] === 'function' ? this[key].bind(this) : this[key]; } return builder; } /** * Create in input field or argument for the current type * @param {PothosSchemaTypes.InputFieldOptions} [options={}] - Options for this field */ field(options) { const ref = this.kind === 'Arg' ? new _arg.ArgumentRef((name, field, typeConfig)=>{ const opts = options; var _opts_required, _opts_extensions; return { name, parentField: field, kind: this.kind, graphqlKind: this.kind, parentType: typeConfig.name, type: (0, _utils.inputTypeFromParam)(opts.type, this.builder.configStore, (_opts_required = opts.required) !== null && _opts_required !== void 0 ? _opts_required : this.builder.defaultInputFieldRequiredness), pothosOptions: opts, description: opts.description, deprecationReason: opts.deprecationReason, defaultValue: opts.defaultValue, extensions: (_opts_extensions = opts.extensions) !== null && _opts_extensions !== void 0 ? _opts_extensions : {}, astNode: opts.astNode }; }) : new _inputfield.InputFieldRef((name, typeConfig)=>{ const opts = options; var _opts_required, _opts_extensions; return { name, parentField: undefined, kind: this.kind, graphqlKind: this.kind, parentType: typeConfig.name, type: (0, _utils.inputTypeFromParam)(opts.type, this.builder.configStore, (_opts_required = opts.required) !== null && _opts_required !== void 0 ? _opts_required : this.builder.defaultInputFieldRequiredness), pothosOptions: opts, description: opts.description, deprecationReason: opts.deprecationReason, defaultValue: opts.defaultValue, extensions: (_opts_extensions = opts.extensions) !== null && _opts_extensions !== void 0 ? _opts_extensions : {}, astNode: opts.astNode }; }); return ref; } helper(type) { return (...args)=>{ const [options = {}] = args; return this.field({ ...options, type }); }; } constructor(builder, kind){ _define_property(this, "kind", void 0); _define_property(this, "builder", void 0); /** * Create a Boolean input field * @param {PothosSchemaTypes.InputFieldOptions} [options={}] - Options for this field */ _define_property(this, "boolean", this.helper('Boolean')); /** * Create a Float input field * @param {PothosSchemaTypes.InputFieldOptions} [options={}] - Options for this field */ _define_property(this, "float", this.helper('Float')); /** * Create a ID input field * @param {PothosSchemaTypes.InputFieldOptions} [options={}] - Options for this field */ _define_property(this, "id", this.helper('ID')); /** * Create a Int input field * @param {PothosSchemaTypes.InputFieldOptions} [options={}] - Options for this field */ _define_property(this, "int", this.helper('Int')); /** * Create a String input field * @param {PothosSchemaTypes.InputFieldOptions} [options={}] - Options for this field */ _define_property(this, "string", this.helper('String')); /** * Create a Boolean list input field * @param {PothosSchemaTypes.InputFieldOptions} [options={}] - Options for this field */ _define_property(this, "booleanList", this.helper([ 'Boolean' ])); /** * Create a Float list input field * @param {PothosSchemaTypes.InputFieldOptions} [options={}] - Options for this field */ _define_property(this, "floatList", this.helper([ 'Float' ])); /** * Create a ID list input field * @param {PothosSchemaTypes.InputFieldOptions} [options={}] - Options for this field */ _define_property(this, "idList", this.helper([ 'ID' ])); /** * Create a Int list input field * @param {PothosSchemaTypes.InputFieldOptions} [options={}] - Options for this field */ _define_property(this, "intList", this.helper([ 'Int' ])); /** * Create a String list input field * @param {PothosSchemaTypes.InputFieldOptions} [options={}] - Options for this field */ _define_property(this, "stringList", this.helper([ 'String' ])); _define_property(this, "listRef", (type, options)=>{ var _options_required; return new _inputlist.InputListRef(type, (_options_required = options === null || options === void 0 ? void 0 : options.required) !== null && _options_required !== void 0 ? _options_required : true); }); this.builder = builder; this.kind = kind; } } //# sourceMappingURL=input.js.map