UNPKG

@webundsoehne/nestjs-graphql-typeorm-dataloader

Version:

A library to utilize the graphql-dataloader with nestjs and typeorm.

59 lines (58 loc) 3.17 kB
var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __name = (target, value) => __defProp(target, "name", { value, configurable: true }); var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); var typeorm_loader_middleware_exports = {}; __export(typeorm_loader_middleware_exports, { TypeormLoaderMiddleware: () => TypeormLoaderMiddleware }); module.exports = __toCommonJS(typeorm_loader_middleware_exports); var import_context = require("../constants/context.constants"); var import_extension_field = require("../constants/extension-field.constants"); var import_loader_handlers = require("../loader-handlers/index"); const TypeormLoaderMiddleware = /* @__PURE__ */ __name(async ({ context, info, source }, next) => { const extensions = info.parentType.getFields()[info.fieldName].extensions?.[import_extension_field.TYPEORM_DATALOADER_EXTENSION_FIELD]; const args = extensions?.args; if (!args) { return next(); } if (context?.[import_context.DATA_LOADER_CONTEXT_KEY].typeormGetConnection == null) { throw new Error("Typeorm connection is not set."); } const relation = context[import_context.DATA_LOADER_CONTEXT_KEY].typeormGetConnection().getMetadata(extensions.target.constructor).findRelationWithPropertyPath(extensions.key.toString()); if (relation == null) { return next(); } if (args?.options?.selfKey && !(relation.isOneToMany || relation.isOneToOneNotOwner)) { throw new Error("selfKey option is available only for OneToMany or OneToOneNotOwner."); } if (relation.isManyToOne || relation.isOneToOneOwner) { return (0, import_loader_handlers.handleToOne)(args.keyFunc, source, context, relation); } else if (relation.isOneToMany) { return args?.options?.selfKey ? (0, import_loader_handlers.handleOneToManyWithSelfKey)(args.keyFunc, source, context, relation) : (0, import_loader_handlers.handleToMany)(args.keyFunc, source, context, relation); } else if (relation.isOneToOneNotOwner) { return args?.options?.selfKey ? (0, import_loader_handlers.handleOneToOneNotOwnerWithSelfKey)(args.keyFunc, source, context, relation) : (0, import_loader_handlers.handleToOne)(args.keyFunc, source, context, relation); } else if (relation.isManyToMany) { return (0, import_loader_handlers.handleToMany)(args.keyFunc, source, context, relation); } else { return next(); } }, "TypeormLoaderMiddleware"); // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { TypeormLoaderMiddleware });