UNPKG

graphql-modules

Version:

Create reusable, maintainable, testable and extendable GraphQL modules

79 lines (78 loc) 3.16 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Injectable = Injectable; exports.Optional = Optional; exports.Inject = Inject; exports.ExecutionContext = ExecutionContext; const providers_1 = require("./providers"); const metadata_1 = require("./metadata"); function ensureReflect() { if (!(Reflect && Reflect.getOwnMetadata)) { throw 'reflect-metadata shim is required when using class decorators'; } } function Injectable(options) { return (target) => { var _a; ensureReflect(); const params = (Reflect.getMetadata('design:paramtypes', target) || []).map((param) => ((0, providers_1.isType)(param) ? param : null)); const existingMeta = (0, metadata_1.readInjectableMetadata)(target); const meta = { params: ((_a = existingMeta === null || existingMeta === void 0 ? void 0 : existingMeta.params) === null || _a === void 0 ? void 0 : _a.length) > 0 && params.length === 0 ? existingMeta === null || existingMeta === void 0 ? void 0 : existingMeta.params : params.map((param, i) => { var _a; const existingParam = (_a = existingMeta === null || existingMeta === void 0 ? void 0 : existingMeta.params) === null || _a === void 0 ? void 0 : _a[i]; return { type: (existingParam === null || existingParam === void 0 ? void 0 : existingParam.type) || param, optional: typeof (existingParam === null || existingParam === void 0 ? void 0 : existingParam.optional) === 'boolean' ? existingParam.optional : false, }; }), options: { ...((existingMeta === null || existingMeta === void 0 ? void 0 : existingMeta.options) || {}), ...(options || {}), }, }; target[metadata_1.INJECTABLE] = meta; return target; }; } function Optional() { return (target, _, index) => { ensureReflect(); (0, metadata_1.ensureInjectableMetadata)(target); const meta = (0, metadata_1.readInjectableMetadata)(target); meta.params[index] = { ...meta.params[index], optional: true, }; }; } function Inject(type) { return (target, _, index) => { ensureReflect(); (0, metadata_1.ensureInjectableMetadata)(target); const meta = (0, metadata_1.readInjectableMetadata)(target); meta.params[index] = { type, optional: false, }; }; } function ExecutionContext() { return (obj, propertyKey) => { ensureReflect(); const target = obj.constructor; (0, metadata_1.ensureInjectableMetadata)(target); const meta = (0, metadata_1.readInjectableMetadata)(target); if (!meta.options) { meta.options = {}; } if (!meta.options.executionContextIn) { meta.options.executionContextIn = []; } meta.options.executionContextIn.push(propertyKey); }; }