Skip to content

Commit 4ab789a

Browse files
feature(plugin) use type defined in decorator instead introspection
1 parent 5c6e0c0 commit 4ab789a

File tree

2 files changed

+25
-17
lines changed

2 files changed

+25
-17
lines changed

packages/graphql/lib/plugin/visitors/model-class.visitor.ts

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -474,22 +474,27 @@ export class ModelClassVisitor {
474474
);
475475
}
476476

477-
private hasExplicitTypeInDecorator(
477+
private getExplicitTypeInDecoratorOrNull(
478478
member: ts.PropertyDeclaration | ts.GetAccessorDeclaration,
479-
) {
479+
): ts.ArrowFunction {
480480
const fieldDecorator = member.decorators?.find(
481481
(decorator) => getDecoratorName(decorator) === Field.name,
482482
);
483483

484484
if (!fieldDecorator) {
485-
return false;
485+
return null;
486486
}
487487

488488
const expression = fieldDecorator.expression as ts.CallExpression;
489-
return (
490-
expression.arguments.length > 0 &&
491-
ts.isArrowFunction(expression.arguments[0])
492-
);
489+
490+
if (
491+
expression.arguments.length > 0
492+
&& ts.isArrowFunction(expression.arguments[0])
493+
) {
494+
return expression.arguments[0];
495+
}
496+
497+
return null
493498
}
494499

495500
private createDecoratorObjectLiteralExpr(
@@ -504,8 +509,11 @@ export class ModelClassVisitor {
504509
!!node.questionToken || isNull(type) || isUndefined(type);
505510

506511
let typeArrowFunction: ts.ArrowFunction;
507-
const t = this.hasExplicitTypeInDecorator(node);
508-
if (!t) {
512+
const t = this.getExplicitTypeInDecoratorOrNull(node);
513+
514+
if (t) {
515+
typeArrowFunction = t;
516+
} else {
509517
const inlineStringEnumTypeName =
510518
this.getInlineStringEnumTypeOrUndefined(node);
511519

@@ -518,11 +526,11 @@ export class ModelClassVisitor {
518526
inlineStringEnumTypeName
519527
? f.createIdentifier(inlineStringEnumTypeName)
520528
: this.getTypeUsingTypeChecker(
521-
f,
522-
node.type,
523-
typeChecker,
524-
hostFilename,
525-
),
529+
f,
530+
node.type,
531+
typeChecker,
532+
hostFilename,
533+
),
526534
);
527535
}
528536

packages/graphql/tests/plugin/model-class-visitor.spec.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -609,7 +609,7 @@ var Status2;
609609
});
610610
});
611611

612-
it('Should not introspect type for field if user explicitly wrote a type', () => {
612+
it('Should use type defined in explicit decorator instead of introspection', () => {
613613
const source = `
614614
import { ID } from '@nestjs/graphql';
615615
@@ -620,7 +620,7 @@ class Model {
620620
*/
621621
@Field(() => ID)
622622
field: string;
623-
623+
624624
name: string;
625625
}
626626
`;
@@ -630,7 +630,7 @@ class Model {
630630
"import { ID } from '@nestjs/graphql';
631631
let Model = class Model {
632632
static _GRAPHQL_METADATA_FACTORY() {
633-
return { field: {}, name: { type: () => String } };
633+
return { field: { type: () => ID }, name: { type: () => String } };
634634
}
635635
};
636636
__decorate([

0 commit comments

Comments
 (0)