UNPKG

3.27 kBJavaScriptView Raw
1"use strict";
2/**
3 * The API surface of this module has been heavily inspired by the "type-graphql" library (https://github.com/MichalLytek/type-graphql), originally designed & released by Michal Lytek.
4 * In the v6 major release of NestJS, we introduced the code-first approach as a compatibility layer between this package and the `@nestjs/graphql` module.
5 * Eventually, our team decided to reimplement all the features from scratch due to a lack of flexibility.
6 * To avoid numerous breaking changes, the public API is backward-compatible and may resemble "type-graphql".
7 */
8Object.defineProperty(exports, "__esModule", { value: true });
9exports.addFieldMetadata = exports.Field = void 0;
10const shared_utils_1 = require("@nestjs/common/utils/shared.utils");
11const lazy_metadata_storage_1 = require("../schema-builder/storages/lazy-metadata.storage");
12const type_metadata_storage_1 = require("../schema-builder/storages/type-metadata.storage");
13const reflection_utilts_1 = require("../utils/reflection.utilts");
14/**
15 * @Field() decorator is used to mark a specific class property as a GraphQL field.
16 * Only properties decorated with this decorator will be defined in the schema.
17 */
18function Field(typeOrOptions, fieldOptions) {
19 return (prototype, propertyKey, descriptor) => {
20 addFieldMetadata(typeOrOptions, fieldOptions, prototype, propertyKey, descriptor);
21 };
22}
23exports.Field = Field;
24function addFieldMetadata(typeOrOptions, fieldOptions, prototype, propertyKey, descriptor, loadEagerly) {
25 const [typeFunc, options = {}] = (0, shared_utils_1.isFunction)(typeOrOptions)
26 ? [typeOrOptions, fieldOptions]
27 : [undefined, typeOrOptions];
28 const applyMetadataFn = () => {
29 const isResolver = !!descriptor;
30 const isResolverMethod = !!(descriptor && descriptor.value);
31 const { typeFn: getType, options: typeOptions } = (0, reflection_utilts_1.reflectTypeFromMetadata)({
32 metadataKey: isResolverMethod ? 'design:returntype' : 'design:type',
33 prototype,
34 propertyKey,
35 explicitTypeFn: typeFunc,
36 typeOptions: options,
37 ignoreOnUndefinedType: loadEagerly,
38 });
39 type_metadata_storage_1.TypeMetadataStorage.addClassFieldMetadata({
40 name: propertyKey,
41 schemaName: options.name || propertyKey,
42 typeFn: getType,
43 options: typeOptions,
44 target: prototype.constructor,
45 description: options.description,
46 deprecationReason: options.deprecationReason,
47 complexity: options.complexity,
48 middleware: options.middleware,
49 });
50 if (isResolver) {
51 type_metadata_storage_1.TypeMetadataStorage.addResolverPropertyMetadata({
52 kind: 'internal',
53 methodName: propertyKey,
54 schemaName: options.name || propertyKey,
55 target: prototype.constructor,
56 complexity: options.complexity,
57 });
58 }
59 };
60 if (loadEagerly) {
61 applyMetadataFn();
62 }
63 else {
64 lazy_metadata_storage_1.LazyMetadataStorage.store(prototype.constructor, applyMetadataFn, { isField: true });
65 }
66}
67exports.addFieldMetadata = addFieldMetadata;