UNPKG

@nestjs/graphql

Version:

Nest - modern, fast, powerful node.js web framework (@graphql)

73 lines (72 loc) 3.11 kB
import { SetMetadata } from '@nestjs/common'; import { isFunction, isObject, isString, } from '@nestjs/common/utils/shared.utils.js'; import { FIELD_RESOLVER_MIDDLEWARE_METADATA, RESOLVER_NAME_METADATA, RESOLVER_PROPERTY_METADATA, } from '../graphql.constants.js'; import { LazyMetadataStorage } from '../schema-builder/storages/lazy-metadata.storage.js'; import { TypeMetadataStorage } from '../schema-builder/storages/type-metadata.storage.js'; import { reflectTypeFromMetadata } from '../utils/reflection.utilts.js'; /** * Property resolver (method) Decorator. * * @publicApi */ export function ResolveField(propertyNameOrFunc, typeFuncOrOptions, resolveFieldOptions) { return (target, key, descriptor) => { // eslint-disable-next-line prefer-const let [propertyName, typeFunc, options] = isFunction(propertyNameOrFunc) ? typeFuncOrOptions && typeFuncOrOptions.name ? [ typeFuncOrOptions.name, propertyNameOrFunc, typeFuncOrOptions, ] : [undefined, propertyNameOrFunc, typeFuncOrOptions] : isString(propertyNameOrFunc) ? isFunction(typeFuncOrOptions) ? [propertyNameOrFunc, typeFuncOrOptions, resolveFieldOptions] : [propertyNameOrFunc, undefined, typeFuncOrOptions] : [ propertyNameOrFunc?.name, undefined, propertyNameOrFunc, ]; SetMetadata(RESOLVER_NAME_METADATA, propertyName)(target, key, descriptor); SetMetadata(RESOLVER_PROPERTY_METADATA, true)(target, key, descriptor); SetMetadata(FIELD_RESOLVER_MIDDLEWARE_METADATA, options?.middleware)(target, key, descriptor); options = isObject(options) ? { name: propertyName, ...options, } : propertyName ? { name: propertyName } : {}; LazyMetadataStorage.store(target.constructor, () => { let typeOptions, typeFn; try { const implicitTypeMetadata = reflectTypeFromMetadata({ metadataKey: 'design:returntype', prototype: target, propertyKey: key, explicitTypeFn: typeFunc, typeOptions: options, }); typeOptions = implicitTypeMetadata.options; typeFn = implicitTypeMetadata.typeFn; } catch { /* empty */ } TypeMetadataStorage.addResolverPropertyMetadata({ kind: 'external', methodName: key, schemaName: options.name || key, target: target.constructor, typeFn, typeOptions, description: options.description, deprecationReason: options.deprecationReason, complexity: options.complexity, }); }); }; }