UNPKG

@nestjs/graphql

Version:

GraphQL for NestJS: code-first and schema-first APIs, resolvers as providers, subscriptions, federation and generated SDL, on Apollo or Mercurius.

92 lines (91 loc) • 4.07 kB
import { isFunction } from '@nestjs/common/utils/shared.utils.js'; import { applyIsOptionalDecorator, applyValidateIfDefinedDecorator, inheritPropertyInitializers, inheritTransformationMetadata, inheritValidationMetadata, } from '@nestjs/mapped-types'; import { Field } from '../decorators/index.js'; import { MetadataLoader } from '../plugin/metadata-loader.js'; import { METADATA_FACTORY_NAME } from '../plugin/plugin-constants.js'; import { getFieldsAndDecoratorForType } from '../schema-builder/utils/get-fields-and-decorator.util.js'; import { applyFieldDecorators } from './type-helpers.utils.js'; function isPartialTypeOptions(optionsOrDecorator) { return (optionsOrDecorator && ('decorator' in optionsOrDecorator || 'omitDefaultValues' in optionsOrDecorator || 'skipNullProperties' in optionsOrDecorator)); } export function PartialType(classRef, optionsOrDecorator) { let decorator; let omitDefaultValues = false; let skipNullProperties = true; if (isPartialTypeOptions(optionsOrDecorator)) { decorator = optionsOrDecorator.decorator; omitDefaultValues = optionsOrDecorator.omitDefaultValues; skipNullProperties = optionsOrDecorator.skipNullProperties ?? true; } else { decorator = optionsOrDecorator; } const { fields, decoratorFactory } = getFieldsAndDecoratorForType(classRef, { decorator, }); // Property initializers are default values too (the schema builder reads // them off an instance), so the ones of the fields are not inherited when // the default values are omitted. const fieldNames = new Set(); const isInheritedPredicate = (propertyKey) => !omitDefaultValues || !fieldNames.has(propertyKey); class PartialObjectType { constructor() { inheritPropertyInitializers(this, classRef, isInheritedPredicate); } } const applyPartialDecoratorFn = skipNullProperties === false ? applyValidateIfDefinedDecorator : applyIsOptionalDecorator; if (decorator) { decorator({ isAbstract: true })(PartialObjectType); } else { decoratorFactory({ isAbstract: true })(PartialObjectType); } inheritValidationMetadata(classRef, PartialObjectType); inheritTransformationMetadata(classRef, PartialObjectType); function applyFields(fields) { fields.forEach((item) => { fieldNames.add(item.name); if (isFunction(item.typeFn)) { // Execute type function eagerly to update the type options object (before "clone" operation) // when the passed function (e.g., @Field(() => Type)) lazily returns an array. item.typeFn(); } Field(item.typeFn, { ...item.options, nullable: item.options.nullable === 'items' || item.options.nullable === 'itemsAndList' ? 'itemsAndList' : true, defaultValue: omitDefaultValues ? undefined : item.options.defaultValue, })(PartialObjectType.prototype, item.name); applyPartialDecoratorFn(PartialObjectType, item.name); applyFieldDecorators(PartialObjectType, item, classRef); }); } applyFields(fields); // Register a refresh hook to update the fields when the serialized metadata // is loaded from file. MetadataLoader.addRefreshHook(() => { const { fields } = getFieldsAndDecoratorForType(classRef, { overrideFields: true, decorator, }); applyFields(fields); }); if (PartialObjectType[METADATA_FACTORY_NAME]) { const pluginFields = Object.keys(PartialObjectType[METADATA_FACTORY_NAME]()); pluginFields.forEach((key) => { fieldNames.add(key); applyPartialDecoratorFn(PartialObjectType, key); }); } Object.defineProperty(PartialObjectType, 'name', { value: `Partial${classRef.name}`, }); return PartialObjectType; }