UNPKG

@eleven-am/nestjs-graphql-crud

Version:

nestjs-graphql-crud is a library that aims to reduce the boilerplate code needed to create a GraphQL CRUD API.

99 lines 3.99 kB
"use strict"; /** * @module decorators * @description Provides decorators and utility functions for GraphQL resolvers */ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; var __metadata = (this && this.__metadata) || function (k, v) { if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.CurrentAbility = exports.PUB_SUB_SYMBOL = exports.PaginationArgs = void 0; exports.firstLetterUppercase = firstLetterUppercase; exports.createFindMany = createFindMany; exports.CurrentPubSub = CurrentPubSub; const common_1 = require("@nestjs/common"); const graphql_1 = require("@nestjs/graphql"); const authorizer_1 = require("@eleven-am/authorizer"); /** * Utility function to capitalize the first letter of a string * * @param {string} str - Input string * @returns {string} String with first letter uppercase and remaining letters lowercase */ function firstLetterUppercase(str) { return str.charAt(0).toUpperCase() + str.slice(1); } /** * Input type for pagination arguments */ let PaginationArgs = class PaginationArgs { }; exports.PaginationArgs = PaginationArgs; __decorate([ (0, graphql_1.Field)(() => Number, { nullable: true }), __metadata("design:type", Number) ], PaginationArgs.prototype, "take", void 0); __decorate([ (0, graphql_1.Field)(() => Number, { nullable: true }), __metadata("design:type", Number) ], PaginationArgs.prototype, "skip", void 0); exports.PaginationArgs = PaginationArgs = __decorate([ (0, graphql_1.InputType)() ], PaginationArgs); /** * Cache to store generated FindMany input types */ const findManyArgsCache = new Map(); /** * Creates a GraphQL input type for FindMany operations * * @template T - The type of the where input * @param {Type<T>} whereInput - The where input class * @param {string} modelName - The name of the model * @returns {Type<FindManyContract<T>>} A dynamically generated input type for FindMany operations */ function createFindMany(whereInput, modelName) { const className = `${firstLetterUppercase(modelName)}FindManyArgs`; // Return a cached class if it exists if (findManyArgsCache.has(className)) { return findManyArgsCache.get(className); } /** * Input type for FindMany operations */ let FindManyArgs = class FindManyArgs { }; __decorate([ (0, graphql_1.Field)(() => whereInput, { nullable: true }), __metadata("design:type", Object) ], FindManyArgs.prototype, "where", void 0); __decorate([ (0, graphql_1.Field)(() => PaginationArgs, { nullable: true }), __metadata("design:type", PaginationArgs) ], FindManyArgs.prototype, "pagination", void 0); FindManyArgs = __decorate([ (0, graphql_1.InputType)(className) ], FindManyArgs); // Give the dynamic class a descriptive name for easier debugging Object.defineProperty(FindManyArgs, 'name', { value: className, writable: false, }); // Cache the class for future use findManyArgsCache.set(className, FindManyArgs); return FindManyArgs; } exports.PUB_SUB_SYMBOL = Symbol('PUB_SUB_SYMBOL'); function CurrentPubSub() { return (0, common_1.Inject)(exports.PUB_SUB_SYMBOL); } exports.CurrentAbility = (0, authorizer_1.createParamDecorator)((ctx) => { return ctx.getData('ABILITY_KEY'); }); //# sourceMappingURL=decorators.js.map