UNPKG

@pothos/plugin-dataloader

Version:

A Pothos plugin for attaching dataloader to object types

94 lines (93 loc) 4.45 kB
import SchemaBuilder, { PothosSchemaError } from '@pothos/core'; import { ImplementableLoadableNodeRef, LoadableNodeRef } from './refs/index.js'; import { ImplementableLoadableInterfaceRef } from './refs/interface.js'; import { ImplementableLoadableObjectRef } from './refs/object.js'; import { LoadableUnionRef } from './refs/union.js'; import { dataloaderGetter } from './util.js'; const schemaBuilderProto = SchemaBuilder.prototype; schemaBuilderProto.loadableObjectRef = function loadableObjectRef(name, options) { return new ImplementableLoadableObjectRef(this, name, options); }; schemaBuilderProto.loadableInterfaceRef = function loadableInterfaceRef(name, options) { return new ImplementableLoadableInterfaceRef(this, name, options); }; schemaBuilderProto.loadableNodeRef = function loadableNodeRef(name, options) { return new ImplementableLoadableNodeRef(this, name, options); }; schemaBuilderProto.loadableObject = function loadableObject(nameOrRef, options) { var _options_name; const name = typeof nameOrRef === "string" ? nameOrRef : (_options_name = options.name) !== null && _options_name !== void 0 ? _options_name : nameOrRef.name; const ref = new ImplementableLoadableObjectRef(this, name, options); ref.implement(options); if (typeof nameOrRef !== "string") { this.configStore.associateParamWithRef(nameOrRef, ref); } return ref; }; schemaBuilderProto.loadableInterface = function loadableInterface(nameOrRef, options) { var _options_name; const name = typeof nameOrRef === "string" ? nameOrRef : (_options_name = options.name) !== null && _options_name !== void 0 ? _options_name : nameOrRef.name; const ref = new ImplementableLoadableInterfaceRef(this, name, options); ref.implement(options); if (typeof nameOrRef !== "string") { this.configStore.associateParamWithRef(nameOrRef, ref); } return ref; }; schemaBuilderProto.loadableUnion = function loadableUnion(name, { load, toKey, sort, cacheResolved, loaderOptions, ...options }) { const getDataloader = dataloaderGetter(loaderOptions, load, toKey, sort); const ref = new LoadableUnionRef(name, getDataloader); const unionRef = this.unionType(name, { ...options, extensions: { getDataloader, cacheResolved: typeof cacheResolved === "function" ? cacheResolved : cacheResolved && toKey } }); this.configStore.associateParamWithRef(ref, unionRef); return ref; }; const TloadableNode = schemaBuilderProto.loadableNode; schemaBuilderProto.loadableNode = function loadableNode(nameOrRef, options) { if (typeof this.nodeInterfaceRef !== "function") { throw new PothosSchemaError("builder.loadableNode requires @pothos/plugin-relay to be installed"); } var _options_name; const name = typeof nameOrRef === "string" ? nameOrRef : (_options_name = options.name) !== null && _options_name !== void 0 ? _options_name : nameOrRef.name; const ref = new LoadableNodeRef(this, name, options); var _options_isTypeOf; this.node(ref, { ...options, extensions: { ...options.extensions, pothosParseGlobalID: options.id.parse, getDataloader: ref.getDataloader, cacheResolved: typeof options.cacheResolved === "function" ? options.cacheResolved : options.cacheResolved && options.toKey }, loadManyWithoutCache: (ids, context) => ref.getDataloader(context).loadMany(ids), isTypeOf: (_options_isTypeOf = options.isTypeOf) !== null && _options_isTypeOf !== void 0 ? _options_isTypeOf : typeof nameOrRef === "function" ? (maybeNode) => { if (!maybeNode) { return false; } if (maybeNode instanceof nameOrRef) { return true; } const proto = Object.getPrototypeOf(maybeNode); try { if (proto === null || proto === void 0 ? void 0 : proto.constructor) { const config = this.configStore.getTypeConfig(proto.constructor); return config.name === name; } } catch { // ignore } return false; } : undefined }); if (typeof nameOrRef !== "string") { this.configStore.associateParamWithRef(nameOrRef, ref); } return ref; }; //# sourceMappingURL=schema-builder.js.map