UNPKG

3.23 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 });
38 type_metadata_storage_1.TypeMetadataStorage.addClassFieldMetadata({
39 name: propertyKey,
40 schemaName: options.name || propertyKey,
41 typeFn: getType,
42 options: typeOptions,
43 target: prototype.constructor,
44 description: options.description,
45 deprecationReason: options.deprecationReason,
46 complexity: options.complexity,
47 middleware: options.middleware,
48 });
49 if (isResolver) {
50 type_metadata_storage_1.TypeMetadataStorage.addResolverPropertyMetadata({
51 kind: 'internal',
52 methodName: propertyKey,
53 schemaName: options.name || propertyKey,
54 target: prototype.constructor,
55 complexity: options.complexity,
56 });
57 }
58 };
59 if (loadEagerly) {
60 applyMetadataFn();
61 }
62 else {
63 lazy_metadata_storage_1.LazyMetadataStorage.store(prototype.constructor, applyMetadataFn, { isField: true });
64 }
65}
66exports.addFieldMetadata = addFieldMetadata;