UNPKG

@pothos/core

Version:

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

143 lines (142 loc) 5.94 kB
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; } import { ArgumentRef } from '../refs/arg.js'; import { InputFieldRef } from '../refs/input-field.js'; import { InputListRef } from '../refs/input-list.js'; import { inputTypeFromParam } from '../utils/index.js'; export 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 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: 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 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: 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 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