UNPKG

@pothos/plugin-with-input

Version:

A Pothos plugin for defining fields with input objects

47 lines (46 loc) 2.31 kB
import { InputFieldBuilder, RootFieldBuilder } from '@pothos/core'; const rootBuilderProto = RootFieldBuilder.prototype; function capitalize(s) { return `${s.slice(0, 1).toUpperCase()}${s.slice(1)}`; } function defaultGetName({ parentTypeName, fieldName }) { return `${parentTypeName}${capitalize(fieldName)}Input`; } rootBuilderProto.fieldWithInput = function fieldWithInput({ typeOptions: { name: typeName, ...typeOptions } = {}, argOptions: { name: argName = "input", ...argOptions } = {}, args, input, ...fieldOptions }) { var _this_builder_options_withInput; const inputRef = this.builder.inputRef(typeName !== null && typeName !== void 0 ? typeName : "UnnamedWithInput"); const fieldRef = this.field({ args: { ...args, [argName]: this.arg({ required: true, ...(_this_builder_options_withInput = this.builder.options.withInput) === null || _this_builder_options_withInput === void 0 ? void 0 : _this_builder_options_withInput.argOptions, ...argOptions, type: inputRef }) }, ...fieldOptions }); fieldRef.onFirstUse((config) => { var _this_builder_options_withInput; var _this_builder_options_withInput_typeOptions; const { name: getTypeName = defaultGetName, ...defaultTypeOptions } = (_this_builder_options_withInput_typeOptions = (_this_builder_options_withInput = this.builder.options.withInput) === null || _this_builder_options_withInput === void 0 ? void 0 : _this_builder_options_withInput.typeOptions) !== null && _this_builder_options_withInput_typeOptions !== void 0 ? _this_builder_options_withInput_typeOptions : {}; const name = typeName !== null && typeName !== void 0 ? typeName : getTypeName({ parentTypeName: config.parentType, fieldName: config.name }); inputRef.name = name; this.builder.inputType(inputRef, { fields: () => input, ...defaultTypeOptions, ...typeOptions }); }); return fieldRef; }; Object.defineProperty(rootBuilderProto, "input", { get: function getInputBuilder() { return new InputFieldBuilder(this.builder, "InputObject"); } }); //# sourceMappingURL=schema-builder.js.map