UNPKG

2.45 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.Subscription = void 0;
4const common_1 = require("@nestjs/common");
5const shared_utils_1 = require("@nestjs/common/utils/shared.utils");
6require("reflect-metadata");
7const resolver_enum_1 = require("../enums/resolver.enum");
8const graphql_constants_1 = require("../graphql.constants");
9const undefined_return_type_error_1 = require("../schema-builder/errors/undefined-return-type.error");
10const lazy_metadata_storage_1 = require("../schema-builder/storages/lazy-metadata.storage");
11const type_metadata_storage_1 = require("../schema-builder/storages/type-metadata.storage");
12const reflection_utilts_1 = require("../utils/reflection.utilts");
13const resolvers_utils_1 = require("./resolvers.utils");
14/**
15 * Subscription handler (method) Decorator. Routes subscriptions to this method.
16 */
17function Subscription(nameOrType, options = {}) {
18 return (target, key, descriptor) => {
19 const name = (0, shared_utils_1.isString)(nameOrType)
20 ? nameOrType
21 : (options && options.name) || undefined;
22 (0, resolvers_utils_1.addResolverMetadata)(resolver_enum_1.Resolver.SUBSCRIPTION, name, target, key, descriptor);
23 (0, common_1.SetMetadata)(graphql_constants_1.SUBSCRIPTION_OPTIONS_METADATA, options)(target, key, descriptor);
24 lazy_metadata_storage_1.LazyMetadataStorage.store(target.constructor, () => {
25 if (!nameOrType || (0, shared_utils_1.isString)(nameOrType)) {
26 throw new undefined_return_type_error_1.UndefinedReturnTypeError(Subscription.name, key);
27 }
28 const { typeFn, options: typeOptions } = (0, reflection_utilts_1.reflectTypeFromMetadata)({
29 metadataKey: 'design:returntype',
30 prototype: target,
31 propertyKey: key,
32 explicitTypeFn: nameOrType,
33 typeOptions: options,
34 });
35 const metadata = {
36 methodName: key,
37 schemaName: options.name || key,
38 target: target.constructor,
39 typeFn,
40 returnTypeOptions: typeOptions,
41 description: options.description,
42 deprecationReason: options.deprecationReason,
43 };
44 type_metadata_storage_1.TypeMetadataStorage.addSubscriptionMetadata(metadata);
45 });
46 };
47}
48exports.Subscription = Subscription;